Resources for lexing, tokenising and parsing in python

后端 未结 8 2683
鱼传尺愫
鱼传尺愫 2020-12-04 06:41

Can people point me to resources on lexing, parsing and tokenising with Python?

I\'m doing a little hacking on an open source project (hotwire) and wanted to do a fe

8条回答
  •  执念已碎
    2020-12-04 07:37

    For medium-complex grammars, PyParsing is brilliant. You can define grammars directly within Python code, no need for code generation:

    >>> from pyparsing import Word, alphas
    >>> greet = Word( alphas ) + "," + Word( alphas ) + "!" # <-- grammar defined here
    >>> hello = "Hello, World!"
    >>>> print hello, "->", greet.parseString( hello )
    Hello, World! -> ['Hello', ',', 'World', '!']
    

    (Example taken from the PyParsing home page).

    With parse actions (functions that are invoked when a certain grammar rule is triggered), you can convert parses directly into abstract syntax trees, or any other representation.

    There are many helper functions that encapsulate recurring patterns, like operator hierarchies, quoted strings, nesting or C-style comments.

提交回复
热议问题