This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

59 linhas
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")