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.

46 regels
1.1 KiB

  1. """
  2. Earley’s dynamic lexer
  3. ======================
  4. Demonstrates the power of Earley’s dynamic lexer on a toy configuration language
  5. Using a lexer for configuration files is tricky, because values don't
  6. have to be surrounded by delimiters. Using a standard lexer for this just won't work.
  7. In this example we use a dynamic lexer and let the Earley parser resolve the ambiguity.
  8. Another approach is to use the contextual lexer with LALR. It is less powerful than Earley,
  9. but it can handle some ambiguity when lexing and it's much faster.
  10. See examples/conf_lalr.py for an example of that approach.
  11. """
  12. from lark import Lark
  13. parser = Lark(r"""
  14. start: _NL? section+
  15. section: "[" NAME "]" _NL item+
  16. item: NAME "=" VALUE? _NL
  17. NAME: /\w/+
  18. VALUE: /./+
  19. %import common.NEWLINE -> _NL
  20. %import common.WS_INLINE
  21. %ignore WS_INLINE
  22. """, parser="earley")
  23. def test():
  24. sample_conf = """
  25. [bla]
  26. a=Hello
  27. this="that",4
  28. empty=
  29. """
  30. r = parser.parse(sample_conf)
  31. print (r.pretty())
  32. if __name__ == '__main__':
  33. test()