Python parser for Python-like language

后端 未结 3 1292
遥遥无期
遥遥无期 2021-02-13 16:48

I\'m looking to write a Python import filter or preprocessor for source files that are essentially Python with extra language elements. The goal is to read the source file, pars

相关标签:
3条回答
  • 2021-02-13 16:59

    I like SimpleParse a lot, but I never tried to feed it the Python grammar (BTW, is it a deterministic grammar?). If it chokes, PLY will do the job.

    See this compilation about Python parsing tools.

    0 讨论(0)
  • 2021-02-13 17:00

    The first thing that comes to mind is lib2to3. It is a complete pure-Python implementation of a Python parser. It reads a Python grammar file and parses Python source files according to this grammar. It offers a great infrastructure for performing AST manipulations and writing back nicely formatted Python code -- after all it's purpose is to transform between two Python-like languages with slightly different grammars.

    Unfortunately it's lacking documentation and doesn't guarantee a stable interface. There are projects that build on top of lib2to3 nevertheless, and the source code is quite readable. If API stability is an issue, you can just fork it.

    0 讨论(0)
  • 2021-02-13 17:08

    I would recommend that you check out my library: https://github.com/erezsh/lark

    It can parse ALL context-free grammars, automatically builds an AST (with line & column numbers), and accepts the grammar in EBNF format, which is considered the standard.

    It can easily parse a language like Python, and it can do so faster than any other parsing library written in Python.

    In fact, there's already an example python grammar and parser

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