4

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 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.

Anteru
  • 19,042
  • 12
  • 77
  • 121

2 Answers2

3

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)").

3

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.

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • ANTLR's python support library claims to be 2.x based, does it work with Python3? – Anteru May 27 '11 at 10:58
  • 1
    It does now: [pip install antlr4-python3-runtime](https://pypi.python.org/pypi/antlr4-python3-runtime) – cfi Aug 06 '15 at 11:33
  • Has a Linux and a Mac example on its front page and does not lose one word why it does not support the other 85% of Computers. Also, the documentation is payware. – Nils Lindemann Oct 14 '15 at 15:12
  • @Nils, it also has a Windows example on the homepage. Sure, there are books you can buy, but there's plenty of good online material available to get started with ANTLR. – Bart Kiers Oct 14 '15 at 17:55
  • @Bart the [startpage](http://www.antlr.org/) has _no_ Windows example. It has an OS X quickstart example, a grammar example, a Linux commandline session example, an example containing the word "Linux" and an example containing the word "Mac". This is unfixed for _more_ _than_ _a_ _year_ now. Also the spelling errors in the doc. Also, no addons to the doc for years. Eg how to create a python 3 parser in an executable *.py file which prints something to the console so that i can see how this works from the _very start_ to the _very end_. Tell me, which quality should i expect from the codebase?! – Nils Lindemann Oct 16 '15 at 19:16
  • 1
    @Nils the start page's quick start tab has 3 small round buttons in the lower right corner: 1st is OSX, 2nd is Linux and the 3rd is Windows. You are right that this has been the same for more than a year. I will not argue with you about the other things you mention because it seems to me you're not really open to a normal discussion. And relating miss spelled words with code quality, really? Shall I compare the quality of your eye sight with your coding or research skills? Or would that be silly too? – Bart Kiers Oct 16 '15 at 19:42
  • +1 for you and -1 for me for that i didnt see the two examples in the 'quickstart' tab. However the examples in the 'Samples' tab are still broken, the doc is full of spelling errors, ANTLR is extremely difficult to install. I just tried it the fourth time (from [here](https://github.com/jszheng/py3antlr4book)) and i failed again as it wont accept my python 3.2 version, which i wont change. Yes, i compare this sluttiness to the code, it is a simple tool to avoid stress for me. See also Rule 17 of the Zen of Python. BTW, i use [Varas](https://pypi.python.org/pypi/Varas). One file. Hackable. – Nils Lindemann Oct 17 '15 at 13:46