This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1003 B

  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. if __name__ == '__main__':
  26. print(parser.parse(sentence).pretty())
  27. # make_png(sys.argv[1])
  28. # Output:
  29. #
  30. # _ambig
  31. # comparative
  32. # noun fruit
  33. # verb flies
  34. # noun bananas
  35. # simple
  36. # noun
  37. # fruit
  38. # flies
  39. # verb like
  40. # noun bananas
  41. #
  42. # (or view a nicer version at "./fruitflies.png")