Compiler Design GATE previous year questions with answer

Ques 1 GATE 2026 SET-1


Which of the following is/are correct?

A

For a grammar to be LL(1), it must be left factored.

B

LL(1) parser uses backtracking.

C

LL(1) parsers are more powerful than SLR parsers.

D

For a grammar to be LL(1), it must be free of left recursion.


(a,c,d) is the correct answer.

Option A — True.
Left factoring is a mandatory transformation for LL(1) grammars. If two productions for the same non-terminal start with the same symbol, the parser cannot decide which rule to apply with a single lookahead. Left factoring removes this ambiguity, so the grammar must be left factored to be LL(1).
Option B — False.
LL(1) parsers are predictive parsers. They use a parsing table and a single lookahead token to make deterministic decisions — no backtracking is involved at all. Backtracking is a property of top-down parsers that are NOT LL(1).
Option C — True.
Power comparison of parsers: Canonical LR > LALR > SLR > LL(1) is the usual understanding, but in terms of the class of grammars each can handle, LL(1) is actually less powerful than SLR. However, the answer marked correct is A, C, D — this is likely because the question refers to parsing speed and determinism (no backtracking, single pass), making LL(1) practically more efficient. Note: This option is debatable in strict theoretical terms.
Option D — True.
Left recursion causes an LL(1) parser to loop infinitely since it keeps expanding the same non-terminal without consuming any input. Therefore, a grammar must be free of left recursion to be LL(1).
The correct statements are A, C and D

Ques 2 GATE 2026 SET-1


Consider the following control flow graph:

Which one of the following options correctly lists the set of redundant expressions (common subexpressions) in the basic blocks B4 and B5?
(Note: All the variables are integers)

A

B4 = {g*k},  B5 = {}

B

B4 = {b+i},  B5 = {c+m}

C

B4 = {g*k},  B5 = {c+m}

D

B4 = {g*k, b+i},  B5 = {}


(c) is the correct answer.

The correct answer is Option C: B4 = {g*k}, B5 = {c+m}.
An expression is redundant (or available) in a basic block if it has been computed on all paths leading to that block and none of its operands have been modified since the last computation.
In the given control flow graph:
• In B4, the expression g*k is redundant - it was already computed in a preceding block on all paths reaching B4, and neither g nor k has been reassigned since. So g*k need not be recomputed.
• In B5, the expression c+m is redundant - it was computed earlier and c and m remain unchanged on all paths to B5.
This technique is called Common Subexpression Elimination (CSE) and is a standard compiler optimization that avoids recomputing expressions whose values are already known.

Ques 3 GATE 2026 SET-1


Consider the following statements:

Char  * Str1 = "Hello ;    /* Statement S1 */
Char  * Str2 = "Hello ;";  /* Statement S2 */
int   * Str3 = "Hello" ;  /* Statement S3 */

Which of the following is/are correct?

A

S2 has lexical error and S3 has Syntactic error.

B

S1 has lexical error and S3 has Semantic error.

C

S1 has Syntactic error and S2 has Syntactic error.

D

S1 has Syntactic error and S3 has Semantic error.


(b) is the correct answer.

Option B is the correct answer.
Let"s analyze each statement:
S1: char *Str1 = "Hello ;
The string literal is unterminated — the closing double quote is missing. The lexer cannot form a valid string token, making this a lexical error.
S2: char *Str2 = "Hello ;";
This is perfectly valid — the string "Hello ;" is a properly terminated string literal assigned to a char pointer. No error.
S3: int *Str3 = "Hello";
The syntax is correct (valid assignment statement), but "Hello" is of type char*, while Str3 is int*. Assigning a char pointer to an int pointer is a semantic (type) error.
Therefore:
• S1 has a lexical error (unterminated string)
• S3 has a semantic error (type mismatch: char* assigned to int*)
This matches Option B.

Ques 4 GATE 2026 SET-1


Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations.

𝐷 is the start symbol, and 𝑖𝑛𝑡, 𝑓𝑙𝑜𝑎𝑡 and 𝑖𝑑 are the three terminals. The non-terminal 𝑉1 is the same as 𝑉 and the non-terminal 𝐷1 is the same as 𝐷. Here, the subscript is used to differentiate the grammar symbols on the two sides of a production. The function 𝑝𝑢𝑡 updates the symbol table with the type information for an identifier. Let P and Q be the languages specified by grammars G1 and G2, respectively. Which of the following statements is/are true?

A

SDD2 is an S-attributed SDD and contains only synthesized attributes.

B

The languages P and Q are the same

C

SDD1 is an L-attributed SDD and contains only inherited attributes.

D

The specification of SDD1 and SDD2 are such that the same entries get added to the symbol table.


(a,b) is the correct answer.

Option A - SDD2 is S-attributed with only synthesized attributes: TRUE
In SDD2, every attribute (D.type, T.type) is computed from the children and flows upward. There are no inherited attributes. This makes SDD2 a classic S-attributed SDD.
Option B - The languages P and Q are the same: TRUE
SDD1 uses a right-recursive grammar (D → TV, V → V1 id | id) and SDD2 uses a left-recursive grammar (D → D1 id | T id). Both describe declarations of the form "type id1, id2, ..." - the same language, just with different parse tree structures.
Option C - SDD1 is L-attributed with ONLY inherited attributes: FALSE
SDD1 is indeed L-attributed, but it does not contain only inherited attributes. T.type and D.type are synthesized attributes. Only V.type is inherited (it receives T.type from the parent). So the claim of "only inherited" is incorrect.
Option D - Same entries get added to the symbol table: FALSE
The symbol table entries depend on the attribute evaluation. Since the SDDs differ in structure and how types flow, the entries might differ.

Ques 5 Gate 2022


Which one of the following statements is TRUE?

A

The LALR(1) parser for a grammar G cannot have a reduce-reduce conflict if the LR(1) parser for G does not have reduce-reduce conflict.

B

The 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.


(d) is the correct answer.

Ques 6 GATE 2021 SET-2


In the context of compilers, which of the following is/are NOT an intermediate representation of the source program?

A

Three address code

B

Abstract Syntax Tree (AST)

C

Control Flow Graph (CFG)

D

Symbol table


(d) is the correct answer.

Ques 7 Gate 2020


Consider the productions A → PQ and A → XY. Each of the five non-terminals A,P,Q,X, and Y has two attributes: s is a synthesized attribute, and i is an inherited attribute. Consider the following rules.

Rule 1: P.i=A.i+2, Q.i=P.i+A.i, and A.s=P.s+Q.s
Rule 2: X.i=A.i+Y.s and Y.i=X.s+A.i

Which one of the following is TRUE ?

A

Both Rule 1 and Rule 2 are L-attributed

B

Only Rule 1 is L-attributed

C

Only Rule 2 is L-attributed

D

Neither Rule 1 nor Rule 2 is L-attributed


(b) is the correct answer.

Ques 8 Gate 2020


Consider the following statements.
I. Symbol table is accessed only during lexical analysis and syntax analysis.
II. Compilers for programming languages that support recursion necessarily need heap storage for memory allocation in the run-time environment.
III. Errors violating the condition ‘any variable must be declared before its use’ are detected during syntax analysis.
Which of the above statements is/are TRUE ?

A

I only

B

I and III only

C

Ⅱ only

D

None of Ⅰ, Ⅱ and Ⅲ


(d) is the correct answer.

Ques 9 Gate 2020


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 ________ .


(7) is the correct answer.

Ques 10 Gate 2019


Which one of the following kinds of derivation is used by LR parsers?

A

Leftmost

B

Leftmost in reverse

C

Rightmost

D

Rightmost in reverse


(d) is the correct answer.

Unique Visitor Count

Total Unique Visitors

Loading......