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

前端 未结 5 722
小蘑菇
小蘑菇 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:15

    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.

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

    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.

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

    Check 3:The Grammar should not be ambiguous.

    These are some simple checks.
    

提交回复
热议问题