[英]LL(1) Parsing table with grammar
I'm trying to get LL(1) Parsing Table with grammar below,我正在尝试使用下面的语法获取 LL(1) 解析表,
S -> ( L ) | a
L -> L , S | S
I think it has indirect left recursion because of this,我认为因此它具有间接左递归,
L -> S
so I change it to,所以我把它改成
S -> ( L ) | a
L -> L , ( L ) | L , a | ( L ) | a
then like this,然后像这样
S -> ( L ) | a
L -> ( L ) L' | aL'
L' -> , ( L ) L' | ,aL' | epsilon
with this, I get FIRSTs and FOLLOWs有了这个,我得到了 FIRSTs 和 FOLLOWs
FIRST(S) = { (, a } FOLLOW(S) = { $ }
FIRST(L) = { (, a } FOLLOW(L) = { ) }
FIRST(L') = { , , epsilon} FOLLOW(L') = { ) }
But when I draw LL parsing table, It doesn't go to $.但是当我绘制LL解析表时,它不会转到$。
Did I make mistake or misunderstanding?我有没有弄错或误解?
(My parsing theory is rusty, so I may have made some mistakes here.) (我的解析理论是生疏的,所以我可能在这里犯了一些错误。)
L -> S
is part of indirect recursion, but not left recursion. L -> S
是间接递归的一部分,但不是左递归。 The production can only "expand" into L ->+ ( L )
or L ->+ a
, both of which start with a terminal.产生式只能“扩展”为
L ->+ ( L )
或L ->+ a
,两者都以终端开头。 The only left recursion here is in the production L -> L , S
.这里唯一的左递归是在产生式
L -> L , S
。 After removing this direct left recursion , I end up with the following grammar: 删除这个直接左递归后,我最终得到以下语法:
S -> ( L ) | a
L -> S L'
L' -> , S L' | ε
With this, the FIRST
and FOLLOW
sets you have computed are the same, except that FOLLOW(S)
is incomplete.这样,您计算的
FIRST
和FOLLOW
集是相同的,只是FOLLOW(S)
是不完整的。 Besides $
(because S
is the start state), FOLLOW(S)
must include all elements of FIRST(L')
(because L -> S L'
and L' -> , S L'
) and FOLLOW(L')
(because L' -> ε
).除了
$
(因为S
是开始状态), FOLLOW(S)
必须包括FIRST(L')
(因为L -> S L'
和L' -> , S L'
)和FOLLOW(L')
(因为L' -> ε
)。 I get FOLLOW(S) = { $, ,, ), ε }
.我得到
FOLLOW(S) = { $, ,, ), ε }
。
The parsing table I get is as follows:我得到的解析表如下:
| ( | a | , | ) | $ |
----+--------+--------+----------+-----+-----+
S | L ) | a | | | |
L | S L' | S L' | | | |
L' | | | , S L' | ε | |
I'm not sure what you mean by "it doesn't go to $
".我不确定你所说的“它不会去
$
”是什么意思。 All I can say is that the column $
is empty, because all productions of S
are non-empty and end in a terminal.我只能说
$
列是空的,因为S
所有产生式都是非空的并且以终端结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.