[英]Why is this grammar not LL(1)
I have the next grammar: 我有下一个语法:
C := (PZ)
P := X | C
X := iQ | eQ | rQ
Q := AX | ε
A := +
L := >
Z := LP | AP | ε
and i am using JFLAP to build an LL(1) parsing table, but at the moment that i type those rules, JFLAP throws me an error that says: the grammar is not LL(1).I have found where the mistake is, in the rule 'Q'. 我正在使用JFLAP来构建LL(1)解析表,但是在我键入这些规则的那一刻,JFLAP抛出了一个错误:语法不是LL(1)。我发现了错误所在,在规则“ Q”中。
The first set of Q is Q = {+,ε}, and the follow set of Q is Q = { ), + , >} and in the parsing table i am going to have two rules in table[Q,+] and that´s the mistake, but i dont know how to fix it because i need to have the rule Q -> ε Q的第一组为Q = {+,ε},Q的第二组为Q = {),+,>},在解析表中,我将在table [Q,+]和那是错误,但是我不知道如何解决,因为我需要规则Q->ε
The basic problem is that your grammar is ambiguous -- you have two nested repeating patterns from your rules for X
and Z
and both of them can match an i+i
fragment. 基本问题是语法不明确
X
和Z
规则中有两个嵌套的重复模式,并且它们都可以匹配i+i
片段。 So you need to decide how you want to resolve that ambiguity -- which way should a fragment like i+i
match: 因此,您需要确定如何解决这种歧义-像
i+i
这样的片段应该以哪种方式匹配:
PZ PZ
/ \ / \
X ε X AP
/ \ / \ / \
i Q i Q + X
/ \ / / \
A X ε i Q
/ / \ |
+ i Q ε
|
ε
The easiest fix is to make it always match the right example, which you can do by just getting rid of the X
/ Q
repeating pattern: 最简单的解决方法是使其始终与正确的示例匹配,您可以通过摆脱
X
/ Q
重复模式来做到这一点:
C := (PZ)
P := X | C
X := i | e | r
A := +
L := >
Z := LP | AP | ε
If you want to always match the left example, you need to disallow +
in the Z
pattern: 如果要始终匹配左例,则需要在
Z
模式中禁止+
:
C := (PZ)
P := X | C
X := iQ | eQ | rQ
Q := AX | ε
A := +
L := >
Z := LP | ε
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.