见图:
1. The parser will obtain the first token from the scanner — obtaining "The", an article. The parser will push the "The" on the stack (Figure 1-a).parser得到“The”,它是个冠词,所以parser将它置入堆栈
4. The parser will obtain another token from the scanner — obtaining "a", an article. There is no rule reducing "noun-phrase verb article" and so it pushes the "a" on the stack (Figure 1-d).
parser得到“a”,它是个名词,因为这里没有规则来减少“名词短语 动词 冠词”,所以parser将a栈parser得到“.”,它是个分隔符,因为这里有规则“名词短语 动词 名词短语 分隔符”,所以parser将栈情况,在栈中放入一句话“The man ate a worm”
这里的合并被称为Reduce,入栈被称为Shfit。
其实bison中的parser是一个有限状态机,下面一句话是从gun网站上copy来的,很好的说明了这个原理。
Each time a lookahead(当前获得到的token) token is read, the current parser state together with the type of lookahead token are looked up in a table. This table entry can say, “Shift the lookahead token.” In this case, it also specifies the new parser state, which is pushed onto the top of the parser stack. Or it can say, “Reduce using rule number n.” This means that a certain number of tokens or groupings are taken off the top of the stack, and replaced by one grouping. In other words, that number of states are popped from the stack, and one new state is pushed.