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.

38 lines
1.2 KiB

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