Parsing and Syntax directed translation - GeeksforGeeks

21 Jan, 2014
S->aB
B->bC
C->cd 
S -> aB ( Reduction 3 )
-> abC ( Reduction 2 )
-> abcd ( Reduction 1 )
S->abA
A-> cd
S -> abA ( Reduction 2 )
-> abcd ( Reduction 1 )
X -> c.X, c/d
   X -> .cX, c/d
   X -> .d, c/d
   X -> c.X, $
   X -> .cX, $
   X -> .d, $
Cannot be merged since look aheads are different.Can be merged but will result in S-R conflict.Can be merged but will result in R-R conflict.Cannot be merged since goto on c will lead to two different sets.
Cannot be merged since look aheads are different.
Can be merged but will result in S-R conflict.
Can be merged but will result in R-R conflict.
Cannot be merged since goto on c will lead to two different sets.

X -> c.X, c/d
X -> .cX, c/d
X -> .d, c/d
and
X -> c.X, $
X -> .cX, $
X -> .d, $ 

X -> c.X, c/d/$
X -> .cX, c/d/$
X -> .d, c/d/$
First(X) - It is the set of terminals that begin the 
            strings derivable from X.

Follow(X) - It is the set of terminals that can appear
            immediately to the right of X in some sentential
            form.

Now in the above question,

FIRST(S) = { a, b, epsilon}
FIRST(A) = FIRST(S) = { a, b, epsilon}
FIRST(B) = FIRST(S) = { a, b, epsilon}
FOLLOW (A) = { b , a }
FOLLOW (S) = { $ } U FOLLOW (A) = { b , a , $ }
FOLLOW (B) = FOLLOW (S) = { b ,a , $ }

epsilon corresponds to empty string.

Rule 1: if P --> Q is a production, for each terminal
        't' in FIRST(Q) add P-->Q to M [ P , t ]

Rule 2 : if epsilon is in FIRST(Q), add P --> Q to
         M [ P , f ] for each f in FOLLOW(P). 
First(aSa) = a
First(bS) = b
First(c) = c
All are mutually disjoint i.e no common terminal 
between them, the given grammar is LL(1).

As the grammar is LL(1) so it will also be LR(1) as LR parsers are
more powerful then LL(1) parsers. and all LL(1) grammar are also LR(1)
So option C is correct. 
Formally, grammar G is LL(1) if and only if 
   For all productions  A → α1 |  α2 | ... | αn, 
      First(αi) ∩ First(αj) =  ∅, 1 ≤ i,j  ≤ n,  i ≠ j.
   For every non-terminal A such that First(A) contains ε, 
      First(A) ∩ Follow(A) = ∅ 
Group 1                          Group 2
P. Regular expression        1. Syntax analysis
Q. Pushdown automata         2. Code generation
R. Dataflow analysis         3. Lexical analysis
S. Register allocation       4. Code optimization
I.  There exist parsing algorithms for some programming languages 
     whose complexities are less than O(n3).
II.  A programming language which allows recursion can be implemented 
    with static storage allocation.
III. No L-attributed definition can be evaluated in The framework 
     of bottom-up parsing.
IV. Code improving transformations can be performed at both source 
    language and intermediate code level.

For Ex :

Grammar is :

S-> aAcBe

A->Ab|b

B->d

Input string : abbcde

Derivation : ( Top-Down, Right Most Derivation)

S->aAcBe
->aAcde
->aAbcde
->abbcde 

: abbcde

->aAbcde ( using A-> b )

->aAcde ( using A-> Ab )

->aAcBe ( using B -> d )

->S ( using S-> aAcBe ) 

A-> a.bB , p
C-> d. , b

i.e. it is getting both shift and reduce at symbol b, 
hence a conflict. 
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
Learn C++ Programming Step by Step - A 20 Day Curriculum!
Must Do Coding Questions for Product Based Companies
GeeksforGeeks Jobathon - Are You Ready For This Hiring Challenge?
Types of Distributed System
Floyd’s Cycle Finding Algorithm
Formatted and Unformatted Input/Output functions in C with Examples
Free Online Resume Builder By GeeksforGeeks - Create Your Resume Now!
Difference Between NPDA and DPDA
How to Install Git in VS Code?