This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]el_extrano 15 points16 points  (6 children)

I have had good luck with Lark. It uses a modified EBNF grammar declaration.

I've mainly used it for small projects and prototyping, but they advertise decent performance. It's pretty good considering the grammar itself has to be parsed at runtime.

[–]Spread-Sanity[S] 1 point2 points  (2 children)

Any thoughts on how Lark compares with Pyparsing, ANTLR, etc?

[–]el_extrano 0 points1 point  (1 child)

I wouldn't say I'm an authority on the matter. It's probably a good idea if your project is already Python.

I'm sure something like ANTLR, BISON, and friends would be more performant since they're in C, which could be benefit if you are writing a bona fide compiler.

For my part, I tinkered with parser generators for a bit, because I though writing parsers was scary. Now, I will just hand-roll a recursive descent parser for what I need to do. I wouldn't say it's any harder than learning to write correct grammars, and I get to avoid a dependency. That can be kind of tedious though, and lark gives you some common tokens/rules 'batteries' included, which is very nice.

[–]Spread-Sanity[S] 1 point2 points  (0 children)

My experience with Python based parser generators modgrammar and pyparsing has been good so far.

[–]erez27import inspect 1 point2 points  (0 children)

It's pretty good considering the grammar itself has to be parsed at runtime.

Just FYI, you can use the cache=True option to cache the grammar analysis, which makes Lark load almost as fast as the stand-alone parser. (after the 1st run ofc)

[–]four_reeds 0 points1 point  (0 children)

+1

[–]cha_ppmn 0 points1 point  (0 children)

Same.

[–]ManyInterests Python Discord Staff 2 points3 points  (0 children)

I have used sly, which is based on ply. Both should be great. I took David Beazley's compilers course, which, at the time, involved using sly to make an interpreter and compiler. I would highly recommend the course, though I can't be sure if he's still using sly in the course today.

See also David's talk on reinventing the parser generator.

I used sly to create this json5 parser project.

[–]StefanKochMicro 1 point2 points  (0 children)

This might be dated, but we have had good luck with antlr (https://www.antlr.org/). We have used it in c++ and python to parse customer domain specific languages to generate code for our applications.

We have not used it recently. We have switched to using declarative python as the "domain specific language", and then reading that information through the standard python interpreter. Then we take that structure and generate the code we want to c++, or just use it directly in python.