Can't we have smarter parser generators that do make debugging nice, but are still formally verified?
Can't we have smarter parser generators that do make debugging nice, but are still formally verified?
Menhir is a parser generator that tries to do that: http://gallium.inria.fr/~fpottier/slides/fpottier-2015-11-ou... (Don't get scared by the first slide; the title is in French, the rest is in English.)
> formally verified
Menhir has been used for a formally verified parser for C: http://gallium.inria.fr/~xleroy/publi/validated-parser.pdf
The result is in CompCert: https://github.com/AbsInt/CompCert/tree/master/cparser
It generates recursive descent parsers, it even allows left-recursive grammars - it will rewrite the grammar on its own and wrangle it back into left-recursive form for your convenience, no need to shuffle a right-recursive AST back into left-recursive form by yourself.
It's a side project so it could use a lot of improvement, especially in the debugging messages side.
But it does prove left-recursive descent grammars can be generated automatically without getting into infinite loop and without hassle of manually reversing right-recursive AST's.
Isn't that basically what PEG parser generators are?
- Recursive descent parser for statements
- "Canned" configurable expression parser for expressions: https://github.com/stefanhaustein/expressionparser
- Both use a simple regex based generic tokenizer
- In more complex cases, parse the expressions to an unresolved syntax tree first (constructed from the bottom), then resolve the tree in a secondary top-down step (potentially to a linear representation).
It's fairly straightforward to make one. One exercise in Wirth's short book Compiler Construction is to make an LL(1) parser generator. You even make it check for ambiguities. (I've even found it useful on a couple occasions.)