Question 1
What is the maximum number of reduce moves that can be taken by a bottom-up parser for a grammar with no epsilon- and unit-production (i.e., of type A -> є and A -> a) to parse a string with n tokens?
A
n/2
B
n-1
C
2n-1
D
2n
GATE CS 2013    Parsing and Syntax directed translation    
Discuss it


Question 1 Explanation: 
Given in the question, a grammar with no epsilon- and unit-production (i.e., of type A -> є and A -> a) . Suppose the string is abcd. ( n = 4 ) We can write the grammar which accepts this string as follows:

S->aB
B->bC
C->cd 
The Right Most Derivation for the above is:

S -> aB ( Reduction 3 )
-> abC ( Reduction 2 )
-> abcd ( Reduction 1 )
We can see here that no production is for unit or epsilon. Hence 3 reductions here. We can get less number of reductions with some other grammar which also does't produce unit or epsilon productions,

S->abA
A-> cd
The Right Most Derivation for the above as:

S -> abA ( Reduction 2 )
-> abcd ( Reduction 1 )
Hence 2 reductions. But we are interested in knowing the maximum number of reductions which comes from the 1st grammar. Hence total 3 reductions as maximum, which is ( n - 1) as n = 4 here. Thus, Option B.
Question 2
Consider the following two sets of LR(1) items of an LR(1) grammar.
   X -> c.X, c/d
   X -> .cX, c/d
   X -> .d, c/d
   X -> c.X, $
   X -> .cX, $
   X -> .d, $
Which of the following statements related to merging of the two sets in the corresponding LALR parser is/are FALSE?
1. Cannot be merged since look aheads are different.
2. Can be merged but will result in S-R conflict.
3. Can be merged but will result in R-R conflict.
4. Cannot be merged since goto on c will lead to two different sets.
A
1 only
B
2 only
C
1 and 4 only
D
1, 2, 3, and 4
GATE CS 2013    Parsing and Syntax directed translation    
Discuss it


Question 2 Explanation: 
The given two LR(1) set of items are :

X -> c.X, c/d
X -> .cX, c/d
X -> .d, c/d
and
X -> c.X, $
X -> .cX, $
X -> .d, $ 
The symbols/terminals after the comma are Look-Ahead symbols. These are the sets of LR(1) ( LR(1) is also called CLR(1) ) items. The LALR(1) parser combines those set of LR(1) items which are identical with respect to their 1st component but different with respect to their 2nd component. In a production rule of a LR(1) set of items, ( A -> B , c ) , A->B is the 1st component , and the Look-Ahead set of symbols, which is c here, is the second component. Now we can see that in the sets given, 1st component of the corresponding production rule is identical in both sets, and they only differ in 2nd component ( i.e. their look-ahead symbols) hence we can combine these sets to make a a single set which would be :

X -> c.X, c/d/$
X -> .cX, c/d/$
X -> .d, c/d/$
This is done to reduce the total number of parser states. Now we can check the statements given. Statement 1 : The statement is false, as merging has been done because 2nd components i.e. look-ahead were different. Statement 2 : In the merged set, we can't see any Shift-Reduce conflict ( because no reduction even possible, reduction would be possible when a production of form P -> q. is present) Statement 3 : In the merged set, we can't see any Reduce-Reduce conflict ( same reason as above, no reduction even possible, so no chances of R-R conflict ) Statement 4: This statement is also wrong, because goto is carried on Non-Terminals symbols, not on terminal symbols, and c is a terminal symbol. Thus, all statements are wrong, hence option D.
Question 3
For the grammar below, a partial LL(1) parsing table is also presented along with the grammar. Entries that need to be filled are indicated as E1, E2, and E3. \epsilon is the empty string, $ indicates end of input, and, | separates alternate right hand sides of productions. CSE_2012_51 CSE_GATE_20122
A
A
B
B
C
C
D
D
GATE CS 2012    Parsing and Syntax directed translation    
Discuss it


Question 3 Explanation: 
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.
Question 4
Consider the date same as above question. The appropriate entries for E1, E2, and E3 are CSE_GATE_20123
A
A
B
B
C
C
D
D
GATE CS 2012    Parsing and Syntax directed translation    
Discuss it


Question 4 Explanation: 
As we need to find entries E1, E2 and E3 which are against Non terminals S and B, so we will deal with only those productions which have S and B on LHS. Here representing the parsing table as M[ X , Y ], where X represents rows( Non terminals) and Y represents columns(terminals).

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). 
For the production rule S--> aAbB, it will be added to parsing table at the position M[ S , FIRST( aAbB ) ], Now FIRST(aAbB) = {a}, hence add S--> aAbB to M[ S , a ] which is E1. For the production rule S--> bAaB, it will be added to parsing table at the position M[ S , FIRST( bAaB ) ], Now FIRST(bAaB) = {b}, hence add S--> bAaB to M[ S , b ] which is E2 For the production rule S--> epsilon , it will be added to parsing table at the position M[ S , FOLLOW(S) ], Now FOLLOW(S) = { a , b , $ }, hence add S --> epsilon to M[ S , a ] and M[ S , b ] which are again E1 and E2 respectively. For the production rule B --> S, it will be added to parsing table at the position M[ B , FIRST( S ) ], Now FIRST(S) also contains epsilon, hence add B --> S to M[ S , FOLLOW(B) ], and FOLLOW(B) contains {$}, i.e. M[ S , $ ] which is E3. epsilon corresponds to empty string.
Question 5
The grammar S → aSa | bS | c is
A
LL(1) but not LR(1)
B
LR(1)but not LR(1)
C
Both LL(1)and LR(1)
D
Neither LL(1)nor LR(1)
GATE CS 2010    Parsing and Syntax directed translation    
Discuss it


Question 5 Explanation: 
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. 
Below are more details. A grammar is LL(1) if it is possible to choose the next production by looking at only the next token in the input string.
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) = ∅ 
Source: https://s3-ap-southeast-1.amazonaws.com/erbuc/files/4147_870334aa-9922-4f78-9c2c-713e7a7f0d53.pdf
Question 6
Match all items in Group 1 with correct options from those given in Group 2.
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
 
A
P-4. Q-1, R-2, S-3
B
P-3, Q-1, R-4, S-2
C
P-3, Q-4, R-1, S-2
D
P-2, Q-1, R-4, S-3
GATE-CS-2009    Parsing and Syntax directed translation    
Discuss it


Question 6 Explanation: 
Regular expressions are used in syntax analysis. Pushdown automata is related to context free grammar which is related to syntax analysis. Dataflow analysis is done in code optimization. Register allocation is done in code generation.
Question 7
Which of the following statements are TRUE?
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.
A
I and II
B
I and IV
C
III and IV
D
I, III and IV
GATE-CS-2009    Parsing and Syntax directed translation    
Discuss it


Question 7 Explanation: 
II is false, in recursion, compiler cannot determine the space needed for recursive calls. III is false.  See http://www.cs.sunysb.edu/~cse304/Fall09/Lectures/attributes-handout.pdf
Question 8
Which of the following describes a handle (as applicable to LR-parsing) appropriately?
A
It is the position in a sentential form where the next shift or reduce operation will occur
B
It is non-terminal whose production will be used for reduction in the next step
C
It is a production that may be used for reduction in a future step along with a position in the sentential form where the next shift or reduce operation will occur
D
It is the production p that will be used for reduction in the next step along with a position in the sentential form where the right hand side of the production may be found
Parsing and Syntax directed translation    GATE CS 2008    
Discuss it


Question 8 Explanation: 
Let's first understand the terminology used here. LR Parsing - Here 'L' stands for Left to Right screening of input string, and 'R' stands for Right Most Derivation in Reverse ( because it is about bottom-up parsing). Sentential Form - Suppose For a given Context Free Grammar G, we have a start symbol S, then to define Language generated by Grammar G, i.e. L(G), we start the derivation starting from S using the production rules of the grammar. After one complete derivation we get a string w which consists of only terminal symbols, i.e. w belongs to L(G). Then we can say that w is a sentence of the Grammar G. Now, while the derivation process, if it gets some form q, where q may contain some Non-terminal then we say that q is a sentential form of the Grammar G. Even the start symbol S is also the sentential form of the Grammar G ( because it also contains Non-terminal S).

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 
Here { abbcde } is the sentence of the Grammar( because it contains only terminal symbols), and { S, aAcBe, aAcde, aAbcde } are the sentential forms of the G ( because these forms contain non-terminals during the derivation process ) Now, let's look at the question. The question is related to LR parsing which is a bottom-up parsing. Let's take the same grammar above, as it is a bottom up parsing we need to start from the string "abbcde" and try to get S using production rules.

: abbcde

->aAbcde ( using A-> b )

->aAcde ( using A-> Ab )

->aAcBe ( using B -> d )

->S ( using S-> aAcBe ) 
The above process is called reduction. The RHS of the Production which is being replaced by their LHS is called Handle, So , { b, Ab, d, aAcBe} are handles, and replacing it with its LHS is called Handle-Pruning. Hence option D suits best.
Question 9
An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if
A
the SLR(1) parser for G has S-R conflicts
B
the LR(1) parser for G has S-R conflicts
C
the LR(0) parser for G has S-R conflicts
D
the LALR(1) parser for G has reduce-reduce conflicts
Parsing and Syntax directed translation    GATE CS 2008    
Discuss it


Question 9 Explanation: 
Both LALR(1) and LR(1) parser uses LR(1) set of items to form their parsing tables. And LALR(1) states can be find by merging LR(1) states of LR(1) parser that have the same set of first components of their items. i.e. if LR(1) parser has 2 states I and J with items A->a.bP,x and A->a.bP,y respectively, where x and y are look ahead symbols, then as these items are same with respect to their first component, they can be merged together and form one single state, let's say K. Here we have to take union of look ahead symbols. After merging, State K will have one single item as A->a.bP,x,y . This way LALR(1) states are formed ( i.e. after merging the states of LR(1) ). Now, S-R conflict in LR(1) items can be there whenever a state has items of the form :

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

i.e. it is getting both shift and reduce at symbol b, 
hence a conflict. 
Now, as LALR(1) have items similar to LR(1) in terms of their first component, shift-reduce form will only take place if it is already there in LR(1) states. If there is no S-R conflict in LR(1) state it will never be reflected in the LALR(1) state obtained by combining LR(1) states. Note: But this process of merging may introduce R-R conflict, and then the Grammar won't be LALR(1).
Question 10
Which one of the following is a top-down parser?
A
Recursive descent parser.
B
Operator precedence parser.
C
An LR(k) parser.
D
An LALR(k) parser
Parsing and Syntax directed translation    GATE-CS-2007    
Discuss it


Question 10 Explanation: 
Recursive Descent parsing is LL(1) parsing which is top down parsing.
Question 11
Consider the grammar with non-terminals N = {S,C,S1 },terminals T={a,b,i,t,e}, with S as the start symbol, and the following set of rules:
S --> iCtSS1|a
S1 --> eS|ϵ
C --> b
The grammar is NOT LL(1) because:
A
it is left recursive
B
it is right recursive
C
it is ambiguous
D
It is not context-free.
Parsing and Syntax directed translation    GATE-CS-2007    
Discuss it


Question 11 Explanation: 

A  LL(1) grammar doesn't give to multiple entries in a single cell of its parsing table. It has only single entry in a single cell, hence it should be unambiguous. 

Option A is wrong. Grammar is not left recursive. For a grammar to be left recursive a production should be of form A->Ab, where A is a single Non-Terminal and b is any string of grammar symbols.

Option B is wrong. Because a right recursive grammar has nothing to do with LL(1).

Option D is wrong. Because the given grammar is clearly a Context Free Grammar. A grammar is CFG if it has productions of the form A->(V∪ T)* , where A is a single non-terminal and V is a set of Non-terminals and T is a set of Terminals. 

Hence Option C should be the correct one. i.e. the grammar is ambiguous.

But let's see how the grammar is ambiguous.

If the grammar is ambiguous then it should give multiple entry in a cell while making its parsing table. And Parse table is made with the aid of two functions : FIRST and FOLLOW.

A parsing table of a grammar will not have multiple entries in a cell( i.e. will be a LL(1) grammar) if and only if the following conditions hold for each production of the form A->α|β 1) FIRST(α)  ∩ FIRST(β) =   Φ 2) if FIRST(α) contains ' ε ' then FIRST(α) ∩ FOLLOW (A) =  Φ and vice-versa.

Now,

  • For the production , S->iCtSS1|a, rule 1 is satisfied, because FIRST(iCtSS1) ∩ FIRST(a) = {i} ∩ {a} = Φ
  • For the production, S1->eS|ε, rule 1 is satisfied, as FIRST(eS) ∩ FIRST(ε) = {e} ∩ {ε} = Φ   .  But here due to 'ε' in FIRST, we have to check for rule 2. FIRST(eS)  ∩ FOLLOW(S1) = {e} ∩ {e, $} ≠ Φ .  Hence rule 2 fails in this production rule. Therefore there will be multiple entries in the parsing table, hence the grammar is ambiguous and not LL(1).
Please refer these link to learn how to find FIRST and FOLLOW:  http://geeksquiz.com/compiler-design-first-in-syntax-analysis/ http://geeksquiz.com/compiler-design-follow-set-in-syntax-analysis/
Question 12
Consider the following two statements:
P: Every regular grammar is LL(1)
Q: Every regular set has a LR(1) grammar
Which of the following is TRUE?
A
Both P and Q are true
B
P is true and Q is false
C
P is false and Q is true
D
Both P and Q are false
Parsing and Syntax directed translation    GATE-CS-2007    
Discuss it


Question 12 Explanation: 
A regular grammar can also be ambiguous also
For example, consider the following grammar,                             
S → aA/a
A → aA/ε
In above grammar, string 'a' has two leftmost
derivations. 
(1)   S → aA                      (2)   S → a
      S->a (using A->ε)
And LL(1) parses only unambiguous grammar,
so statement P is False.
Statement Q is true is for every regular set, we can have a regular
grammar which is unambiguous so it can be parse by LR parser. 

So option C is correct choice 
Question 13
Consider the following grammar.
S -> S * E
S -> E
E -> F + E
E -> F
F -> id
Consider the following LR(0) items corresponding to the grammar above.
(i) S -> S * .E
(ii) E -> F. + E
(iii) E -> F + .E 
Given the items above, which two of them will appear in the same set in the canonical sets-of-items for the grammar?
A
(i) and (ii)
B
(ii) and (iii)
C
(i) and (iii)
D
None of the above
Parsing and Syntax directed translation    GATE-CS-2006    
Discuss it


Question 13 Explanation: 
Let's make the LR(0) set of items. First we need to augment the grammar with the production rule S' -> .S , then we need to find closure of items in a set to complete a set. Below are the LR(0) sets of items. LR(0) items
Question 14
A canonical set of items is given below
S --> L. > R
Q --> R.
On input symbol < the set has
A
a shift-reduce conflict and a reduce-reduce conflict.
B
a shift-reduce conflict but not a reduce-reduce conflict.
C
a reduce-reduce conflict but not a shift-reduce conflict.
D
neither a shift-reduce nor a reduce-reduce conflict.
Parsing and Syntax directed translation    GATE-CS-2014-(Set-1)    
Discuss it


Question 14 Explanation: 
The question is asked with respect to the symbol  ' < '  which is not present in the given canonical set of items. Hence it is neither a shift-reduce conflict nor a reduce-reduce conflict on symbol '<'. Hence D is the correct option. But if the question would have asked with respect to the symbol  ' > '  then it would have been a shift-reduce conflict.
Question 15
Consider the grammar defined by the following production rules, with two operators ∗ and +
    S --> T * P 
    T --> U | T * U
    P --> Q + P | Q
    Q --> Id
    U --> Id
Which one of the following is TRUE?
A
+ is left associative, while ∗ is right associative
B
+ is right associative, while ∗ is left associative
C
Both + and ∗ are right associative
D
Both + and ∗ are left associative
Parsing and Syntax directed translation    GATE-CS-2014-(Set-2)    
Discuss it


Question 15 Explanation: 
From the grammar we can find out associative by looking at grammar.
Let us consider the 2nd production
T -> T * U
T is generating T*U recursively (left recursive) so * is 
left associative.

Similarly
P -> Q + P
Right recursion so + is right associative.
So option B is correct. 
NOTE: Above is the shortcut trick that can be observed after drawing few parse trees. One can also find out correct answer by drawing the parse tree.
Question 16
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 → ε}
Parsing and Syntax directed translation    GATE-CS-2006    
Discuss it


Question 16 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://geeksquiz.com/compiler-design-first-in-syntax-analysis/ http://geeksquiz.com/compiler-design-follow-set-in-syntax-analysis/
Question 17
Consider the following translation scheme. S → ER R → *E{print("*");}R | ε E → F + E {print("+");} | F F → (S) | id {print(id.value);} Here id is a token that represents an integer and id.value represents the corresponding integer value. For an input '2 * 3 + 4', this translation scheme prints
A
2 * 3 + 4
B
2 * +3 4
C
2 3 * 4 +
D
2 3 4+*
Parsing and Syntax directed translation    GATE-CS-2006    
Discuss it


Question 17 Explanation: 
Background Required to solve the question - Syntax Directed Translation and Parse Tree Construction.
Explanation : We are given L-Attributed Syntax Directed Translation as
semantic actions like printf statements are inserted anywhere on the 
RHS of production (R → *E{print(“*”);}R). After constructing the parse tree 
as shown below from the given grammar, we will follow depth first order left 
to right evaluation in order to generate the final output.
Parse Tree: parse-tree
Just follow the arrows in the picture (This is actually Depth first 
left to right evaluation ) and the moment we take exit from any child 
which is printf statement in this question, we print that symbol which 
can be a integer value or ‘*’ or ‘+’.
Evaluation : syntax-directed-translation This explanation has been contributed by Pranjul Ahuja.
Question 18
The grammar A → AA | (A) | ε is not suitable for predictive-parsing because the grammar is
A
ambiguous
B
left-recursive
C
right-recursive
D
an operator-grammar
Parsing and Syntax directed translation    GATE-CS-2005    
Discuss it


Question 18 Explanation: 
Answer (B) is correct because grammar is left recursive, hence the parser may fall into a loop. Answer A is not correct because ambiguity can occur from both left or right recursion. Refer https://lambda.uta.edu/cse5317/notes/node13.html
Question 19
Consider the grammar
E → E + n | E × n | n 
For a sentence n + n × n, the handles in the right-sentential form of the reduction are
A
n, E + n and E + n × n
B
n, E + n and E + E × n
C
n, n + n and n + n × n
D
n, E + n and E × n
Parsing and Syntax directed translation    GATE-CS-2005    
Discuss it


Question 19 Explanation: 
E → E + n       {Applying E → E + n } 
  → E + E * n   {Applying E → E * n } 
  → E + n * n   {Applying E → n } 
  → n + n * n   {Applying E → n } 
Question 20
Consider the grammar
S → (S) | a
Let the number of states in SLR(1), LR(1) and LALR(1) parsers for the grammar be n1, n2 and n3 respectively. The following relationship holds good
A
n1 < n2 < n3
B
n1 = n3 < n2
C
n1 = n2 = n3
D
n1 ≥ n3 ≥ n2
Parsing and Syntax directed translation    GATE-CS-2005    
Discuss it


Question 20 Explanation: 
LALR(1) is formed by merging states of LR(1) ( also called CLR(1)), hence no of states in LALR(1) is less than no of states in LR(1), therefore n3 < n2. And SLR(1) and LALR(1) have same no of states, i.e ( n1 = n3).
Hence n1 = n3 < n2
Question 21
Consider the following expression grammar. The seman­tic rules for expression calculation are stated next to each grammar production.
 E → number 	 E.val = number. val
    | E '+' E 	 E(1).val = E(2).val + E(3).val
    | E '×' E	 E(1).val = E(2).val × E(3).val
The above grammar and the semantic rules are fed to a yacc tool (which is an LALR (1) parser generator) for parsing and evaluating arithmetic expressions. Which one of the following is true about the action of yacc for the given grammar?
A
It detects recursion and eliminates recursion
B
It detects reduce-reduce conflict, and resolves
C
It detects shift-reduce conflict, and resolves the conflict in favor of a shift over a reduce action
D
It detects shift-reduce conflict, and resolves the conflict in favor of a reduce over a shift action
Parsing and Syntax directed translation    GATE-CS-2005    
Discuss it


Question 21 Explanation: 
  Background yacc conflict resolution is done using following rules: shift is preferred over reduce while shift/reduce conflict. first reduce is preferred over others while reduce/reduce conflict.   You can answer to this question straightforward by constructing LALR(1) parse table, though its a time taking process. To answer it faster, one can see intuitively that this grammar will have a shift-reduce conflict for sure. In that case, given this is a single choice question, (C) option will be the right answer. Fool-proof explanation would be to generate LALR(1) parse table, which is a lengthy process. Once we have the parse table with us, we can clearly see that i. reduce/reduce conflict will not arise in the above given grammar ii. shift/reduce conflict will be resolved by giving preference to shift, hence making the expression calculator right associative. According to the above conclusions, only correct option seems to be (C). Referance: http://dinosaur.compilertools.net/yacc/ This solution is contributed by Vineet Purswani.
Question 22
Consider the following expression grammar. The seman­tic rules for expression calculation are stated next to each grammar production.
 E → number 	 E.val = number. val
    | E '+' E 	 E(1).val = E(2).val + E(3).val
    | E '×' E	 E(1).val = E(2).val × E(3).val 
Assume the conflicts in Part (a) of this question are resolved and an LALR(1) parser is generated for parsing arithmetic expressions as per the given grammar. Consider an expression 3 × 2 + 1. What precedence and associativity properties does the generated parser realize?
A
Equal precedence and left associativity; ex­pression is evaluated to 7
B
Equal precedence and right associativity; ex­pression is evaluated to 9
C
Precedence of '×' is higher than that of '+', and both operators are left associative; expression is evaluated to 7
D
Precedence of '+' is higher than that of '×', and both operators are left associative; expression is evaluated to 9
Parsing and Syntax directed translation    GATE-CS-2005    
Discuss it


Question 22 Explanation: 
Answer is B as the productions belong to the same non-terminal and since YACC resolves by shift over reduce, the associativity will be right associative.
Question 23
Which of the following grammar rules violate the requirements of an operator grammar ? P, Q, R are nonterminals, and r, s, t are terminals.
1.    P → Q R                    
2.    P → Q s R
3.    P → ε       
4.    P → Q t R r 
A
1 only
B
1 and 3 only
C
2 and 3 only
D
3 and 4 only
Parsing and Syntax directed translation    GATE-CS-2004    
Discuss it


Question 23 Explanation: 
Question 24
Consider the grammar with the following translation rules and E as the start symbol.
E → E1 # T { E.value = E1.value * T.value }
         | T{ E.value = T.value }
T → T1 & F { T.value = T1.value + F.value }
          | F{ T.value = F.value }
F → num { F.value = num.value } 
Compute E.value for the root of the parse tree for the expression: 2 # 3 & 5 # 6 & 4.
A
200
B
180
C
160
D
40
Parsing and Syntax directed translation    GATE-CS-2004    
Discuss it


Question 24 Explanation: 
Question 25
Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?
A
Removing left recursion alone
B
Factoring the grammar alone
C
Removing left recursion and factoring the grammar
D
None of these
Parsing and Syntax directed translation    GATE-CS-2003    
Discuss it


Question 25 Explanation: 
Removing left recursion and factoring the grammar do not suffice to convert an arbitrary CFG to LL(1) grammar. http://pages.cpsc.ucalgary.ca/~robin/class/411/LL1.3.html
Question 26
Assume that the SLR parser for a grammar G has n1 states and the LALR parser for G has n2 states. The relationship between n1 and n2 is:
A
n1 is necessarily less than n2
B
n1 is necessarily equal to n2
C
n1 is necessarily greater than n2
D
none of these
Parsing and Syntax directed translation    GATE-CS-2003    
Discuss it


Question 27
In a bottom-up evaluation of a syntax directed definition, inherited attributes can
A
always be evaluated
B
be evaluated only if the definition is L-­attributed
C
be evaluated only if the definition has synthesized attributes
D
never be evaluated
Parsing and Syntax directed translation    GATE-CS-2003    
Discuss it


Question 27 Explanation: 
A Syntax Directed Definition (SDD) is called S Attributed if it has only synthesized attributes. L-Attributed Definitions contain both synthesized and inherited attributes but do not need to build a dependency graph to evaluate them. References: http://cse.iitkgp.ac.in/~bivasm/notes/SDD.pdf
Question 28
Consider the grammar shown below S → i E t S S' | a S' → e S | ε E → b In the predictive parse table. M, of this grammar, the entries M[S', e] and M[S', $] respectively are
A
{S' → e S} and {S' → e}
B
{S' → e S} and {}
C
{S' → ε} and {S' → ε}
D
{S' → e S, S'→ ε} and {S' → ε}
Parsing and Syntax directed translation    GATE-CS-2003    
Discuss it


Question 28 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', hence we only need to consider its productions to get the answer. For S' → eS, according to rule 1, this production rule should be placed at the entry M[ S', FIRST(eS) ], and from the given grammar, FIRST(eS) ={e}, hence S'->eS is placed in the parsing table at entry M[S' , e]. Similarly, For S'->ε, as FIRST(ε) = {ε}, hence rule 2 should be applied, therefore, this production rule should be placed in the parsing table at entry M[S',FOLLOW(S')], and FOLLOW(S') = FOLLOW(S) = { e, $ }, hence R->ε is placed at entry M[ S', e ] and M[ S' , $ ]. Therefore Answer is option D. Visit the Following links to Learn how to find First and Follow sets. http://geeksquiz.com/compiler-design-first-in-syntax-analysis/ http://geeksquiz.com/compiler-design-follow-set-in-syntax-analysis/
Question 29
Consider the grammar shown below.
S → C C
C → c C | d
The grammar is
A
LL(1)
B
SLR(1) but not LL(1)
C
LALR(1) but not SLR(1)
D
LR(1) but not LALR(1)
Parsing and Syntax directed translation    GATE-CS-2003    
Discuss it


Question 29 Explanation: 
Since there is no conflict, the grammar is LL(1). We can construct a predictive parse table with no conflicts. This grammar also LR(0), SLR(1), CLR(1) and LALR(1).
Question 30
Consider the translation scheme shown below
S → T R
R → + T {print ('+');} R | ε
T → num {print (num.val);} 
Here num is a token that represents an integer and num.val represents the corresponding integer value. For an input string '9 + 5 + 2', this translation scheme will print
A
9 + 5 + 2
B
9 5 + 2 +
C
9 5 2 + +
D
+ + 9 5 2
Parsing and Syntax directed translation    GATE-CS-2003    
Discuss it


Question 30 Explanation: 
Let us make the parse tree for 9+5+2 in top down manner, left first derivation.
Steps:
1) Exapnd S->TR
2) apply T->Num...
3) apply R -> +T...
4) appy T->Num...
5) apply R-> +T..
6) apply T-> Num..
7) apply R-> epsilon 
After printing through the print statement in the parse tree formed you will get the answer as 95+2+
Question 31
Consider the syntax directed definition shown below.
S → id : = E  {gen (id.place = E.place;);}
E → E1 + E2   {t = newtemp ( ); gen (t = El.place + E2.place;); E.place = t}
E → id     {E.place = id.place;} 
Here, gen is a function that generates the output code, and newtemp is a function that returns the name of a new temporary variable on every call. Assume that ti's are the temporary variable names generated by newtemp. For the statement 'X: = Y + Z', the 3-address code sequence generated by this definition is
A
X = Y + Z
B
t1 = Y + Z; X = t1
C
t1 =Y; t2 = t1 + Z; X = t2
D
t1 = Y; t2 = Z; t3 = t1 + t2; X = t3
Parsing and Syntax directed translation    GATE-CS-2003    
Discuss it


Question 31 Explanation: 
It must be B. The production E --> E + E is used only one time and hence only one temporary variable is generated.
Question 32
Which of the following statements is false?
A
An unambiguous grammar has same leftmost and rightmost derivation
B
An LL(1) parser is a top-down parser
C
LALR is more powerful than SLR
D
An ambiguous grammar can never be LR(k) for any k
Parsing and Syntax directed translation    GATE-CS-2001    
Discuss it


Question 32 Explanation: 
1. A grammar is ambiguous if there exists a string s such that the grammar has more than one leftmost derivations for s. We could also come up with more than one rightmost derivations for a string to prove the above proposition, but not both of right and leftmost. An unambiguous grammar can have different rightmost and leftmost derivations. 2. LL parser is top-down by nature. Leftmost derivation is, intuitively, expanding or top-down in fashion, hence such convention. Rightmost derivation, on the other hand, seems like a compressing or bottom-up thing. 3. LALR is more powerful than SLR, even when both have the same LR(0) states, due to the fact that SLR checks for lookaheads by looking at FIRST and FOLLOW from the grammar after constructing its parse table and on the other hand, LALR computes lookaheads from the LR(0) states while constructing the parse table, which is a better method. 4. An ambiguous grammar can never be LR(k) for any k, because LR(k) algorithm aren’t designed to handle ambiguous grammars. It would get stuck into undecidability problem, if employed upon an ambiguous grammar, no matter how large the constant k is. Reference: http://stackoverflow.com/questions/2676144/what-is-the-difference-between-lr-slr-and-lalr-parsers/16575211#16575211 See question 3 of http://www.geeksforgeeks.org/compilers-set-1/ This solution is contributed by Vineet Purswani.
Question 33
Which of the following derivations does a top-down parser use while parsing an input string? The input is assumed to be scanned in left to right order.
A
Leftmost derivation
B
Leftmost derivation traced out in reverse
C
Rightmost derivation
D
Rightmost derivation traced out in reverse
Parsing and Syntax directed translation    GATE-CS-2000    
Discuss it


Question 33 Explanation: 
Question 34
Given the following expression grammar:
E -> E * F | F + E | F
F -> F - F | id 
which of the following is true? (GATE CS 2000)
A
* has higher precedence than +
B
– has higher precedence than *
C
+ and — have same precedence
D
+ has higher precedence than *
Parsing and Syntax directed translation    GATE-CS-2000    
Discuss it


Question 34 Explanation: 
Let say i/p is 3*4-5 when we draw parse tree according to grammar
      E
   /  |  \
  E   *   F
  |     / | \
  F    F  -  F
  |    |     |
id(3) id(4) id(5)
As we can see first '- ' will be evaluated then ' * ' is evaluated so ' - ' has higher precedence then *. So correct choice is B See question 1 of http://www.geeksforgeeks.org/compilers-set-2/
Question 35
Which one of the following is True at any valid state in shift-reduce parsing?
A
Viable prefixes appear only at the bottom of the stack and not inside
B
Viable prefixes appear only at the top of the stack and not inside
C
The stack contains only a set of viable prefixes
D
The stack never contains viable prefixes
Parsing and Syntax directed translation    GATE-CS-2015 (Set 1)    
Discuss it


Question 35 Explanation: 
The prefixes of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefixes. By definition, a viable prefix is a prefix of a right sentential form that does not continue past the right end of the rightmost handle of that sentential form. Source http://cse.iitkgp.ac.in/~bivasm/notes/scribe/11CS30001.pdf
Question 36
In the context of abstract-syntax-tree (AST) and control-flow-graph (CFG), which one of the following is True?
A
In both AST and CFG, let node N2 be the successor of node N1. In the input program, the code corresponding to N2 is present after the code corresponding to N1
B
For any input program, neither AST nor CFG will contain a cycle
C
The maximum number of successors of a node in an AST and a CFG depends on the input program
D
Each node in AST and CFG corresponds to at most one statement in the input program
Parsing and Syntax directed translation    GATE-CS-2015 (Set 2)    
Discuss it


Question 36 Explanation: 
(A) is false, In CFG (Control Flow Graph), code of N2
may be present before N1 when there is a loop or goto.

(B) is false, CFG (Control Flow Graph) contains cycle
when input program has loop.

(C) is true, successors in AST and CFG depedend on
input program

(D) is false, a single statement may belong to a block
of statements. 
Question 37
Match the following:
      List-I                  List-II
A. Lexical analysis       1. Graph coloring
B. Parsing                2. DFA minimization
C. Register allocation    3. Post-order traversal
D. Expression evaluation  4. Production tree
Codes:
    A B C D
(a) 2 3 1 4
(b) 2 1 4 3
(c) 2 4 1 3
(d) 2 3 4 1
A
a
B
b
C
c
D
d
Parsing and Syntax directed translation    GATE-CS-2015 (Set 2)    
Discuss it


Question 37 Explanation: 
Register allocation is a variation of Graph Coloring problem.

Lexical Analysis uses DFA.

Parsing makes production tree

Expression evaluation is done using tree traversal 
Question 38
Among simple LR (SLR), canonical LR, and look-ahead LR (LALR), which of the following pairs identify the method that is very easy to implement and the method that is the most powerful, in that order?
A
SLR, LALR
B
Canonical LR, LALR
C
SLR, canonical LR
D
LALR, canonical LR
Parsing and Syntax directed translation    GATE-CS-2015 (Set 3)    
Discuss it


Question 38 Explanation: 
SLR parser is a type of LR parser with small parse tables and a relatively simple parser generator algorithm. Canonical LR parser or LR(1) parser is an LR(k) parser for k=1, i.e. with a single lookahead terminal. It can handle all deterministic context-free languages. LALR parser or Look-Ahead LR parser is a simplified version of a canonical LR parser,
Question 39
Consider the following grammar G.
  S → F ⎪ H
  F → p ⎪ c
  H → d ⎪ c 
Where S, F and H are non-terminal symbols, p, d and c are terminal symbols. Which of the following statement(s) is/are correct?
S1: LL(1) can parse all strings that are generated using grammar G.
S2: LR(1) can parse all strings that are generated using grammar G. 
A
Only S1
B
Only S2
C
Both S1 and S2
D
Neither S1 and S2
Parsing and Syntax directed translation    GATE-CS-2015 (Set 3)    
Discuss it


Question 39 Explanation: 
The given grammar is ambiguous as there are two possible leftmost derivations for string "c".

First Leftmost Derivation
  S → F 
  F → c

Second Leftmost Derivation
  S → H
  H → c  
An Ambiguous grammar can neither be LL(1) nor LR(1)
Question 40
Consider the following Syntax Directed Translation Scheme (SDTS), with non-terminals {S, A} and terminals {a, b}}. gt46 Using the above SDTS, the output printed by a bottom-up parser, for the input aab is
A
1 3 2
B
2 2 3
C
2 3 1
D
Syntax Error
Parsing and Syntax directed translation    GATE-CS-2016 (Set 1)    
Discuss it


Question 40 Explanation: 
Bottom up parser builds the parse tree from bottom to up, i.e from the given string to the starting symbol. The given string is aab and starting symbol is S. so the process is to start from aab and reach S. =>aab ( given string) =>aSb (after reduction by S->a, and hence print 2) =>aA (after reduction by A->Sb, and hence print 3) =>S (after reduction by S->aA, and hence print 1) As we reach the starting symbol from the string, the string belongs to the language of the grammar. Another way to do the same thing is :- bottom up parser does the parsing by RMD in reverse. RMD is as follows: =>S => aA (hence, print 1) => aSb (hence, print 3) => aab (hence, print 2) If we take in Reverse it will print : 231
Question 41
Which one of the following grammars is free from left recursion? z3
A
A
B
B
C
C
D
D
Parsing and Syntax directed translation    GATE-CS-2016 (Set 2)    
Discuss it


Question 41 Explanation: 
Grammar A has direct left recursion because of the production rule: A->Aa. Grammar C has indirect left recursion because of the production rules:S-> Aa and A->Sc Grammar D has indirect left recursion because of production rules : A-> Bd and B-> Ae Grammar B doesn't have any left recursion (neither direct nor indirect).
Question 42
A CFG G is given with the following productions where S is the start symbol, A is a non-terminal and a and b are terminals.
S→aS∣A
A→aAb∣bAa∣ϵ
For the correct answer in Q75, how many steps are required to derive the string and how many parse trees are there?
A
6 and 1
B
6 and 2
C
7 and 2
D
4 and 2
Context free languages and Push-down automata    Parsing and Syntax directed translation    Gate IT 2008    
Discuss it


Question 42 Explanation: 
2008_78_sol
There are 42 questions to complete.

GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.