开发者

Python3 parser generator

开发者 https://www.devze.com 2023-03-08 11:04 出处:网络
I\'m looking for a parser generator for a reasonably complex language (similar in complexity to Python itself) which works with Python3. If it can generate开发者_JAVA百科 an AST automatically, this wo

I'm looking for a parser generator for a reasonably complex language (similar in complexity to Python itself) which works with Python3. If it can generate开发者_JAVA百科 an AST automatically, this would be a bonus, but I'm fine if it just calls rules while parsing. I have no special requirements, nor does it have to be very efficient/fast.


LEPL isn't exactly a parser generator - it's better! The parsers are defined in Python code and constructed at runtime (hence some inefficiency, but much easier to use). It uses operator overloading to construct a quite readable DSL. Things like c = a & b | b & c for the BNF c := a b | b c..

You can pass the results of a (sub-)parser to an abritary callable, and this is very usable for AST generation (also useful for converting e.g. number literals to Python-level number objects). It's a recursive descent parser, so you better avoid left recursion in the grammar (there are memoization objets that can make left recursion work, but "Lepl's support for them has historically been unreliable (buggy)").


ANTLR can generate a lexer and/or parser in Python. You can also use it to create AST's and iterator-like structures to walk the AST (called tree grammars).

See ANTLR get and split lexer content for an ANTLR demo that produces an AST with the Python target.

0

精彩评论

暂无评论...
验证码 换一张
取 消