I'm trying to wrap my head around parser theory, and I keep finding the same example in different sources. The grammar goes approximately like this (simplified):
E = T
E = E + T
T = 0..9
So supposedly a string 2 + 2
will be parsed as such ("|" separates the stack from the reminder)
|2 + 2 <-can't reduce, shift
2|+ 2 <-reduce by T = 0..9
T|+ 2 <-reduce by E = T
E|+ 2 <-can't reduce, shift
E +|2 <-can't reduce, shift
E + 2| <-reduce by T = 0..9
E + T| <-reduction by E = E + T here?
E| <-done
The question is, at E + T
step parser can apply two different reductions to the rightmost part of the stack: E = T
(resulting in E + E
) and E = E + T
(resulting in E
). And I can't find a clear and conscise explanation how it chooses one over the other.
What am I missing?