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.

50 lines
932 B

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