This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Você não pode selecionar mais de 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.

46 linhas
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()