如何识别语法是否为LL(1)、LR(0)或SLR(1)?

时间:2021-09-13 02:39:05

How do you identify whether a grammar is LL(1), LR(0), or SLR(1)?

你如何判断一个文法是LL(1)、LR(0)或SLR(1)?

Can anyone please explain it using this example, or any other example?

有人能用这个例子解释一下吗,或者其他的例子?

X → Yz | a

X→Yz |

Y → bZ | ε

Y→bZ |ε

Z → ε

Z→ε

4 个解决方案

#1


84  

To check if a grammar is LL(1), one option is to construct the LL(1) parsing table and check for any conflicts. These conflicts can be

为了检查语法是否为LL(1),一个选项是构造LL(1)解析表并检查是否有冲突。这些冲突可以

  • FIRST/FIRST conflicts, where two different productions would have to be predicted for a nonterminal/terminal pair.
  • 第一个/第一个冲突,两个不同的产品必须被预测为一个非终端/终端对。
  • FIRST/FOLLOW conflicts, where two different productions are predicted, one representing that some production should be taken and expands out to a nonzero number of symbols, and one representing that a production should be used indicating that some nonterminal should be ultimately expanded out to the empty string.
  • 首先,在冲突中,预测两种不同的产品,一种表示某种产品应该被采用并扩展到非零数的符号,一种表示生产应该被使用,表明一些非终端应该最终扩展到空字符串。
  • FOLLOW/FOLLOW conflicts, where two productions indicating that a nonterminal should ultimately be expanded to the empty string conflict with one another.
  • 跟随/跟踪冲突,其中两个结果表明一个非终端应该最终扩展为空的字符串冲突。

Let's try this on your grammar by building the FIRST and FOLLOW sets for each of the nonterminals. Here, we get that

让我们通过构建第一个和遵循每个非终端的设置来尝试语法。在这里,我们得到了

FIRST(X) = {a, b, z}
FIRST(Y) = {b, epsilon}
FIRST(Z) = {epsilon} 

We also have that the FOLLOW sets are

我们也有下面的集合。

FOLLOW(X) = {$}
FOLLOW(Y) = {z}
FOLLOW(Z) = {z}

From this, we can build the following LL(1) parsing table:

由此,我们可以构建如下的LL(1)解析表:

    a    b    z   $
X   a    Yz   Yz  
Y        bZ   eps
Z             eps

Since we can build this parsing table with no conflicts, the grammar is LL(1).

因为我们可以构建这个没有冲突的解析表,所以语法是LL(1)。

To check if a grammar is LR(0) or SLR(1), we begin by building up all of the LR(0) configurating sets for the grammar. In this case, assuming that X is your start symbol, we get the following:

为了检查语法是否为LR(0)或SLR(1),我们首先构建语法的所有LR(0)配置集。在这种情况下,假设X是你的开始符号,我们得到如下:

(1)
X' -> .X
X -> .Yz
X -> .a
Y -> .
Y -> .bZ

(2)
X' -> X.

(3)
X -> Y.z

(4)
X -> Yz.

(5)
X -> a.

(6)
Y -> b.Z
Z -> .

(7)
Y -> bZ.

From this, we can see that the grammar is not LR(0) because there are shift/reduce conflicts in states (1) and (6). Specifically, because we have the reduce items Z → . and Y → ., we can't tell whether to reduce the empty string to these symbols or to shift some other symbol. More generally, no grammar with ε-productions is LR(0).

从这,我们可以看到,语法不是LR(0),因为有移位/归约冲突状态(1)和(6)。具体地说,因为我们有减少项目Z→。和Y→。,我们不能判断,减少空字符串这些符号或转向其他的象征。更一般的,没有语法ε-productions LR(0)。

However, this grammar might be SLR(1). To see this, we augment each reduction with the lookahead set for the particular nonterminals. This gives back this set of SLR(1) configurating sets:

但是,这个语法可能是SLR(1)。为了查看这一点,我们增加了每一项减少,为特定的非终端设置了lookahead设置。这就返回了这组SLR(1)配置集:

(1)
X' -> .X
X -> .Yz [$]
X -> .a  [$]
Y -> .   [z]
Y -> .bZ [z]

(2)
X' -> X.

(3)
X -> Y.z [$]

(4)
X -> Yz. [$]

(5)
X -> a.  [$]

(6)
Y -> b.Z [z]
Z -> .   [z]

(7)
Y -> bZ. [z]

Now, we don't have any more shift-reduce conflicts. The conflict in state (1) has been eliminated because we only reduce when the lookahead is z, which doesn't conflict with any of the other items. Similarly, the conflict in (6) is gone for the same reason.

现在,我们没有任何更多的转变——减少冲突。状态(1)的冲突已经被消除,因为我们只在前面是z的时候减少,这与其他任何项都不冲突。同样,(6)的冲突也出于同样的原因。

Hope this helps!

希望这可以帮助!

#2


7  

If you have no FIRST/FIRST conflicts and no FIRST/FOLLOW conflicts, your grammar is LL(1).

如果你没有第一次/第一次冲突,没有冲突,你的语法就会变成“LL(1)”。

An example of a FIRST/FIRST conflict:

第一次/第一次冲突的例子:

S -> Xb | Yc
X -> a 
Y -> a 

By seeing only the first input symbol a, you cannot know whether to apply the production S -> Xb or S -> Yc, because a is in the FIRST set of both X and Y.

通过只看到第一个输入符号a,你不知道是否应用生产S -> Xb或S -> Yc,因为a在X和Y的第一组中。

An example of a FIRST/FOLLOW conflict:

第一个/后续冲突的例子:

S -> AB 
A -> fe | epsilon 
B -> fg 

By seeing only the first input symbol f, you cannot decide whether to apply the production A -> fe or A -> epsilon, because f is in both the FIRST set of A and the FOLLOW set of A (A can be parsed as epsilon and B as f).

通过只看到第一个输入符号f,你不能决定是否应用生产A -> fe或A ->,因为f在A的第一组和A的后面集(A可以被解析为,B为f)。

Notice that if you have no epsilon-productions you cannot have a FIRST/FOLLOW conflict.

请注意,如果你没有爱扑塞隆的作品,你就不能有第一个/跟随的冲突。

#3


2  

Simple answer:A grammar is said to be an LL(1),if the associated LL(1) parsing table has atmost one production in each table entry.

简单的回答:如果关联的LL(1)解析表在每个表项中最多有一个生产,那么语法就是一个LL(1)。

Take the simple grammar A -->Aa|b.[A is non-terminal & a,b are terminals]
   then find the First and follow sets A.
    First{A}={b}.
    Follow{A}={$,a}.

    Parsing table for Our grammar.Terminals as columns and Nonterminal S as a row element.

        a            b                   $
    --------------------------------------------
 S  |               A-->a                      |
    |               A-->Aa.                    |
    -------------------------------------------- 

As [S,b] contains two Productions there is a confusion as to which rule to choose.So it is not LL(1).

As [S,b]包含两种产品,有一种混淆的规则可供选择。所以它不是LL(1)。

Some simple checks to see whether a grammar is LL(1) or not. Check 1: The Grammar should not be left Recursive. Example: E --> E+T. is not LL(1) because it is Left recursive. Check 2: The Grammar should be Left Factored.

一些简单的检查,看看语法是否为LL(1)或not。检查1:语法不应该是递归的。例子:E - - > E + T。不是i(1)因为它是左递归的。检查2:语法应该被分解。

Left factoring is required when two or more grammar rule choices share a common prefix string. Example: S-->A+int|A.

当两个或多个语法规则选择共享一个公共前缀字符串时,就需要左分解。例如:S - - > + int |。

Check 3:The Grammar should not be ambiguous.

检查3:语法不应该模棱两可。

These are some simple checks.

#4


1  

LL(1) grammar is Context free unambiguous grammar which can be parsed by LL(1) parsers.

LL(1)语法是上下文*明确的语法,可由LL(1)解析器解析。

In LL(1)

在我(1)

  • First L stands for scanning input from Left to Right. Second L stands for Left Most Derivation. 1 stands for using one input symbol at each step.
  • L代表从左到右的扫描输入。第二个L表示最左边的推导,1表示每个步骤中使用一个输入符号。

For Checking grammar is LL(1) you can draw predictive parsing table. And if you find any multiple entries in table then you can say grammar is not LL(1).

为了检查语法,您可以绘制预测性解析表。如果你在表格中发现了多个条目,那么你可以说语法不是LL(1)。

Their is also short cut to check if the grammar is LL(1) or not . Shortcut Technique

他们也会抄近路检查语法是否正确(1)。快捷的技术

#1


84  

To check if a grammar is LL(1), one option is to construct the LL(1) parsing table and check for any conflicts. These conflicts can be

为了检查语法是否为LL(1),一个选项是构造LL(1)解析表并检查是否有冲突。这些冲突可以

  • FIRST/FIRST conflicts, where two different productions would have to be predicted for a nonterminal/terminal pair.
  • 第一个/第一个冲突,两个不同的产品必须被预测为一个非终端/终端对。
  • FIRST/FOLLOW conflicts, where two different productions are predicted, one representing that some production should be taken and expands out to a nonzero number of symbols, and one representing that a production should be used indicating that some nonterminal should be ultimately expanded out to the empty string.
  • 首先,在冲突中,预测两种不同的产品,一种表示某种产品应该被采用并扩展到非零数的符号,一种表示生产应该被使用,表明一些非终端应该最终扩展到空字符串。
  • FOLLOW/FOLLOW conflicts, where two productions indicating that a nonterminal should ultimately be expanded to the empty string conflict with one another.
  • 跟随/跟踪冲突,其中两个结果表明一个非终端应该最终扩展为空的字符串冲突。

Let's try this on your grammar by building the FIRST and FOLLOW sets for each of the nonterminals. Here, we get that

让我们通过构建第一个和遵循每个非终端的设置来尝试语法。在这里,我们得到了

FIRST(X) = {a, b, z}
FIRST(Y) = {b, epsilon}
FIRST(Z) = {epsilon} 

We also have that the FOLLOW sets are

我们也有下面的集合。

FOLLOW(X) = {$}
FOLLOW(Y) = {z}
FOLLOW(Z) = {z}

From this, we can build the following LL(1) parsing table:

由此,我们可以构建如下的LL(1)解析表:

    a    b    z   $
X   a    Yz   Yz  
Y        bZ   eps
Z             eps

Since we can build this parsing table with no conflicts, the grammar is LL(1).

因为我们可以构建这个没有冲突的解析表,所以语法是LL(1)。

To check if a grammar is LR(0) or SLR(1), we begin by building up all of the LR(0) configurating sets for the grammar. In this case, assuming that X is your start symbol, we get the following:

为了检查语法是否为LR(0)或SLR(1),我们首先构建语法的所有LR(0)配置集。在这种情况下,假设X是你的开始符号,我们得到如下:

(1)
X' -> .X
X -> .Yz
X -> .a
Y -> .
Y -> .bZ

(2)
X' -> X.

(3)
X -> Y.z

(4)
X -> Yz.

(5)
X -> a.

(6)
Y -> b.Z
Z -> .

(7)
Y -> bZ.

From this, we can see that the grammar is not LR(0) because there are shift/reduce conflicts in states (1) and (6). Specifically, because we have the reduce items Z → . and Y → ., we can't tell whether to reduce the empty string to these symbols or to shift some other symbol. More generally, no grammar with ε-productions is LR(0).

从这,我们可以看到,语法不是LR(0),因为有移位/归约冲突状态(1)和(6)。具体地说,因为我们有减少项目Z→。和Y→。,我们不能判断,减少空字符串这些符号或转向其他的象征。更一般的,没有语法ε-productions LR(0)。

However, this grammar might be SLR(1). To see this, we augment each reduction with the lookahead set for the particular nonterminals. This gives back this set of SLR(1) configurating sets:

但是,这个语法可能是SLR(1)。为了查看这一点,我们增加了每一项减少,为特定的非终端设置了lookahead设置。这就返回了这组SLR(1)配置集:

(1)
X' -> .X
X -> .Yz [$]
X -> .a  [$]
Y -> .   [z]
Y -> .bZ [z]

(2)
X' -> X.

(3)
X -> Y.z [$]

(4)
X -> Yz. [$]

(5)
X -> a.  [$]

(6)
Y -> b.Z [z]
Z -> .   [z]

(7)
Y -> bZ. [z]

Now, we don't have any more shift-reduce conflicts. The conflict in state (1) has been eliminated because we only reduce when the lookahead is z, which doesn't conflict with any of the other items. Similarly, the conflict in (6) is gone for the same reason.

现在,我们没有任何更多的转变——减少冲突。状态(1)的冲突已经被消除,因为我们只在前面是z的时候减少,这与其他任何项都不冲突。同样,(6)的冲突也出于同样的原因。

Hope this helps!

希望这可以帮助!

#2


7  

If you have no FIRST/FIRST conflicts and no FIRST/FOLLOW conflicts, your grammar is LL(1).

如果你没有第一次/第一次冲突,没有冲突,你的语法就会变成“LL(1)”。

An example of a FIRST/FIRST conflict:

第一次/第一次冲突的例子:

S -> Xb | Yc
X -> a 
Y -> a 

By seeing only the first input symbol a, you cannot know whether to apply the production S -> Xb or S -> Yc, because a is in the FIRST set of both X and Y.

通过只看到第一个输入符号a,你不知道是否应用生产S -> Xb或S -> Yc,因为a在X和Y的第一组中。

An example of a FIRST/FOLLOW conflict:

第一个/后续冲突的例子:

S -> AB 
A -> fe | epsilon 
B -> fg 

By seeing only the first input symbol f, you cannot decide whether to apply the production A -> fe or A -> epsilon, because f is in both the FIRST set of A and the FOLLOW set of A (A can be parsed as epsilon and B as f).

通过只看到第一个输入符号f,你不能决定是否应用生产A -> fe或A ->,因为f在A的第一组和A的后面集(A可以被解析为,B为f)。

Notice that if you have no epsilon-productions you cannot have a FIRST/FOLLOW conflict.

请注意,如果你没有爱扑塞隆的作品,你就不能有第一个/跟随的冲突。

#3


2  

Simple answer:A grammar is said to be an LL(1),if the associated LL(1) parsing table has atmost one production in each table entry.

简单的回答:如果关联的LL(1)解析表在每个表项中最多有一个生产,那么语法就是一个LL(1)。

Take the simple grammar A -->Aa|b.[A is non-terminal & a,b are terminals]
   then find the First and follow sets A.
    First{A}={b}.
    Follow{A}={$,a}.

    Parsing table for Our grammar.Terminals as columns and Nonterminal S as a row element.

        a            b                   $
    --------------------------------------------
 S  |               A-->a                      |
    |               A-->Aa.                    |
    -------------------------------------------- 

As [S,b] contains two Productions there is a confusion as to which rule to choose.So it is not LL(1).

As [S,b]包含两种产品,有一种混淆的规则可供选择。所以它不是LL(1)。

Some simple checks to see whether a grammar is LL(1) or not. Check 1: The Grammar should not be left Recursive. Example: E --> E+T. is not LL(1) because it is Left recursive. Check 2: The Grammar should be Left Factored.

一些简单的检查,看看语法是否为LL(1)或not。检查1:语法不应该是递归的。例子:E - - > E + T。不是i(1)因为它是左递归的。检查2:语法应该被分解。

Left factoring is required when two or more grammar rule choices share a common prefix string. Example: S-->A+int|A.

当两个或多个语法规则选择共享一个公共前缀字符串时,就需要左分解。例如:S - - > + int |。

Check 3:The Grammar should not be ambiguous.

检查3:语法不应该模棱两可。

These are some simple checks.

#4


1  

LL(1) grammar is Context free unambiguous grammar which can be parsed by LL(1) parsers.

LL(1)语法是上下文*明确的语法,可由LL(1)解析器解析。

In LL(1)

在我(1)

  • First L stands for scanning input from Left to Right. Second L stands for Left Most Derivation. 1 stands for using one input symbol at each step.
  • L代表从左到右的扫描输入。第二个L表示最左边的推导,1表示每个步骤中使用一个输入符号。

For Checking grammar is LL(1) you can draw predictive parsing table. And if you find any multiple entries in table then you can say grammar is not LL(1).

为了检查语法,您可以绘制预测性解析表。如果你在表格中发现了多个条目,那么你可以说语法不是LL(1)。

Their is also short cut to check if the grammar is LL(1) or not . Shortcut Technique

他们也会抄近路检查语法是否正确(1)。快捷的技术