yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
884 stars 112 forks source link

Packrat not working with precedence {} #112

Closed sasq64 closed 4 years ago

sasq64 commented 4 years ago

If I try to enable packrat parsing with my grammar, I get syntax errors for even the simplest input.

What specific requirements does packrat put on the grammar?

yhirose commented 4 years ago

@sasq64, could you give me the simplest grammar and source text to reproduce it? Thanks!

sasq64 commented 4 years ago

As soon as the grammar tries to evaluate an expression, which uses precedence climibng, I get a parse error. Is precedence not compatible with packrat perhaps?

sasq64 commented 4 years ago

Yes, if just remove the precedence {} clause it parses (except of course can't do correct math :)

yhirose commented 4 years ago

@sasq64, I added enable_packrat_parsing() to precedence expression parsing tests, but I couldn't reproduce the problem. The tests work fine. https://github.com/yhirose/cpp-peglib/commit/c1f087a91e5b1d6d2d569ca4a5f9d2108246dd16

Could you give me the smallest possible grammar that can reveal the problem? Thanks for your help!

sasq64 commented 4 years ago

Parsing 1+2 fails using

Root <- Expression EOT
Decimal <- [0-9]+
_ <- ' '+
EOT <- !.
Expression  <- Atom (Operator Atom)* {
                         precedence
                           L - +
                           L / *
                       }

Atom <- _? (Decimal  / '(' Expression ')') _?
Operator <-  '+' / '-' / '*' / '/'

If I take away the whitespace rules from Atom it works.

yhirose commented 4 years ago

@sasq64, thanks for the sample. I can now reproduce it. I'll try to fix it when I have time. Thanks!

yhirose commented 4 years ago

Should be ok now.

sasq64 commented 4 years ago

Thank you, my parsing just got a nice 30% or so speed bump.