This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

59 righe
1.1 KiB

  1. """
  2. Handling Ambiguity
  3. ==================
  4. A demonstration of ambiguity
  5. This example shows how to use get explicit ambiguity from Lark's Earley parser.
  6. """
  7. import sys
  8. from lark import Lark, tree
  9. grammar = """
  10. sentence: noun verb noun -> simple
  11. | noun verb "like" noun -> comparative
  12. noun: adj? NOUN
  13. verb: VERB
  14. adj: ADJ
  15. NOUN: "flies" | "bananas" | "fruit"
  16. VERB: "like" | "flies"
  17. ADJ: "fruit"
  18. %import common.WS
  19. %ignore WS
  20. """
  21. parser = Lark(grammar, start='sentence', ambiguity='explicit')
  22. sentence = 'fruit flies like bananas'
  23. def make_png(filename):
  24. tree.pydot__tree_to_png( parser.parse(sentence), filename)
  25. def make_dot(filename):
  26. tree.pydot__tree_to_dot( parser.parse(sentence), filename)
  27. if __name__ == '__main__':
  28. print(parser.parse(sentence).pretty())
  29. # make_png(sys.argv[1])
  30. # make_dot(sys.argv[1])
  31. # Output:
  32. #
  33. # _ambig
  34. # comparative
  35. # noun fruit
  36. # verb flies
  37. # noun bananas
  38. # simple
  39. # noun
  40. # fruit
  41. # flies
  42. # verb like
  43. # noun bananas
  44. #
  45. # (or view a nicer version at "./fruitflies.png")