I'm using the pest crate to implement a recursive grammar in Rust:
id = _{ ASCII_ALPHA_LOWER ~ (ASCII_ALPHANUMERIC|"_")* }integer = _{ (ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*)|"0" }real = _{ ((integer ~ "." ~ ASCII_DIGIT*) | (integer? ~ "." ~ ASCII_DIGIT+)) ~ (("e"|"E") ~ ("-"|"+")? ~ ASCII_DIGIT+)? }unaryop = _{ "sin"|"cos"|"tan"|"exp"|"ln"|"sqrt" }inner_exp = _{ real|integer|"pi"|id }exp = { SOI ~ ( inner_exp | (exp ~ ( "+"|"-"|"*"|"/"|"^" ) ~ inner_exp) | ("-" ~ exp) | ("(" ~ exp ~ ")") | (unaryop ~ "(" ~ exp ~ ")") ) ~ EOI }
However, I'm finding that pest isn't parsing the grammar as I would expect. For instance, 2+3
gives me an error of:
--> 1:2 |1 | 2+3 | ^--- | = expected EOI
It appears that the inner_exp
choice is being parsed and then, when the +
symbol is encountered, the parser doesn't know what to do. I'm pretty sure there's a problem with how I've written the exp ~ ( "+"|"-"|"*"|"/"|"^" ) ~ inner_exp
choice but I'm not sure what exactly is causing the problem. If I replace that choice with exp ~ ( "+"|"-"|"*"|"/"|"^" ) ~ exp
I get an error stating that the expression is left-recursive. How do I fix this grammar?