Professional Documents
Culture Documents
Compiler Design
Compiler Design
Compiler Design
1. Basics
2. Lexical
3. Parsing
4. SDT
5. ICG & TCG
6. Code Optimum
7. Miscellaneous
Basics
[A] 5 [B] 25
[C]36 [D]42
2) Faster access to non-local variables is achieved using an array of pointers to activation records called a
2 Marks GATE-CSE/IT-1998( )
[A] 1, 2, 1, 2 [B] 2, 1, 2, 1
[C]2, 1, 1, 2 [D]1, 2, 2, 2
5) The correct matching for the following pairs is
(A) Activation record (1) Linking loader
(B) Location counter (2) Garbage collection
(C) Reference counts (3) Subroutine call
(D) Address relocation (4) Assembler
2 Marks GATE-CSE/IT-1996( )
[A] A – 3 B – 4 C – 1 D - 2 [B] A – 4 B – 3 C – 1 D - 2
[C]A – 4 B – 3 C – 2 D - 1 [D]A – 3 B – 4 C – 2 D - 1
6) In the following Pascal program segment, what is the value of X after the execution of the program
segment?
X:=-10; Y:=20;
If X >Y then if X <0 then X:=abs(X) else X:=2*X;
Basics
1 Marks GATE-CSE/IT-1995( )
[A] I [B] II
[C]III [D]Alloftheabove
9) A shift reduce parser carries out the actions specified within braces immediately after reducing with the
corresponding rule of grammar
_S _→ _x_xW {_print"1"}
_S _→ _y {_print"2"}
1_W _→ _Sz {_print"3"}
What is the translation of xxxxyzz using the syntax directed translation scheme described by the above
rules?
2 Marks GATE-CSE/IT-1995( )
Key Paper
1. D 2. C 3. C 4. B 5. D
6. B 7. D 8. B 9. A 10. B
Lexical Anal
1) Consider the grammar rule E --> E1 – E2 for arithmetic expressions. The Code generated is targeted to a
CPU having a single user register. The subtraction operation required the first operand to be in the
register. If E1 and E2 do not have any common sub expression, in order to get the shortest possible code
1 Marks GATE-CSE/IT-2004( )
[A] 2 [B] 3
[C]5 [D]7
3) Consider the grammar rule E E1 E2 for arithmetic expressions. The Code generated is targeted to a
CPU having a single user register. The subtraction operation required the first operand to be in the
register.If E1 and E2 do not have any common sub expression, in order to get the shortest possible code
1 Marks GATE-CSE/IT-2004( )
Key Paper
1. B 2. D 3. B
Parsing
Consider the CFG with {S,A,B} as the non-terminal alphabet, {a,b} as the terminal alphabet, S as the start
symbol and the following set of production rules.
S bA S aB
A a B b
A aS B bS
A bAA B aBB
[A] 1 [B] 2
[C]3 [D]4
Common Data for Q3 and Q4 is given below
Consider the following expression grammar. The semantic rules for expression calculation are stated
next to each grammar production.
E number E.val= number.val
|E’+’E E .val= E(2).val + E(3).val
(1)
[A] It detects recursion and eliminates recursion [B] It detects reduce-reduce conflict, and resolves
[C] It detects shift-reduce conflict, and resolves the [D] It detects shift-reduce conflict, and resolves the
conflict in favor of a shift over a reduce action conflict in favor of a reduce over a shift action
4) 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 x 2 + 1. What
precedence and associativity properties does the generated parser realize ?
2 Marks GATE-CSE/IT-2005,GATE-CSE/IT-2006,GATE-CSE/IT-2005( )
[A] Equal precedence and left associativity; [B] Equal precedence and right associativity;
expression is evaluated to 7 expression is evaluated to 9
[C]Precedence of ‘ x ‘ is higher than that of ‘+’, and
[D]Precedence of ‘+’ is higher than that of ‘x’, and
both operators are left associative; expression is both operators are left associative; expression is
evaluaterd to 7 evaluaterd to 9
Common Data for Q5 and Q6 is given below
Consider the CFG with {S,A,B} as the non-terminal alphabet, {a,b} as the terminal alphabet, S as the start
symbol and the following set of production rules.
S -->bA S --> aB
A -->a B --> b
A --> aS B --> bS
1 Marks GATE-CSE/IT-2007( )
[A] 1 [B] 2
[C]3 [D]4
Common Data for Q7 and Q8 is given below
Consider the following expression grammar. The semantic rules for expression calculation are stated
next to each grammar production
7) 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 ?
2 Marks GATE-CSE/IT-2005( )
[A] It detects recursion and eliminates recursion [B] It detects reduce-reduce conflict, and resolves
[C] It detects shift-reduce conflict, and resolves the [D] It detects shift-reduce conflict, and resolves the
conflict in favor of a shift over a reduce action conflict in favor of a reduce over a shift action
8) 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 x 2 + 1. What
precedence and associativity properties does the generated parser realize ?
2 Marks GATE-CSE/IT-2005( )
[A] Equal precedence and left associativity; [B] Equal precedence and right associativity;
expression is evaluated to 7 expression is evaluated to 9
[C]Precedence of ‘ x ‘ is higher than that of ‘+’, and [D] Precedence of ‘+’ is higher than that of ‘x’, and
both operators are left associative; expression is both operators are left associative; expression is
evaluated to 7 evaluaterd to 9
9) 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 ?
2 Marks GATE-CSE/IT-2005( )
In the predictive parser table, M, of the grammar the entries M[S,id] and M[R,$] respectively
2 Marks GATE-CSE/IT-2006( )
[A]
[B] S -->aS | Sb | a | b
[C] [D]
S --> iCtSS1 | a
S1 --> eS |
C -->b
The grammar is NOT LL(1) because:
2 Marks GATE-CSE/IT-2007( )
16) Consider the following two sets of LR(1) items of an LR(1) grammar
X → c . X, c / d X→ c . X, $
X → . cX, c / d X → . cX, $
X → . d, c / d 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
2 Marks GATE-CSE/IT-2013( )
[A] It is the position in a sentential form where the [B] It is a non-terminal whose production will be used
next shift or reduce operation will occur for reduction in the next step
[C]It is a production that may be used for reduction [D]It is the production p that will be used for
in a future step along with a position in the reduction in the next step along with a position in
sentential form where the next shift or reduce the sentential form where the right hand side of
operation will occur the production may be found
25) An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if.
2 Marks GATE-CSE/IT-2008( )
[A] the SLR (1) parser for G has S-R conflicts [B] the LR(1) parser for G has S-R conflicts
[D]the LALR(1) parser for G has reduce-reduce
[C]the LR (0) parser for G has S-R conflicts conflicts
26) The grammar A AA|(A)| is not suitable for predictive-parsing because the grammar is
2 Marks GATE-CSE/IT-2005( )
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 ?
1 Marks GATE-CSE/IT-2006( )
S FR
R *S|
F id
In the predictive parser table, M, of the grammar the entries M[S,id] and M[R,$] respectively.
2 Marks GATE-CSE/IT-2006( )
L=(ai bj | i j} ?
2 Marks GATE-CSE/IT-2006( )
[A] S AC | CB
C aCb | a | b
[B] S aS | Sb | a | b
A aA |
B Bb |
[C] S AC CB [D] S AC | CB
C aCb | C aCb |
A aA | A aA | a
B Bb | B bB | b
32) Which of the following describes a handle (as applicable to LR-parsing) appropriately ?
1 Marks GATE-CSE/IT-2008( )
[A] It is the position in a sentential form where the [B] It is a non-terminal whose production will be used
next shift or reduce operation will occur for reduction in the next step
[C]It is a production that may be used for reduction
[D]It is the production p that will be used for
in a future step along with a position in the reduction in the next step along with a position in
sentential form where the next shift or reduce the sentential form where the right hand side of
operation will occur. the production may be found
33) An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if.
2 Marks GATE-CSE/IT-2008( )
[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
34) The grammar is
2 Marks GATE-CSE/IT-2010( )
[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)
35) Which of the following statements is false?
1 Marks GATE-CSE/IT-2001( )
Q.
The First and Follow sets for the non-terminals A and B are
2 Marks GATE-CSE/IT-2012,GATE-CSE/IT-2012( )
2 Marks GATE-CSE/IT-2012( )
Key Paper
1. C 2. B 3. C 4. B 5. C
6. B 7. C 8. C 9. C 10. A
[A] 9 + 5 + 2 [B] 9 5 + 2 +
[C]9 5 2 + + [D]+ + 9 5 2
2) Consider the syntax directed definition shown below
[A] X = Y + Z [B] = Y + Z ; X =
[C] = Y ; = + Z ; X = [D] = Y ; = Z ; = + ; X =
3) Consider the grammar with the following translation rules and E as the start symbol.
S -->ER
R -->*E {print{‘*’); R |
E -->F + E {print(‘+’); | F
F --> (S) | id {print (id.value);}
Here id is a taken that represents an integer and id. Value represents the corresponding integer value. For
an input ‘2 * 3 + 4’, this translation scheme prints
2 Marks GATE-CSE/IT-2006( )
5) 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.
2 Marks GATE-CSE/IT-2004( )
[A] 200 [B] 180
[C]160 [D]40
6) 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 taken that represents an integer and id. Value represents the corresponding integer value. For
an input ‘2 * 3 + 4’, this translation scheme prints
2 Marks GATE-CSE/IT-2006( )
2 Marks GATE-CSE/IT-2011( )
[A] 2 [B] 9
[C]5 [D]3
8) Type checking is normally done during
1 Marks GATE-CSE/IT-1998( )
[B] syntax-directed
[A] itmakesimplementationoflexicalanalysisandsyntax
translationscanbewritten forintermediatecodegen
analysiseasier
eration
[C]itenhancesthe portabilityofthefrontendofthe compi [D] itisnotpossibletogeneratecodeforrealmachinesdire
ler ctlyfromhighlevellanguageprograms
SDT
Key Paper
1. B 2. B 3. C 4. D 5. C
6. D 7. D 8. C 9. C
ICG & TCG
1) Consider the grammar rule E --> E1 – E2 for arithmetic expressions. The Code generated is targeted to a
CPU having a single user register. The subtraction operation required the first operand to be in the
register. If E1 and E2 do not have any common sub expression, in order to get the shortest possible code
1 Marks GATE-CSE/IT-2004( )
[A] 2 [B] 3
[C]5 [D]7
3) Consider the grammar rule E E1 E2 for arithmetic expressions. The Code generated is targeted to a
CPU having a single user register. The subtraction operation required the first operand to be in the
register.If E1 and E2 do not have any common sub expression, in order to get the shortest possible code
1 Marks GATE-CSE/IT-2004( )
Key Paper
1. B 2. D 3. B
Code Optim
1) Some code optimizations are carried out on the intermediate code because
1 Marks GATE-CSE/IT-2008( )
[A] They enhance the portability of the complier to [B] Program analysis is more accurate on
other target processors intermediate code than on machine code
[C]The information from data flow analysis cannot [D]The information from the front end cannot
otherwise be used for optimization otherwise be used for optimization
2) Some code optimizations are carried out on the intermediate code because
1 Marks GATE-CSE/IT-2008( )
[A] They enhance the portability of the complier to [B] Program analysis is more accurate on
other target processors intermediate code than on machine code
[C]The information from data flow analysis cannot [D]The information from the front end cannot
otherwise be used for optimization otherwise be used for optimization
3) What is the value of X printed by the following program?
program COMPUTE (input, output);
var
X:integer;
procedure FIND (X:real);
begin
X:=sqrt(X);
end;
begin
X:=2
Find(X)
Writeln(X)
end
2 Marks GATE-CSE/IT-1995( )
[A] 2 [B]
[C]Run time error [D]None of the above
Code Optim
Key Paper
1. B 2. B 3. A
Miscellaneous
1) Consider a program P that consists of two source modules M1 and M2 contained in two different files. If
M1 contains a reference to a function defined in M2, the reference will e resolved at
1 Marks GATE-CSE/IT-2004( )
[A] Removing left recursion alone [B] Factoring the grammar alone
[C] Removing left recursion and factoring the
[D] None of this
grammar
3) Which of the following grammar rules violate the requirements of an operator grammar ? P,Q, R are
noterminals, and r,s, t are terminals.
(i) P Q R (ii) P Qs R
(iii) P (iv) P Q t Rr
1 Marks GATE-CSE/IT-2004( )
[A] It allocates space for the literals. [B] It computes the total length of the program
[C] It builds the symbol table for the symbols and [D] It generates code for all the load and store
their values. register instructions.
[E] None of the above.
Miscellaneous
Key Paper
1. C 2. C 3. B 4. C 5.
D
6. A