Resources for lexing, tokenising and parsing in python

后端 未结 8 2229
鱼传尺愫
鱼传尺愫 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.

    0 讨论(0)
  • 2020-12-04 07:45

    I'm a happy user of PLY. It is a pure-Python implementation of Lex & Yacc, with lots of small niceties that make it quite Pythonic and easy to use. Since Lex & Yacc are the most popular lexing & parsing tools and are used for the most projects, PLY has the advantage of standing on giants' shoulders. A lot of knowledge exists online on Lex & Yacc, and you can freely apply it to PLY.

    PLY also has a good documentation page with some simple examples to get you started.

    For a listing of lots of Python parsing tools, see this.

    0 讨论(0)
提交回复
热议问题