-
I'm having an issue with dealing with disambiguation. Very simplified, my grammar is effectively this: expression -> StringExpression | NumericExpression StringExpression -> StringExpression | StringLiteral | Identifier Because Identifier lives as an option under both StringExpression and NumericExpression, the grammar is ambiguous. I thought of changing to this: expression -> StringExpression | NumericExpression | Identifier StringExpression -> StringExpression | StringLiteral But then the identifier cannot be a part of a String or Numeric Expression. I could probably do something like this: expression -> StringExpression | NumericExpression | Identifier StringExpression -> expression | StringLiteral But then I am mixing types, but I could handle type validation after the parser, in the visitor. Is there away around this that I am not seeing ? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey @khkiley, the grammar isn't ambiguous due to the There are quite a few resources that show how to solve this. See here. It generally boils down to rewriting the grammar rules to eliminate the left recursive subrule call. Regarding the |
Beta Was this translation helpful? Give feedback.
Hey @khkiley,
the grammar isn't ambiguous due to the
Identifier
, but rather due to theStringExpression -> StringExpression
andNumericExpression -> NumericExpression
parts. It's not even really ambiguous: Instead this issue is called direct left recursion. It's one of the main limitations of LL parsers.There are quite a few resources that show how to solve this. See here. It generally boils down to rewriting the grammar rules to eliminate the left recursive subrule call.
Regarding the
Identifier
issue: Most people don't actually write their grammar split by string or numeric expressions. They just have operation expression (i.e. with 2 or more operators), unary expressions and then prim…