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.

44 linhas
1.2 KiB

  1. """
  2. LALR’s contextual lexer
  3. =======================
  4. This example demonstrates the power of LALR's contextual lexer,
  5. by parsing a toy configuration language.
  6. The terminals `NAME` and `VALUE` overlap. They can match the same input.
  7. A standard lexer would arbitrarily choose one over the other, based on priority,
  8. which would lead to a (confusing) parse error.
  9. However, due to the unambiguous structure of the grammar, Lark's LALR(1) algorithm knows
  10. which one of them to expect at each point during the parse.
  11. The lexer then only matches the tokens that the parser expects.
  12. The result is a correct parse, something that is impossible with a regular lexer.
  13. Another approach is to use the Earley algorithm.
  14. It will handle more cases than the contextual lexer, but at the cost of performance.
  15. See examples/conf_earley.py for an example of that approach.
  16. """
  17. from lark import Lark
  18. parser = Lark(r"""
  19. start: _NL? section+
  20. section: "[" NAME "]" _NL item+
  21. item: NAME "=" VALUE? _NL
  22. NAME: /\w/+
  23. VALUE: /./+
  24. %import common.NEWLINE -> _NL
  25. %import common.WS_INLINE
  26. %ignore WS_INLINE
  27. """, parser="lalr")
  28. sample_conf = """
  29. [bla]
  30. a=Hello
  31. this="that",4
  32. empty=
  33. """
  34. print(parser.parse(sample_conf).pretty())