Parsers

Question 1

Consider the following grammar.

     S → aSB|d
     B → b 

The number of reduction steps taken by a bottom-up parser while accepting the string aaadbbb is _______.

A
7
       Compiler-Design       Parsers       GATE 2020       Video-Explanation
Question 1 Explanation: 

7 reductions total.
Question 2
Which one of the following statements is TRUE?
A
The LALR (1) parser for a grammar G cannot have reduce-reduce conflict if the LR (1) parser for G does not have reduce-reduce conflict.
B
Symbol table is accessed only during the lexical analysis phase.
C
Data flow analysis is necessary for run-time memory management.
D
LR (1) parsing is sufficient for deterministic context-free languages.
       Compiler-Design       Parsers       GATE 2022       Video-Explanation
Question 2 Explanation: 
Even though there is no reduce-reduce conflict in CLR(1) but in LALR(1) while merging the states differ in only lookahead may get reduce-reduce conflict. So the given statement is not true.
Symbol table is accessed in all the phases of compiler and not only in lexical analysis phase.
Data flow analysis is done in the control flow graph, in the code optimization phase. If LR(1) parses a grammar then definitely it is DCFL, so LR(1) parsing is sufficient for deterministic context-free languages.
Question 3
Consider the augmented grammar with { + , *, (, ), id } as the set of terminals.
A
5
       Compiler-Design       Parsers       GATE 2022       Video-Explanation
Question 3 Explanation: 
Question 4
Consider the following grammar along with translation rules.
A
80
       Compiler-Design       Parsers       GATE 2022       Video-Explanation
Question 4 Explanation: 
Question 5

Which of the following statements is true?

A
SLR parser is more powerful than LALR
B
LALR parser is more powerful than Canonical LR parser
C
Canonical LR parser is more powerful than LALR parser
D
The parsers SLR, Canonical CR, and LALR have the same power
       Compiler-Design       Parsers       GATE 1998
Question 5 Explanation: 
LR > LALR > SLR
Canonical LR parser is more powerful than LALR parser.
Question 6

Which of the following is the most powerful parsing method?

A
LL (1)
B
Canonical LR
C
SLR
D
LALR
       Compiler-Design       Parsers       GATE 1999
Question 6 Explanation: 
Canonical LR is most powerful.
LR > LALR > SLR
Question 7

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
       Compiler-Design       Parsers       GATE 2000
Question 7 Explanation: 
Top-down parser - Leftmost derivation
Bottom-Up parser - Reverse of rightmost derivation
Question 8

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 the above
       Compiler-Design       Parsers       GATE 2003
Question 8 Explanation: 
No. of states in SLR and LALR are equal and no. of states in SLR and LALR are less than or equal to LR(1).
Question 9

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'→ε}
B
{S'→e S} and { }
C
{S'→ε} and {S'→ε}
D
{S'→e S, S'→ε} and {S'→ε}
       Compiler-Design       Parsers       GATE 2003
Question 9 Explanation: 
First(S) = {1,a}
First(S') = {e,ε}
First(E) = {b}
Follow(S') = {e,$}
Only when 'First' contains ε, we need to consider FOLLOW for getting the parse table entry.

Hence, option (D) is correct.
Question 10

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)
       Compiler-Design       Parsers       GATE 2003
Question 10 Explanation: 

Hence, it is LL(1).
Question 11

Which of the following grammar rules violate the requirements of an operator grammar? P,Q,R are nonterminals, and r,s,t are terminals.

    (i) P → Q R
    (ii) P → Q s R
    (iii) P → ε
    (iv) P → Q t R r
A
(i) only
B
(i) and (iii) only
C
(ii) and (iii) only
D
(iii) and (iv) only
       Compiler-Design       Parsers       GATE 2004
Question 11 Explanation: 
Operator values doesn't contains nullable values and two adjacent non-terminals on RHS production.
i) On RHS it contains two adjacent non-terminals.
ii) Have nullable values.
Question 12

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
       Compiler-Design       Parsers       GATE 2005
Question 12 Explanation: 
The given grammar can be turned into a infinite parse tree. So it is ambiguous.
It have A → AA has left recursion.
Question 13

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
       Compiler-Design       Parsers       GATE 2005
Question 13 Explanation: 
→ SLR(1) and LALR(1) both are be the states of LR(0) items then SLR(1) = LALR(1).
→ LR(1) be the states of LR(1) items.
→ LR(0) items never be greater than LR(1) items then SLR(1) = LALR(1) < LR(1)
n1 = (n3) < (n2)
Question 14

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>sup>(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; expression is evaluated to 7
B
Equal precedence and right associativity; expression 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
       Compiler-Design       Parsers       GATE 2005
Question 14 Explanation: 
First of all, it is ambiguous grammar. Hence, equal precedence and associativity. Now as Yacc resolved it with shift move we will shift until the last operator and then we will start reducing.

Hence, the answer is 9 and right associative.
Question 15

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
       Compiler-Design       Parsers       GATE 2006
Question 15 Explanation: 
As we can see in the below given LR(0) items, that all three belongs to different state (sets).
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 → ε}
       Compiler-Design       Parsers       GATE 2006
Question 16 Explanation: 
Predictive parsing table for the mentioned grammar:

The representation M[X,Y] means X represents Variable (rows) and Y represents terminals (columns).
The productions are filled in parsing table by the below mentioned rules:
For every production P → α, we have:
Rule 1: If P → α is a production then add this production for each terminal “t” which is in FIRST of [α] i.e., ADD P → α to M[P, a]
Rule 2: If “ϵ” belongs to FIRST of [P] then add P → α to M[P, b] where “b” represents terminals FOLLOW[P].
By the above rules, we can see that production S → FR will go M[S, a] where “a” is FIRST [FR] which is equal to FIRST[F] = id, So S → FR will go in M[S,id].
Since in the production R→ϵ , FIRST[ϵ] = ϵ, hence the production will go in M[R, b] where “b” represents terminals FOLLOW[R] and FOLLOW[R] = $, so production R→ϵ will go in M[R,$]
Question 17

A shift reduce parser carries out the actions specified within braces immediately after reducing with the corresponding rule of grammar

S → xxW {print "1"}
S → y {print "2"}
W → Sz {print "3"} 

What is the translation of xxxxyzz using the syntax directed translation scheme described by the above rules?

A
23131
B
11233
C
11231
D
33211
       Compiler-Design       Parsers       GATE 1995
Question 17 Explanation: 

⇒ 23131
Note SR is bottom up parser.
Question 18
Consider the following augmented grammar with {#, @, <, >, a, B, c} as the set of terminals.
S’ ⟶ S
S ⟶ S#cS
S ⟶ SS
S ⟶ S@
S ⟶ < S >
S ⟶ a
S ⟶ b
S ⟶ c  
Let I0=CLOSURE({S' ⟶.S}). The number of items in the set GOTO(GOTO(I0, <), <) is ____________.
A
8
       Compiler-Design       Parsers       GATE 2021 CS-Set-2       Video-Explanation
Question 18 Explanation: 
Question 19
Consider the following statements.
S1:Every SLR(1) grammar is unambiguous but there are certain unambiguous grammars that  are not SLR(1).
S2: For any context-free grammar, there is a parser that takes at most O(n3 )
Which one of the following options is correct?
A
S1is true and S2is true
B
S1is true and S2is false
C
S1is false and S2is true
D
S1is false and S2is false
       Compiler-Design       Parsers       GATE 2021 CS-Set-1       Video-Explanation
Question 19 Explanation: 

Every unambiguous grammar need not be SLR(1). As we know some unambiguous grammar which is CLR(1)  but not SLR(1).  So S1 is true.


Any CFG (which is in CNF form) can be parsed by CYK algorithm in O(n3) where n is length of string. Although it is not given that CFG is in CNF form but since we can convert any CFG in CNF form so S2 is true

Question 20
 Consider the following context-free grammar where the set of terminals is {a, b, c, d, f}

Which one of the following choices represents the correct combination for the numbered cells in the parsing table (“blank” denotes that the corresponding cell is empty)? 
A
① S ⟶ Rf ② S ⟶ Rf ③ T ⟶ ∊ ④ T ⟶ ∊
B
① blank ② S ⟶ Rf ③ T ⟶ ∊ ④ T ⟶ ∊
C
① S ⟶ Rf ② blank ③ blank ④ T ⟶ ∊
D
① blank ② S ⟶ Rf ③ blank ④ blank
       Compiler-Design       Parsers       GATE 2021 CS-Set-1       Video-Explanation
Question 20 Explanation: 
Question 21

An operator precedence parser is a

A
Bottom-up parser.
B
Top-down parser.
C
Back tracking parser.
D
None of the above.
       Compiler-Design       Parsers       GATE 1987       Video-Explanation
Question 21 Explanation: 
An operator precedence parser is a Bottom-up parser.
Question 22

Merging states with a common core may produce __________ conflicts and does not produce ___________ conflicts in an LALR purser.

A
Reduce-Reduce, Shift-Reduce
       Compiler-Design       Parsers       GATE 1989       Video-Explanation
Question 22 Explanation: 
Merge states with a common core may produce Reduce-Reduce conflicts and does not produce Shift-Reduce conflicts in an LALR parser.
Question 23

Choose the correct alternatives (more than one may be correct) and write the corresponding letters only: Indicate all the true statements from the following:

A
Recursive descent parsing cannot be used for grammar with left recursion.
B
The intermediate form the representing expressions which is best suited for code optimization is the post fix form.
C
A programming language not supporting either recursion or pointer type does not need the support of dynamic memory allocation.
D
Although C does not support call by name parameter passing, the effect can be correctly simulated in C.
E
No feature of Pascal violates strong typing in Pascal.
F
A and D
       Compiler-Design       Parsers       GATE 1991       Video-Explanation
Question 23 Explanation: 
(A) It is true. Left recursive grammar if used directly in recursive descent parsing causes an infinite loop. So, left recursion must be removed before giving to a recursive descent parser.
(B) False.
(C) It is false. The language can have dynamic data types which required dynamically growing memory when data type size increases.
(D) Is true and using macro we can do this.
(E) Out of syllabus now.
Question 24
Consider the SLR(1) and LALR (1) parsing tables for a context free grammar. Which of the following statements is/are true?
A
The go to part of both tables may be different.
B
The shift entries are identical in both tables.
C
The reduce entries in the tables may be different.
D
The error entries in the tables may be different.
       Compiler-Design       Parsers       GATE 1992       Video-Explanation
Question 24 Explanation: 
Goto parts and shift entry must be same.
Reduce entry and error entry may be different due to conflicts.
Question 25

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.
       Compiler-Design       Parsers       GATE 2007
Question 25 Explanation: 
Recursive descent parser is top down parser, while others are bottom up parser.
Question 26

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
       Compiler-Design       Parsers       GATE 2007
Question 26 Explanation: 
The given grammar is not left recursive and also it is context free (Type 2 grammar), so option A and D is wrong. Being a right recursive grammar is not an issue for LL(1) grammar. So even if given grammar is right recursive, this is not a reason for NOT LL(1).
This grammar has two parse tree for string “ibt ibt aea”.
Question 27

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
       Compiler-Design       Parsers       GATE 2007
Question 27 Explanation: 
Every regular grammar is LL(1) is false, as the grammar may have left recursion or left factoring or also it is possible that grammar is ambiguous.
For ex: Consider a regular grammar
S -> aS | a | ϵ
this grammar is ambiguous as for string "a" two parse tree is possible.

Hence it is regular but not LL(1).
But every regular set has a language accept or as DFA , so every regular set must have atleast one grammar which is unambiguous.
Hence, every regular set has LR(1) grammar.
Question 28

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.
       Compiler-Design       Parsers       GATE 2008
Question 28 Explanation: 
A handle 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.
Question 29

Which of the following statements about parser is/are CORRECT?

I. Canonical LR is more powerful than SLR.

II. SLR is more powerful than LALR.

III. SLR is more powerful than Canonical LR.

A
I only
B
II only
C
III only
D
II and III only
       Compiler-Design       Parsers       GATE 2017 [Set-2]       Video-Explanation
Question 29 Explanation: 
Canonical LR is more powerful than SLR as every grammar which can be parsed by SLR parser, can also be parsed by CLR parser.
The power in increasing order is:
LR(0) < SLR < LALR < CLR
Hence only I is true.
There are 29 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access