I'm writing a Bison inspired parsing framework, and there's something in the Bison manual that I don't really understand.
From the Bison manual 5.3.5 How Precedence Works:
...each rule gets its precedence from the last terminal symbol mentioned in the components.
and
Not all rules and not all tokens have precedence. If either the rule or the lookahead token has no precedence, then the default is to shift.
As far as I can understand, this comes down to:
- Token has precedence, rule doesn't -> shift
- Rule has precedence, token doesn't -> shift
- Neither has any precedence -> shift
- Both have precedence:
- Token has higher precedence -> shift
- Rule has higher precedence -> reduce
- Equal precedence -> use the associativity of the precedence level to determine action
But wouldn't that mean that a rule that doesn't contain any terminal symbol with a declared precedence would never be reduced? What am I missing?
For example, the below grammar:
S -> E + E
E -> foo1
E -> foo2
Gives a dfa with seven states, the first two are:
- State 0
- Items:
- S' -> . S $
- S -> . E + E
- E -> . foo1
- E -> . foo2
- Edges:
- E -> 3
- S -> 4
- foo1 -> 1
- foo2 -> 2
- Items:
- State 1
- Items:
- E -> foo1 . lookahead: +,$
- Items:
When parsing the string foo1 + foo2, the parser shifts the token foo1 and ends up in state 1. The next lookahead token is +, neither '+' nor the rule E -> foo1 has any declared precedence, which means the parser should shift, but there is no such edge in state 1, which leads the parser to return a syntax error.
Thanks in advance