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

前端 未结 5 711
小蘑菇
小蘑菇 2020-12-04 06:07

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

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

X → Yz |

5条回答
  •  自闭症患者
    2020-12-04 06:22

    If you have no FIRST/FIRST conflicts and no FIRST/FOLLOW conflicts, your grammar is 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.

    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).

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

提交回复
热议问题