bison和flex的基本原理

本文通过一个简单的例子详细解释了解析器的工作流程。解析器通过扫描输入串获取符号,并利用堆栈进行符号的存储与合并,形成语法树。文章具体展示了如何处理一系列词汇,包括冠词、名词、动词等,并最终构建出完整的句子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

见图:

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将它置入堆栈


2. The parser will obtain another token from the scanner — obtaining "man", a noun. Since there is an article on the stack, the parser will pop the article off the stack and replace it with a noun-phrase "The man" (Figure 1-b).
parser得到“man”,它是个名词,因为堆栈中有一个冠词,所以parser将这个冠词从堆栈中取出,和man合并后,放入堆栈
3. The parser will obtain another token from the scanner — obtaining "ate", a verb. There is no rule reducing "noun-phrase verb" so it pushes "ate" on the stack (Figure 1-c).
parser得到“ate”,它是个动词,因为这里没有规则来减少“名词短语 动词”,所以parser将ate压栈


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栈
5. The parser will obtain another token from the scanner — obtaining "worm", a noun. The parser can now reduce the "article noun" on the top of the stack to a noun-phrase and places the noun-phrase on the stack (Figure 1-e).
parser得到“worm”,它是个名词,因为这里有规则“冠词 名词”-->“名词短语”,所以parser将a出栈,和worm合并后压栈
6. The parser will obtain another token from the scanner — obtaining ".", a period. The parser can now reduce the "noun-phrase verb noun-phrase period" to a sentence and places this on the stack (Figure 1-f).

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值