GATE | GATE-CS-2006 | Question 58

Consider the following grammar:

S → FR
R → S | ε
F → id

In the predictive parser table, M, of the grammar the entries M[S, id] and M[R, $] respectively.
(A) {S → FR} and {R → ε }
(B) {S → FR} and { }
(C) {S → FR} and {R → *S}
(D) {F → id} and {R → ε}


Answer: (A)

Explanation: Here representing the parsing table as M[ X , Y ], where X represents rows( Non terminals) and Y represents columns(terminals).

Here are the rules to fill the parsing table.

For each distinct production rule A->α, of the grammar, we need to apply the given rules:

Rule 1: if A –> α is a production, for each terminal ‘a’ in FIRST(α), add A–>α to M[ A , a ]

Rule 2 : if ‘ ε ‘ is in FIRST(α), add A –> α to M [ A , b ] for each ‘b’ in FOLLOW(A).

As Entries have been asked corresponding to Non-Terminal S and R, hence we only need to consider their productions to get the answer.

For S → FR, according to rule 1, this production rule should be placed at the entry M[ S, FIRST(FR) ], and from the given grammar, FIRST(FR) ={id}, hence S->FR is placed in the parsing table at entry M[S , id].

Similarly,

For R → S, this production rule should be placed at entry M[ R, FIRST(S) ], and as FIRST(S) = FIRST(F) = {id} hence, R->S is placed at entry M[R,id]

and

For R->ε, as FIRST(ε) = {ε}, hence rule 2 should be applied, therefore, this production rule should be placed in the parsing table at entry M[R,FOLLOW(R)], and FOLLOW(R) = FOLLOW(S) = {$}, hence R->ε is placed at entry M[ R, $ ].

Therefore Answer is option A.

Visit the Following links to Learn how to find First and Follow sets.

http://quiz.geeksforgeeks.org/compiler-design-first-in-syntax-analysis/
http://quiz.geeksforgeeks.org/compiler-design-follow-set-in-syntax-analysis/

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.