-
Hello, I have this rather simple grammar that should parse traditional math expressions such as these:
It does not follow any operator precedence and is strictly left-to-right (i.e. long left scope). I see I can't wrap my head around why this grammar will skip possibly matching rule. Any ideas please? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
At position 0, it tries to match What you probably want to do is to eliminate the alternation in the body of
|
Beta Was this translation helpful? Give feedback.
At position 0, it tries to match
Expression
→BinaryExpression
→Expression
. This is left recursive, so the innerExpression
fails. But, sinceBinaryExpression
only has one alternative, that means that it also fails.What you probably want to do is to eliminate the alternation in the body of
Expression
, and instead create a precedence between the different types of expression. This ensures that the left recursive application ofBinaryExpression
eventually succeeds. For example, here is a snippet from a Smalltalk grammar that I wrote a while back: