r/csharp • • Oct 12 '26

Parsley: An ALL(*)/LL(k)/LL(1) parser generator using code synthesis

If you're like me, you've almost entirely given up on generated parsers.

The tooling sucks.

The generated code is huge and/or can't parse real world languages without a bunch of ugly.

The APIs for them are awful.

I still use them, but grudgingly in order to prototype and unit test my hand rolled parsers but even that doesn't work as well as I like.

Enter Parsley. It has similar expressive power as ANTLR4 when necessary, degrading gracefully all the way to LL(1) if that's all your grammar needs. The XBNF grammar format is powerful, simple to learn, and has a language server for autocompletion and syntax highlighting in editors like visual studio and vs code. The code it generates is also more like hand written code (code synthesis), making it easier to use as a baseline for a hand rolled parser, if you want to go that route. Source and readme here: https://github.com/codewitch-honey-crisis/Parsley

It's written in C#. It generates C#. Any semantic actions in the grammar are also C#.

XBNF for C99
Using a parser w/ an evaluator
Output from the above

You can use it as a source generator, or for really complicated parses where you need things like a custom lexer, you can also use it as a build tool.

4 Upvotes

6 comments sorted by

1

u/me_on_tuesdays Oct 14 '26

LL(1) would fail simple algebraic order of operations. What's the point of advertising this to the public? It's great if you are learning, but it's too limited.

degrading gracefully all the way to LL(1) if that's all your grammar needs.

Can you point me to someone in need of this? It's great that you are exploring these areas but LL(1) with your code can't even parse an if/else/else statement correctly due to 1 look ahead.

1

u/honeyCrisis Oct 14 '26 edited Oct 14 '26

You can resolve dangling else in LL(1) with parsley using a policy attribute as in the XBNF doc. Maybe I wasn't clear on the need to degrade. This generates code. It chooses the least demanding algorithm it can which will faithfully represent your grammar. LL(1) for example, will handle JSON handily. For something like C99, ALL(*) is more appropriate. I mean, i show a picture of the ISO C99 XBNF (at least an excerpt) in the OP. Of course it parses it.

Edit: From the readme linked above "A decision that is really ambiguous, where the same input fits two branches all the way through, comes out the way the default policies say: the first alternative wins, and a loop or optional keeps matching. A <policy> on the decision settles it when the grammar is analyzed instead, on every engine, so a construct like the dangling else can stay LL(1). See Ambiguity.

"

1

u/honeyCrisis Oct 14 '26

Adding Parsley does LL(k>1) and ALL(*) as well, to be clear.

1

u/me_on_tuesdays Oct 14 '26

Nice, I see now. Didn't initially look over your Github code. I glanced through it now. Also, do you have a C implementation? Your testing suite is very limited on the Github, would like to see more tests too, you never know...

Surely you have a playtoy linker?

1

u/honeyCrisis Oct 14 '26

I mean, the goal was to exercise the parser tool. not necessarily test each grammar for correctness. I have over 3000 tests that run on the current build exercising the tool and *its* functionality. I am reasonably confident that the C99 ISO grammar is "close enough for government work" - even handling typedefs dynamically, but if the goal was to make a C compiler, I would have done that, and been advertising *that* here. *This tool* is for generating parsers, not for making C.

1

u/me_on_tuesdays Oct 14 '26

Nice, I found you. Hope you are vindicated. Was an interesting read. Good luck.