Question 1 
n/2  
n1  
2n1  
2^{n} 
Discuss it
S>aB B>bC C>cdThe 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> cdThe 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 
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 SR conflict. 3. Can be merged but will result in RR conflict. 4. Cannot be merged since goto on c will lead to two different sets.
1 only  
2 only  
1 and 4 only  
1, 2, 3, and 4 
Discuss it
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 LookAhead 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 LookAhead 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 lookahead 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. lookahead were different. Statement 2 : In the merged set, we can't see any ShiftReduce 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 ReduceReduce conflict ( same reason as above, no reduction even possible, so no chances of RR conflict ) Statement 4: This statement is also wrong, because goto is carried on NonTerminals symbols, not on terminal symbols, and c is a terminal symbol. Thus, all statements are wrong, hence option D.
Question 3 
A  
B  
C  
D 
Discuss it
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 
A  
B  
C  
D 
Discuss it
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 
LL(1) but not LR(1)  
LR(1)but not LR(1)  
Both LL(1)and LR(1)  
Neither LL(1)nor LR(1) 
Discuss it
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 nonterminal A such that First(A) contains ε, First(A) ∩ Follow(A) = ∅Source: https://s3apsoutheast1.amazonaws.com/erbuc/files/4147_870334aa99224f789c2c713e7a7f0d53.pdf
Question 6 
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
P4. Q1, R2, S3  
P3, Q1, R4, S2  
P3, Q4, R1, S2  
P2, Q1, R4, S3 
Discuss it
Question 7 
I. There exist parsing algorithms for some programming languages whose complexities are less than O(n^{3}). II. A programming language which allows recursion can be implemented with static storage allocation. III. No Lattributed definition can be evaluated in The framework of bottomup parsing. IV. Code improving transformations can be performed at both source language and intermediate code level.
I and II  
I and IV  
III and IV  
I, III and IV 
Discuss it
Question 8 
It is the position in a sentential form where the next shift or reduce operation will occur
 
It is nonterminal whose production will be used for reduction in the next step  
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  
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 
Discuss it
For Ex : Grammar is : S> aAcBe A>Abb B>d Input string : abbcde Derivation : ( TopDown, Right Most Derivation) S>aAcBe >aAcde >aAbcde >abbcdeHere { 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 nonterminals during the derivation process ) Now, let's look at the question. The question is related to LR parsing which is a bottomup 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 HandlePruning. Hence option D suits best.
Question 9 
the SLR(1) parser for G has SR conflicts  
the LR(1) parser for G has SR conflicts  
the LR(0) parser for G has SR conflicts  
the LALR(1) parser for G has reducereduce conflicts 
Discuss it
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, shiftreduce form will only take place if it is already there in LR(1) states. If there is no SR 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 RR conflict, and then the Grammar won't be LALR(1).
Question 10 
Recursive descent parser.  
Operator precedence parser.  
An LR(k) parser.  
An LALR(k) parser 
Discuss it
Question 11 
S > iCtSS_{1}a S_{1} > eSϵ C > bThe grammar is NOT LL(1) because:
it is left recursive  
it is right recursive  
it is ambiguous  
It is not contextfree. 
Discuss it
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 NonTerminal 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 nonterminal and V is a set of Nonterminals 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 viceversa.Now,
 For the production , S>iCtSS1a, 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).
Question 12 
P: Every regular grammar is LL(1) Q: Every regular set has a LR(1) grammarWhich of the following is TRUE?
Both P and Q are true  
P is true and Q is false  
P is false and Q is true  
Both P and Q are false 
Discuss it
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 
S > S * E S > E E > F + E E > F F > idConsider the following LR(0) items corresponding to the grammar above.
(i) S > S * .E (ii) E > F. + E (iii) E > F + .EGiven the items above, which two of them will appear in the same set in the canonical setsofitems for the grammar?
(i) and (ii)  
(ii) and (iii)  
(i) and (iii)  
None of the above 
Discuss it
Question 14 
S > L. > R Q > R.On input symbol < the set has
a shiftreduce conflict and a reducereduce conflict.  
a shiftreduce conflict but not a reducereduce conflict.  
a reducereduce conflict but not a shiftreduce conflict.  
neither a shiftreduce nor a reducereduce conflict. 
Discuss it
Question 15 
S > T * P T > U  T * U P > Q + P  Q Q > Id U > IdWhich one of the following is TRUE?
+ is left associative, while ∗ is right associative  
+ is right associative, while ∗ is left associative  
Both + and ∗ are right associative  
Both + and ∗ are left associative

Discuss it
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 
S → FR R → S  ε F → idIn the predictive parser table, M, of the grammar the entries M[S, id] and M[R, $] respectively.
{S → FR} and {R → ε }  
{S → FR} and { }  
{S → FR} and {R → *S}  
{F → id} and {R → ε} 
Discuss it
Question 17 
2 * 3 + 4  
2 * +3 4  
2 3 * 4 +  
2 3 4+* 
Discuss it
Explanation : We are given LAttributed 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:
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 : This explanation has been contributed by Pranjul Ahuja.
Question 18 
ambiguous  
leftrecursive  
rightrecursive  
an operatorgrammar 
Discuss it
Question 19 
E → E + n  E × n  nFor a sentence n + n × n, the handles in the rightsentential form of the reduction are
n, E + n and E + n × n  
n, E + n and E + E × n  
n, n + n and n + n × n  
n, E + n and E × n 
Discuss it
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 
S → (S)  aLet 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
n1 < n2 < n3  
n1 = n3 < n2  
n1 = n2 = n3  
n1 ≥ n3 ≥ n2 
Discuss it
Question 21 
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).valThe 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?
It detects recursion and eliminates recursion  
It detects reducereduce conflict, and resolves  
It detects shiftreduce conflict, and resolves the conflict in favor of a shift over a reduce action  
It detects shiftreduce conflict, and resolves the conflict in favor of a reduce over a shift action 
Discuss it
Question 22 
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).valAssume 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?
Equal precedence and left associativity; expression is evaluated to 7  
Equal precedence and right associativity; expression is evaluated to 9  
Precedence of '×' is higher than that of '+', and both operators are left associative; expression is evaluated to 7  
Precedence of '+' is higher than that of '×', and both operators are left associative; expression is evaluated to 9 
Discuss it
Question 23 
1. P → Q R 2. P → Q s R 3. P → ε 4. P → Q t R r
1 only  
1 and 3 only  
2 and 3 only  
3 and 4 only 
Discuss it
Question 24 
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.
200  
180  
160  
40 
Discuss it
Question 25 
Removing left recursion alone  
Factoring the grammar alone  
Removing left recursion and factoring the grammar  
None of these 
Discuss it
Question 26 
n1 is necessarily less than n2  
n1 is necessarily equal to n2  
n1 is necessarily greater than n2  
none of these 
Discuss it
Question 27 
always be evaluated  
be evaluated only if the definition is Lattributed  
be evaluated only if the definition has synthesized attributes  
never be evaluated 
Discuss it
Question 28 
{S' → e S} and {S' → e}  
{S' → e S} and {}  
{S' → ε} and {S' → ε}  
{S' → e S, S'→ ε} and {S' → ε} 
Discuss it
Question 29 
S → C C C → c C  dThe grammar is
LL(1)  
SLR(1) but not LL(1)  
LALR(1) but not SLR(1)  
LR(1) but not LALR(1) 
Discuss it
Question 30 
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
9 + 5 + 2  
9 5 + 2 +  
9 5 2 + +  
+ + 9 5 2 
Discuss it
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> epsilonAfter printing through the print statement in the parse tree formed you will get the answer as 95+2+
Question 31 
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 3address code sequence generated by this definition is
X = Y + Z  
t1 = Y + Z; X = t1  
t1 =Y; t2 = t1 + Z; X = t2  
t1 = Y; t2 = Z; t3 = t1 + t2; X = t3 
Discuss it
Question 32 
An unambiguous grammar has same leftmost and rightmost derivation  
An LL(1) parser is a topdown parser  
LALR is more powerful than SLR  
An ambiguous grammar can never be LR(k) for any k 
Discuss it
Question 33 
Leftmost derivation  
Leftmost derivation traced out in reverse
 
Rightmost derivation  
Rightmost derivation traced out in reverse 
Discuss it
Question 34 
E > E * F  F + E  F F > F  F  idwhich of the following is true? (GATE CS 2000)
* has higher precedence than +  
– has higher precedence than *  
+ and — have same precedence  
+ has higher precedence than * 
Discuss it
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/compilersset2/
Question 35 
Viable prefixes appear only at the bottom of the stack and not inside  
Viable prefixes appear only at the top of the stack and not inside  
The stack contains only a set of viable prefixes  
The stack never contains viable prefixes 
Discuss it
Question 36 
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  
For any input program, neither AST nor CFG will contain a cycle  
The maximum number of successors of a node in an AST and a CFG depends on the input program  
Each node in AST and CFG corresponds to at most one statement in the input program 
Discuss it
(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 
ListI ListII A. Lexical analysis 1. Graph coloring B. Parsing 2. DFA minimization C. Register allocation 3. Postorder 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  
b  
c  
d 
Discuss it
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 
SLR, LALR  
Canonical LR, LALR  
SLR, canonical LR  
LALR, canonical LR 
Discuss it
Question 39 
S → F ⎪ H F → p ⎪ c H → d ⎪ cWhere S, F and H are nonterminal 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.
Only S1  
Only S2  
Both S1 and S2  
Neither S1 and S2 
Discuss it
First Leftmost Derivation S → F F → c Second Leftmost Derivation S → H H → cAn Ambiguous grammar can neither be LL(1) nor LR(1)
Question 40 
1 3 2  
2 2 3  
2 3 1  
Syntax Error 
Discuss it
Question 41 
A  
B  
C  
D 
Discuss it
Question 42 
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?
6 and 1  
6 and 2  
7 and 2  
4 and 2 
Discuss it