LR(0)解析器如何离开状态0? [英] How can an LR(0) parser ever leave state 0?

查看:119
本文介绍了LR(0)解析器如何离开状态0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我至少阅读过Wikipedia的解释,但我仍然对LR(0)解析器如何离开状态0感到困惑.

I've read Wikipedia's explanation at least a dozen times, but I'm still confused at how an LR(0) parser ever leaves state 0.

维基百科的示例及其解释说:

解析器从仅包含初始状态('0')的堆栈开始: [0]
解析器看到的输入字符串中的第一个符号是'1'.

The parser starts out with the stack containing just the initial state ('0'): [0]
The first symbol from the input string that the parser sees is '1'.

...但是这对我来说没有意义,因为看到输入符号将执行先行,但是根据定义,LR(0)解析器无法执行前瞻.

... but this doesn't make sense to me, because seeing the input symbol would be performing a lookahead, but an LR(0) parser, by definition, can't perform a lookahead.

当解析器处于状态0时,它尚未移位,因此它的堆栈上没有任何符号.
鉴于它是LR(0)解析器,因此也无法执行超前查找.

When the parser is at state 0, it hasn't shifted yet, so it doesn't have any symbols on its stack.
Given that it's an LR(0) parser, it can't perform a lookahead either.

那么它如何使用表格找出从状态0转变或减少到哪个状态?

So how does it use the table to figure out which state to shift or reduce into, from state 0?

推荐答案

已转移的符号不是超前的.它被消耗了.

The symbol shifted is not a lookahead. It's consumed.

LR(0)语法必须决定简化而无需参考下一个符号.一旦他们决定不减少,他们就隐含地决定转移. [1]

LR(0) grammars have to decide to reduce without consulting the next symbol. Once they've decided not to reduce, they've implicitly decided to shift. [1]

shift动作涉及读取一个符号,将其压入堆栈,并查询动作表以确定要转换到的状态.

The shift action involves reading a symbol, pushing it onto the stack, and consulting the action table to decide which state to transition to.

与LR(k> 0)语法的不同之处在于LR(k> 0)语法可以使用先行符号在移位和缩小之间做出决定,而LR(0)语法则不能.但是他们两个都在阅读移位的符号后决定进入哪个状态.

That's different from LR(k>0) grammars in that an LR(k>0) grammar can use the lookahead symbol to decide between shifting and reducing, while an LR(0) grammar cannot. But both of them get to decide which state to goto after they read the shifted symbol.

[注1] ...或接受,如果移位的符号是输入结束标记,但这只是状态的特例.

[Note 1] ...or to accept, if the shifted symbol is the end-of-input marker, but that's just a special case of a state.

这篇关于LR(0)解析器如何离开状态0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆