0% found this document useful (0 votes)
9 views22 pages

Compiler Design 09

The document contains a series of practice questions related to compiler design, covering topics such as grammar types, parsing techniques, and syntax analysis. It includes multiple-choice questions, fill-in-the-blanks, and matching exercises that test knowledge on LL(1) and LR(0) grammars, as well as semantic rules and syntax-directed translation. The questions are structured to assess understanding of compiler components and their functionalities.

Uploaded by

Aakus REvol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

Compiler Design 09

The document contains a series of practice questions related to compiler design, covering topics such as grammar types, parsing techniques, and syntax analysis. It includes multiple-choice questions, fill-in-the-blanks, and matching exercises that test knowledge on LL(1) and LR(0) grammars, as well as semantic rules and syntax-directed translation. The questions are structured to assess understanding of compiler components and their functionalities.

Uploaded by

Aakus REvol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

7 Compiler Design

Practice Questions

Q.1 Which of the following statements is (A) bexpr → bterm E’


false? E’ → or bterm E’ | H
(A) Unambiguous grammar has different bterm → bfactor F’
derivation tree F’ → and bfactor F’
(B) An LL(1) parser is a top-down parser bfactor → not bfactor | (bexpr) | true
(C) LALR is more powerful than SLR | false
(D) Ambiguous grammar can’t be LR(k) (B) bexpr → bterm E’
Q.2 Assume that the CLR parser for a E’ → or bterm E’ | H
grammar G has n1 states and the LALR bterm → bfactor and F’
F’ → bfactor F’ | H
parser for G has n2 states. The bfactor → not bfactor | (bexpr) | true
relationship between n1 and n2 is | false
(C) bexpr → bterm E’
(A) n1 is necessarily less than n2 E’ → or bterm E’ | H
(B) n1 is necessarily equal to n2 bterm → bfactor F’
F’ → and bfactor F’ | H
(C) n1 is necessarily greater than n2
bfactor → not bfactor | (bexpr) | true
(D) n1 is necessarily greater than or | false
(D) bexpr → bterm E’
equal to n2
E’ → or bterm E’
Q.3 Consider the following grammar G bterm → bfactor F’
{bexpr, {bexpr, bterm, bfactor}, {not, F’ → and bfactor F’ | H
or, and, ( ), true, false}, P} with P given bfactor → not bfactor | (bexpr) | true
below | false
bexpr → bexpr or bterm | bterm
Q.4 Consider the following Code fragment
bterm → bterm and bfactor | bfactor
int main ()
bfactor → not bfactor | (bexpr) | true |
false {
The equivalent non-left recursive int x, y, total;
grammar for the given grammar is x 10, y 20;

Compiler Design 1
total x  y;
printf ("Total %d \n ", total); Code :
} (a) (b) (c) (d)
Number of tokens in the given code (1) (iii) (iv) (ii) (i)
fragment is ______. (2) (iv) (iii) (ii) (i)
Q.5 Match the description of several parts of (3) (ii) (iv) (i) (iii)
a classic optimizing compiler in List-I, (4) (ii) (iv) (iii) (i)
with the names of those parts in List-II: (A) 1 (B) 2
List-I List-II (C) 3 (D) 4
(a) A part of a compiler (i) Optimizer Q.6 Consider the following code segment.
x u  t;
that is responsible
for recognizing y x uV ;
syntax. z y  w;
w t  z;
(b) A part of a compiler (ii) Semantic
that takes as input a Analysis u x u y;
stream of characters The minimum number of total variables
and produces as required to convert the above code
output a stream of segment to static singe assignment form
words along with is _____
their associated Q.7 The left factored and non-left recursive
syntactic grammar for the given grammar is
categories. Numeral : :-
(c) A part of a compiler (iii) Parser Digits | Digits . Digits
that understand the | Digits e Sign Digits
meanings of | Digits . Digits e Sign Digits
variable names and Digits : :- Digit | Digits Digit
other symbols and Digit : :- 0 | 1| 2 | 3
checks that they are (A) Numeral : := Digits N1 N2
used in ways N1 : := T Sign Digits | H
consistent with their
N2 : := Digits | H
definitions.
Digits : := Digit Digits | Digit
(d) An IR-to-IR (iv) Scanner Digit : := 0 | 1 | 2 | 3
transformer that (B) Numeral : := Digits N1 N2
tries to improve the
N1 : := T Sign Digits | H
IR program in some
N2 : := Digits | H
way (Intermediate
Representation). Digits : := Digit Digits | Digit
Digit : := 0 | 1 | 2 | 3

2 Compiler Design
(C) Numeral : := Digits N A ' o H | BDA '
N : := T Sign Digits | . Digits | H B oC |b
Digits : := Digit Digits | Digit C o A| c
Digit : := 0 | 1 | 2 | 3
Dod
(D) Numeral : := Digits N1
N1 : := e Sign Digits | H | .Digits N2
Q.10 Consider the following grammar:
RoR|R
N2 : := T Sign Digits | H
Digits : := Digit D Ro RR
D : := Digits | H R o R*
Digit : := 0 | 1 | 2 | 3 R o ( R)
Q.8 Consider the following grammar Roa
SL → SL. S
R ob
SL → H
where the terminals are {|,*, (, ), a, b}
S → stmt
The given grammar is Follow(R) is _____
(A) LR(0) and SLR(1) (A) {$}
(B) Not LR(0) but SLR(1) (B) {|,*, )}
(C) Neither LR(0) nor SLR(1)
(C) {|,*, ),$}
(D) LL(1) but not LR(0)
Q.9 The non-left recursive grammar from (D) {(, a, b,|,*, ),$}
the given grammar is Q.11 Consider the following grammar
A o B | a | CBD S o CC
B oC |b
C o cC
C o A| c
Dod Cod
(A) A o B | a | CBD The number of canonical collections of
B oC |b CLR(1) items which are having the same
C o A| c reductions with different lookaheads is
Dod (A) 0 (B) 1
(B) A o aA ' | bA ' | cA ' | cBDA ' (C) 2 (D) 3
A ' o H | BDA ' Q.12 Consider the following context-free
B oC |b grammar, the symbols ( , a , ) and , are
C o A| c terminals and S is the initial symbol.
Dod S o ( L)
(C) A o aA ' | bA ' | cA '
S oa
A ' o H | BDA '
L o L, S
B oC |b
C o A| c LoS
Dod The closure of the LR(1) item
(D) A o aA ' | bA ' | cBDA ' [ S o (˜ L)[$] is

Compiler Design 3
(A) S o (. L)|$ Q.14 Consider the following grammar
L o . L, S |) [MSQ]
L o . S ,|) SoA
(B) S o (. L)|$ A o BC | DBC

L o . L, S |) B o Bb | H

L o . S ,|) C oc|H

S o .( L) | ) Doa|d

S o .a | ) Which of the following is part of the


FIRST(A) is
(C) S o (. L) ,$
(A) {a, b} (B) {c, d }
L o . L, S , )
(C) {a, b, c, d , H} (D) {a, d}
L o . S, )
S o .( L), ) Q.15 Compiler can check _____ error
S o . a, ) [MSQ]
(A) Logical (B) Syntax
(D) S o (. L)|$
(C) Semantic (D) All of them
L o . L, S | )
Q.16 Consider the following SDT
L o . S, | )
A o b {print(" a ")}A
L o .L, S |,
A o a {print("b ")}A
L o . S |,
A o c {print(" d ")}
S o .( L) | )
The output produced by the SDT for the
S o .a | )
input string bbac
S o .( L) | ,
(A) aabd (B) abda
S o .a | , (C) dbaa (D) None of these
Q.13 Consider the following grammar with Q.17 Consider the following context free
the semantic rules grammar
S o ABBA
Aoa|H
B ob|H
The entries in the following LL(1) parse
table M is
a b $
S S o ABBA S o ABBA S o ABBA
The output produced by the SDT for the A
input string "3*4  5*2" is _____ B B ob
(A) 34* 52  (B) 34*52* The entries for the
(C) 34  52  * (D) 34  *52  M [ A, a], M [ A, b], M [ A,$] is

4 Compiler Design
(A) M [ A, a] A o a, Which of the above statement(s) is/are
M [ A, b] A o H, incorrect?
M [ A,$] AoH (A) Only I (B) Only II
(B) M [ A, a] { A o a, A o H} (C) Both I and II (D) Neither I nor
M [ A, b] A o H, II
M [ A,$] AoH Q.21 Consider the following Grammar
(C) M [ A, a] A o a, SoX
M [ A, b] A o H, X o Yb | aa

M [ A,$] Nil Y o a | bYa

(D) M [ A, a] { A o a, A o H} The given grammar is


M [ A, b] A o H, (A) LR(0) and SLR(1)
M [ A,$] Nil (B) Not LR(0) but SLR(1)
(C) LR(0) but not SLR(1)
Q.18 Given the following expression
grammar: (D) Neither LR(0) nor SLR(1)
EoE F|FE|F Q.22 Consider the following syntax directed
F o F  F | id translation scheme.
The output produced by the expression E o E1 *T {E.val E1.val *T .val}
grammar after evaluating the expression E o T {E.val T .val}
5  3*4  2
T o F  T1 {T .val F .val  T1.val}
(A) 16 (B) 10
(C) 4 (D) None of these T o F {T .val F .val}
Q.19 In a bottom-up evaluation of a syntax F o 3{F .val 2}
direction definition, inherited attributes F o 5{F .val 4}
can The output produced by the SDTS after
(A) Always be evaluated evaluating the given expression is
(B) Be evaluated only if the definition is 5  3*5*3.
L-attributed Assume attribute evaluation with
(C) Evaluation only done if the bottom-up parsing, i.e., attributes are
definition has synthesized attributes evaluated immediately after a reduction.
(D) None of these (A) 15 (B) – 42
Q.20 Consider the following statements
(C) 30 (D) None of these
related to compiler construction:
I. Lexical Analysis is specified by Q.23 Consider the following context free
context-free grammar and grammar
implemented by pushdown SoP
automata. P o ( P) P
II. Syntax Analysis is specified by
regular expressions and PoH
implemented by finite-state In the LL(1) parse table M, the entries
machine. for M [ S ,$] M [ P, )] are

Compiler Design 5
(A) S o P, P o H B o bB | aaC | bbC | a | b
(B) S o H, P o H C o aaC | bbC | a | b
D oa|b
(C) P o H, P o ( P) P
(D) None of these
(D) None of these
Q.26 Which one of the following statements
Q.24 Given the CFG
is FALSE?
G ^S ,{S ,U ,V ,W },{a, b, c, d ), P` with (A) Context-free grammar can be used to
P given as shown below: specify both lexical and syntax rules.
S o UVW (B) Type checking is done before
U o ( S ) | aSb | d parsing.
V o aV | H (C) High-level language programs can
W o cW | H be translated to different Intermediate
Representations.
Then Follow (U )
(D) Arguments to a function can be
(A) {a, c} (B) {a, c, b, )} passed using the program stack.
(C) {a, c,$, b} (D) {a, c,$, b, )} Q.27 Consider the following parse tree for the
Q.25 Consider the following context free expression a # b$c$d # e # f , involving
grammar two binary operators $ and #.
S o bAB | bb | C #
A o BC | aCB | H | a
B o bB | C | H a #
C o aaC | bbC | D
C oa|b $ #
The Equivalent simplified (After
Elimination of Unit, Null, Useless e
$ d f
Symbols) from the grammar is
(A) S o bAB | bb | aaC | bbC | a | b
A o BC | aCB | a b c

B o bB | aaC | bbC | a | b Which one of the following is correct for


C o aaC | bbC | a | b the given parse tree?
D oa|b (A) $ has higher precedence and is left
associative; # is right associative
(B) S o bAB | bb | aaC | bbC | a | b
(B) # has higher precedence and is left
A o BC | aCB | a
associative; $ is right associative
B o bB | aaC | bbC | a | b (C) $ has higher precedence and is left
C o aaC | bbC | a | b associative; # is left associative
(C) S o bAB | bb | aaC | bbC (D) # has higher precedence and is right
A o BC | aCB | a associative; $ is left associative

6 Compiler Design
Q.28 The attributes of three arithmetic The FIRST and FOLLOW sets for the
operators in some programming non-terminals A and B are
languages are given below: (A) FIRST( A) {a, b, H} FIRST( B)
Operator Precedence Associativity Arity FOLLOW( A) {a, b}
+ High Left Binary FOLLOW( B) {a, b,$}
– Medium Right Binary
(B) FIRST( A) {a, b,$}
* Low Left Binary
FIRST( B) {a, b, H}
The value of the expression FOLLOW( A) {a, b}
2  5  1  7*3 in this language is
FOLLOW( B) {$}
(A) 9 (B) 10
(C) FIRST( A) {a, b, H} FIRST( B)
(C) 11 (D) 12
Q.29 Which of the following statements are FOLLOW( A) {a, b}
CORRECT? FOLLOW( B) I
1. Static allocation of all data areas by (D) FIRST( A) {a, b} FIRST( B)
a compiler makes it impossible to FOLLOW( A) {a, b}
implement recursion. FOLLOW( B) {a, b}
2. Automatic garbage collection is
Q.31 The appropriate entries for
essential to implement recursion.
E1 , E2 and E3 are
3. Dynamic allocation of activation
records is essential to implement (A) E1 : S o aAbB, A o S
recursion. E2 : S o bAaB, B o S
4. Both heap and stack are essential to E3 : B o S
implement recursion.
(A) 1 and 2 only (B) 2 and 3 only (B) E1 : S o aAbB, S o H
(C) 3 and 4 only (D) 1 and 3 only E2 : S o bAaB, S o H
Q.30 For the grammar below, a partial LL(1) E3 : S o H
parsing table is also presented along
(C) E1 : S o aAbB, S o H
with the grammar. Entries that need to
be filled are indicated as E1 , E2 and E3 . E2 : S o bAaB, S o H
H is the empty string, $ indicates end of E3 : B o S
input, and 1 separate alternate right hand (D) E1 : A o S , S o H
sides of productions. E2 : B o S , S o H
S o aAbB | bAaB | H
E3 : B o S
AoS
BoS Q.32 The program below uses six temporary
variables a, b, c, d, e, f.
A b $
a=1
S E1 E2 S oH
b = 10
A AoS AoS error c = 28
B BoS BoS E3 d=a+t

Compiler Design 7
e=c+d S2 : With triples representation
f=c+e optimization can change the execution
b=c+e order.
e=b+f Which of the above is correct?
d=5+e (A) Only S1
return d + f (B) Only S2
Assuming that all operations like their (C) Both S1 and S2
operands from registers, what is the (D) None of these
minimum number of registers needed to Q.36 Consider the following grammar.
execute this program without spilling? X o YZ ^Z .x X .x
(A) 2 (B) 3 X . y Y . y`
(C) 4 (D) 6
Z o PZ '^Z '.x P.x
Q.33 Which of the following statements are
TRUE? Z . y P.x  Z ', y`
I. There exist parsing algorithms for Which of the following is true?
some programming languages (A) Both x and y are inherited attributes
whose complexities are less than (B) Both x and y are synthesized
attributes
T n3 .
(C) x is inherited and y is synthesized
II. A programming language which
(D) x is synthesized and y is inherited
allows recursion can be
Q.37 Choose the correct sequence of
implemented with static storage.
occurrence during compilation process
III. No L-attributed definition can be
(A) Parse tree o Token stream o
evaluated in the framework of
intermediate code
bottom-up parsing. (B) Parse tree o 3 address code o
IV. Code improving transformations can character stream
be performed at both source (C) Character stream o Parse tree o
language and intermediate code SDT tree
level. (D) Token stream o SDT tree o Parse
(A) I and II (B) I and IV tree
(C) III and IV (D) I, III and IV Q.38 Consider the following code.
Q.34 FOLLOW (S) set for the postfix main ()
grammar after removing left recursion {
S o SS  | SS *| a . int temp 200, 10;
(A) {a,} int l1, l 2;
(B) {,*} temp   temp;
(C) {a,$} l1 l 2
(D) None of the above printf "%d ", temp + l1 ;
Q.35 Consider the following statement: }
S1 : Three address code is linear The number of tokens in the above code
representation of syntax tree is______.

8 Compiler Design
Q.39 Consider the regular expression with the S3: During a program execution, heap is
respective token number in the table. stored in main memory and stack is
REX Token No. present in secondary memory.
a  b *c 1 Which of the above is incorrect?
2 (A) Only S1 and S2
ca * b
3 (B) Only S2 and S3
c*
(C) All of these
Choose the correct output when lexical
(D) Only S3
analyzer scans the following input:
Q.42 Consider the basic block given below:
“cabacccab” Note: The analyzer tries to
X o X *Y
output the token that matches the longest
Z o X Z
possible prefix.
PoZ /P
(A) 3122
X oZ P
(B) 2132
Minimum number of edges present in
(C) 1132 the DAG representation of the above
(D) Generates lexical error block is________.
Q.40 Consider the following translation Q.43 Consider the following statement:
scheme: I. Three address code is a linearized
S o XY representation of syntax tree.
X o X *Y > Print[ '*' @ II. Type checking is done during all the
phases especially in syntax analysis
X o id >Print id@ phase.
X o id > Print  @ III. Target code generation phase is
Y o Y > Print  @ machine independent code
generation
Y o id ^Print id ` IV. Symbol table is accessed during
Here id is a token which represent on lexical, syntax and semantic analysis
integer id represent the value of that phase.
integer. For an input 6* 4*5  7 , this The number of the correct statement
translation scheme prints. is/are________.
(A) 64*5*7  (B) 6*4*5  7 Q.44 In SLR parsing for the grammar.
(C) 64*5*7  (D) 64*5  *7  EoE
Q.41 Consider the following statements: E o aEbE \ bEaE \ 
S1: While program in execution, access In state 0, for input ‘a’ and ‘b’
to heap memory is slower as compared (A) Both will have shift reduce conflict
to accessing variables allocate on stack. (B) Only ‘a’ will have shift reduce
S2: While program in execution, in a conflict
multithread situation, each threads has (C) Only ‘b’ will have shift reduce
its own stack and share a common heap conflict
memory. (D) Neither of the other options

Compiler Design 9
Q.45 Which of the following statements is (A) A o ab (B) A o a
true? (C) S o A (D) B o ab
(A) S o aabc/ab, this grammar is not Q.49 Which of the following statements is
LL(1) but it is LL(2). FALSE?
(B) Every regular language is LL (1)
(A) In a SLR(1) parser, it is allowable for
(C) Every regular grammar is LL (1)
both shift and reduce items to be in
(D) Both (a) and (b)
the same state
Q.46 Consider the following grammar G1 and
(B) In a SLR(1) parser, it is allowable for
G2 with S, A, B, C as non-terminals and
multiple reduce items to be in the
a, b, c  as terminals.
same state
G1 : S o A  B | A | B | AB
(C) All SLR(1) grammars are LR(0)
A o A*C | a
(D) All LR(0) grammars are SLR(1)
B o BC |b
Q.50 Which of the followign statements
C oc
regarding LR(0) parser is FALSE?
G2 : S o A * B | (A) A LR(0) configurating set cannot
S o B C have multiple reduce items
Aoa (B) A LR(0) configurating set cannot
B ob have both shift as well as reduce
C oc items
Which of the above grammar is operator (C) If a reduce item is present in a LR(0)
grammar? configurating set it cannot have any
(A) Only G1 other item
(B) Only G2 (D) A LR(0) parser can parse any regular
(C) Both G1 and G2 grammar
(D) None of these Q.51 Which of the following sentences is
Q.47 If we merge states in LR(1) parser to CORRECT?
form a LALR(1) parser, we may (A) A top-down parse produces a
introduce leftmost derivation of a sentence
(A) shift-reduce conflict
(B) A bottom-up parse produces a
(B) reduce-reduce conflict rightmost derivation of a sentence
(C) no extra conflict
(C) A top-down parse produces a
(D) both shift-reduce as well as reduce-
rightmost derivation of a sentence
reduce
(D) A bottom-up parse produces a
Q.48 Suppose we have a rightmost derivation
leftmost derivation of a sentence
which proceeds as follows:
S o Aabw Q.52 Which of the following is TRUE
o ABw regarding (LL0) grammar?
Which of the following is a possible (A) We can have a LL(0) grammar for
handle for it? any regular language

10 Compiler Design
(B) We can have a LL(0) grammar for a Q.57 Find the number of SR and RR conflicts
regular language only if it does not in DFA with LR (0) items S o SS | a |
contain empty string (A) 2, 2 (B) 2, 1
(C) We can have a LL(0) grammar for (C) 3, 1 (D) 3, 2
any regular language if and only if it Q.58 Consider the syntax direction definition
has prefix property shown below
(D) We can have a LL(0) grammar for N o L {N. val = L. val;}
only single string languages L o LB {L.val = 2 * L.val + B. val;}
Q.53 Match the following :
|B {L. val = B. val;}
(i) LL(1) (a) bottom-up
B o 0 {B.val = 0;}
(ii) Recursive Descent (b) Predictive
| 1 {B.val= 1; }
(iii) Recursive Ascent (c) Top-down
If the input is 1010101 then its output
(iv) LR(1) (d) Deterministic are:-
CFL
(A) i-b; ii-c; iii-a; iv-d
(B) i-d; ii-a; iii-c; iv-d
(C) i-c; ii-b; iii-d; iv-a
(D) i-a; ii-c; iii-b; iv-d
Q.54 Which of the below relations does hold
TRUE regarding GRAMMARS?
(A) LL(1)  SLR(1)  LR(1)
(B) SLR(1)  free LL(1)  LR(1)
(C)  free LL(1)  SLR(1)  LR(1)
(D) LL(1)  SLR(1) LR(1)
Q.55 The worst case space complexity of
operator function table and operator
relation table is?
(A) O (n) & O (n)
(B) O (n2 ) & O (n2 )
(C) O (n) & O (n2 )
(D) O (n2 ) & O (n)
Q.56 Consider the grammar shown below
E o E T / T
T o TF / F
F oF* a b
The minimum number of states are
required in SLR (1) parsing table is____.

Compiler Design 11
Answers Compiler Design

1. A 2. D 3. C 4. 34 5. A
6. 7 7. D 8. A 9. B 10. D
11. C 12. C 13. A 14. A, B, C, D 15. B, C
16. A 17. B 18. A 19. B 20. C
21. D 22. D 23. A 24. D 25. D
26. B 27. A 28. A 29. D 30 A
31. C 32. B 33. B 34. B 35. A
36. C 37. C 38. 34 39. D 40 C
41. D 42. 8 43. 2 44. A 45. D
46. D 47. B 48. D 49. C 50. D
51. A 52. D 53. A 54. C 55. C
56. 10 57. B 58. 85

Explanations Compiler Design

1. (A) E’ → or bterm E’ | H
Unambiguous grammar has both kinds of bterm → bfactor F’
derivation : False F’ → and bactor F’ | H
In Unambiguous grammar both LMD and RMD bfactor → not bfactor | (bexpr) | true |
generates the unique parse tree for the given false
input string 4. 34
2. (D) int main() –4
Number of states in a CLR parser table greater { –1
than equal LALR(1) parse table. int x, y, total; –7
x 10, y 20; –8
total x  y; –6
printf ("Total %d \n ", total);
CLR LALR SLR LR(0) –7
} –1
Total number of tokens are
4  1  7  8  6  7  1 34
3. (C) 5. (A)

Non-left recursive grammar for the given Parser is a part of compiler and responsible for
grammar is syntax recognition. Scanner (or tokenization)
bexpr → bterm E’ used by the lexical analyser. In Semantic

12 Compiler Design
analysis consistency and definition of syntax is s2 :
checked. An optimizer is used improve the IR [SL  ! SL;.S]
program. [S  ! .stmt] goto (s2, S) s3;
Hence, the correct option is (A).
s3 :
6. 7 [SL  ! SL; S.] goto
Static Single Assignment is used for (s2, stmt) s3;
intermediate code in compiler design, In static s4 :
single Assignment form (SSA) each assignment [S  ! stmt . ] goto (s2, stmt) s4;
to a variable should be specified with distinct
The given grammar is LR(0) and SLR(1)
names. We use subscripts to distinguish each
SLR(1) parse table is
definition of variables.
In the given code segment each definition is stmt S SL S
distinct. s0 r2 r2
So, the total number of variable is s1 s2 Accept
( x, u, t , y, v, z, w). s2 s4 3
7. (D) s3 r1 r1
s4 r3 r3
The given grammar is
Numeral : := Digits / Digits. Digits / Digits e 9. (B)
Sign Digits / Digits. Digits e
Given grammar is
Sign Digits
A o B | a | CBD
Digits : := Digit Digits | Digit
B oC |b
Digit : := 0 | 1 | 2 | 3
C o A| c
Apply Left Factoring
Numeral : := Digits N1 N2 Dod
N1 : := e Sign Digits | H It is having the indirect recursion.
N2 : := Digits | H A o C | b | a | CBD
Digits : := Digit D B oC |b
D : := Digits | H C o A| c
Digit : := 0 | 1 | 2 | 3 Dod
8. (A) o A o A | c | b | a | cBD | ABD
LR(0) Items : No conflicts in LR(0), SLR(1) B oC |b
s0 : C o A| c
[S'  ! .SL] Dod
[SL  ! .SL; S] Ÿ A o A | ABD | a | b | c | cBD
[SL  ! .] goto [s0, SL] s1; B oC |b
s1 : C o A| c
[S'  ! SL. ]
Dod
[SL  ! SL. ; S] goto (s1, ;) s2; o A o aA ' | bA ' | cA ' | cBDA '

Compiler Design 13
A ' o H | BDA ' 12. (C)
B oC |b
I0
C o A| c I1
s' o .s |$ s
Dod s o .(L) |$ s' o s. |$

10. (D) s o .a |$
ghghgh
As R is the start symbol of the grammar, add 12 ASADaedewe
{$} to the following set.
s o(.L) |$
Follow(R) {$}u {|,*, )} L o .L,S|)
R o RR L o .Ls,|)
Follow(R) First(R) L o.s,|)

First(R) {(, a, b} s o .(L) |)


s o .a |)
Ÿ Follow(R) {(, a, b,|,*, ),$} s o .(L) |)
11. (C) s o .a |)

Augmented grammar for the given grammar is 13. (C)


Let, r1 S o CC, r2 C o cC and E 34+52+*
r3 C o d
S'oS
S o CC 34+E  T 52+
C o cC
Cod
I1 34+ T T * F
I0 s ' o s.$
S
s' o .S,$
s' o .CC,$ I3
C o .cC,c |d c o c.C,c |d C c o cC., c | d I8
c
C o .d,c |d c o cC,c|d c
C c o .d,c |d d I3 T * F F
I2 d I4
I4
s o C.C,$
c o d., c | d Num=4 Num=5
C o .cC,c |d F
C o .d,$
c I6
c o cC
. ,$ C c o cC.,$ I9
C
c o.cC,$ c
Num=3
I5 d c o d.,$ I6
s o C.C,$ d
I7 14. A,B,C,D
c o d.,$ I7
First(A) First(B) u First(D)
14 and 17 contain the same reductions (r3 ) with
First(D) {a, d}
different lookaheads.
First(B) {H}
18 and 19 contain the same reductions (r2 ) with H in place of B
Substitute
different lookaheads. First(B) {b}

14 Compiler Design
Ÿ First(A) First(C) {c, H} Follow( B) First( B) {b, a,$}

Substitute H in place of C LL(1) parse table is


ŸFirst(A) {H} a b $
ŸFirst(A) {a, b, c, d , H} S S o ABBA S o ABBA S o ABBA
First(A) contains all the symbols {a, b, c, d , H}
A A o a, A o H AoH AoH
All the options are correct.
15. B,C
B B oH B o b, B o H B oH
(B) Compiler will recognize all the syntax
and semantic errors in the code, May not
detect the logical errors. 18. (A)

(C) Compiler will recognize all the syntax The parse tree for the given string is 5  3*4  2
and semantic errors in the code, May not E
detect the logical errors.
16. (A)
E * F
Given input string is bbac and the output
produced by the SDT is aabd F E F F
 <
A

id F id id
b A
"a "
b A id
"a "
The output is (5  3) *(4  2) 8* 2 16
a A
"b " 19. (B)
"d "
c
A Syntax Directed Definition (SDD) is called S
Attributed if it has only synthesized attributes L.
17. (B) Attributed Definitions contain both synthesized
First(S) {a, b, H} and inherited attributes but do not need to build
a dependency graph to evaluate them.
First(A) {a, H}
20. (C)
First(B) {b, H}
Both the statements are Incorrect. Lexical
Follow(S) {$}
Analysis is specified by the Regular Expression
First( B) {b} ‰ First( B) and implemented by the finite state-machine
Follow(A) {b} ‰ First( A) and Syntax Analysis is specified by the CFG
{b, a} ‰ follow(S ) (a, b,$} and implemented by the PDA.

Compiler Design 15
21. (D) Ÿ F o 3{F .val 2}
F o 5{F .val 4}
2 s o X. 3 X o a.a a
Y oa
4 X o Y.b (((4  2) * 4) * 2) Ÿ 16
X
1 s o .X
a 23. (A)
s o .Yb
Y
Start
X o .aa 5 X o Y.b
b
6 X o Y.b First(S) {(, H}
Y o .bYa
Y o .a First(P) {(, H}
b 8 Yoa Follow(S) {$}
a
Y o bYa
.
7
Y o bYa
.
Follow(P) {), $}
Y o .a Y
9 Y o bY.a
a
10 Y o bYa
( ) $
b
S SoP SoP
State 13 contains S/R conflict in LR(0). Apply P P o ( P) P PoH PoH
reduction operation on state 13 and shift
operation for the input symbol ' a '. 24. (D)
Need to check for the S/R conflict in SLR(1) Follow(U) First(V ) {a, H}
First(a) intersection Follow(Y) {a} Ÿ {a} ‰ First(W ) {a} ‰{c, H}
intersection {a, b} {a} z ) Substitute {H} in place of W
S/R connect in SLR(1). Ÿ {a} ‰ {c}Follow( S )
The given grammar is neither LR(0) nor Ÿ {a, c,$, b, )}
SLR(1).
25. (D)
22. (D)
S o bAB | bb | C
Given string is 5  3*5*3 A o BC | aCB | H | a
E B o bB | C | H
C o aaC | bbC | D
D oa|b
E * T
Eliminate Null productions
S o bAB | bb | C | bB | bA | b
E T
* F A o BC | aCB | a | C | aC
B o bB | C | b
T F C o aaC | bbC | D
3
D oa|b
F < T Eliminate the Unit productions
5
S o bAB | bb | aaC | bbC | a | b | bB | bA | b
A o BC | aCB | a | aaC | bbC | a | b | aC
5 F B o bB | aaC | bbC | a | b | b
C o aaC | bbC | a | b
D oa|b
3
Eliminate Useless Symbols
(((5  3) *5) *3)
S o bAB | bb | aaC | bbC | a | b | bB | bA | b

16 Compiler Design
A o BC | aCB | a | aaC | bbC | a | b | aC FIRST(P): is the set of terminals that begin the
B o bB | aaC | bbC | a | b | b strings derivable from non-terminal P. If P
C o aaC | bbC | a | b derives epsilon, then we include epsilon in
FIRST(P).
26. (B)
FOLLOW(P): is the set of terminals that can
Type checking is done in semantic analysis appear immediately to the right of P in some
phase after syntax analysis phase (i.e., after sentential form.
parsing). FIRST(A) FIRST(S)
27. (A) FIRST(S) FIRST(aAbB) and FIRST(bAaB)
Since $ will be evaluated before # so $ has and FIRST ()
higher precedence and the left $ i.e., in b$c$d FIRST(S) {a, b,}
the left “$” (i.e., b$c) will be evaluated first so FIRST(B) FIRST(S) {a, b,} FIRST(A)
it is left associative, whereas # is right
FOLLOW(A) {b} // because of production
associative (as in d # e # f ), the right one (i.e., e
S o a Ab B
# f) will be evaluated first.
FOLLOW(A) {a} // because of production
28. (A)
S o b Aa B
+ has highest precedence, so it will be evaluated So, FOLLOW(A) {a, b}
first.
FOLLOW(B) FOLLOW(S) // because of
2  5  1  7*3 2  (5  1)  7*3
production S o a Ab B
2  6  7*3
FOLLOW(S) FOLLOW(A) // because of
Now, - has more precedence than *, so sub will production S o A
be evaluated before * and – has right associative
So, FOLLOW(S) {$, a, b} FOLLOW(B)
so (6-7) will be evaluated first.
2  6  7*3 (2  (6  7))*3 31. (C)
(2  (1))*3
The entries in E1 , E2 and E3 is related to S and
3*3 9
B, so we have to take only those production
29. (D) which have S and B in LHS.
The statement, static allocation of all data areas S o aAbB | bAaB | H
by a compiler makes it impossible to implement The production S o aAbB will go under
recursion is true, as recursion requires memory column
allocation at run time, so it requires dynamic
allocation of memory. FIRST(aAbB) a, so S o aAbB will be in E1.
Hence, Dynamic allocation of activation records S o bAaB will go under column
is essential to implement recursion is also a true FIRST(bAaB) b, so S o bAaB will be in
statement.
E2 .
30. (A)
S o H will go under

Compiler Design 17
FOLLOW( S ) FOLLOW( B) {a, b,$}, So neither of 'e' or ‘ c’ is required. Value of 'b' is
E1 , E2 and under column of $. required because we need to return 'd + f', and
S o H will go in
‘f’ is essentially equal to 'b'
So E1 will have: S o aAbB and S o H. finally, code becomes
E2 will have S o bAaB and S o H. r1 = 1
r2 = 10
Now, B o S will go under
r3 = 20
FIRST( S ) {a, b, H}
r1 = r1 + r2
Since FIRST( S ) H So B o S will go under
r1 = r3 + r1
FOLLOW( B) {a, b,$}
r2 = r3 + r1
So E3 will contain B o S . r2 = r3 + r1
32. (B) r1 = r2 + r2
r3 = 5 + r1
Here a, b, and c all have 3 different values so
return r3 + r2
we need at least 3 registers r1, r2 and r3.
Therefore minimum 3 registers needed.
Assume 'a' is mapped to r1, ‘b' to r2 and 'c' to
13. 33. (B)
d = a – b, after this line if u notice 'a' is never Statement II is false, as a programming
present on right hand side, so we can map 'd' to language which allows recursion requires
r1. dynamic storage allocation. Statement III is
e = c + d, after this line 'd' is never present on false, as L-attributed definition (assume for
rhs, so we can map 'e' to r1. instance the L-attributed definition has
at this time mapping is synthesized attribute only) can be evaluated in
r1________e bottom-up framework.
r2________b Statement I is true, as the bottom-up and top-
r3________c down parser take O (n) time to parse the string,
We have 3 registers for a, b and c. i.e. only one scan of input is required.
f=c-e Statement IV is true, Code improving
b=c+e transformations can be performed at both source
These two are essentially doing same thing, language and intermediate code level. For
after these two line ‘b' and T are same so we can example implicit type casting is also a kind of
skip computing 'f’ or need not give any new code improvement: which is done during
register for 'i'. And wherever 'f’ is present we semantic analysis phase and intermediate code
can replace it with 'b', because neither of ‘f’ and optimization is a topic itself which uses various
‘b’ are changing after these two lines, so value techniques to improve the code such as loop
of these will be "c + e’ till the end of the unrolling, loop invariant.
program. 34. (B)
At second last line "d = 5 - e" (after removing left recursion)
Here 'd’ is introduced, we can map it to any of S oS'
the register r1 or r3, because after this line

18 Compiler Design
S ' o S  S '/ S * S '/ a /  39. (D)
FIRST S ^a,` c ab a c c c ab
FIRST S ' ^a, `
2 1 3 not generated

Hence, lexical error will generate.


FOLLOW S ^, *` 40. (C)

FOLLOW S ' ^ ,*` S

35. (A) X Y
S1 is correct
With triple optimization we cannot change the . + Y
X Y
execution order but with indirect triple we can.
id id
36. (C)
5 7
x is inherited
y is synthesized. . Y
X
37. (C)
id id
Lexical analyzer o Syntax analyzer o 6 4
Semantic analyzer o intermediate code o Output: 64*5*7 
Code optimizer. 41. (D)
38. 34 Statement S1 and S2 are correct
main ( ) Statement S3 in incorrect. Heap and stack both
1 2 3 are present in main memory
{ 42. 8
4 +
int temp 200, 10 ;
f
5 6 7 8 9 10 11
int l1 , l 2 ; + P

12 13 14 15 16 u Z
temp   temp ;
X Y
17 18 19 20 21
Total 8 edges
l1  l2
22 23 24 43. 2

printf ("%d ", temp  l1) ; Statement I and IV is correct


25 26 27 28 29 30 31 32 33 Type checking is done at semantic analysis
phase
}
Target code generation is dependent based on
34
the machine

Compiler Design 19
Symbol table is accessed during lexical, syntax 48. (D)
and semantic analysis phase.
Handle is part of the string in sentential form
44. (A) that will be reduced to non-terminal i.e left hand
side of a production
E o a.EbE
In the above derivation, sentential form Aabw is
State 0 a E o .bEaE |
.aEbE reduced to ABw so has to be a production with
E' oE B o ab and that is the handle at this point of
E o .aEbE derivation.
.bEaE
. b 49. (C)
E o b.EaE
E o . aEbE 1. In a SLR(1) parser, it is allowable for
.bEaE both shift and reduce items to be in the
. same state even though it leads to sr
In state 0, there is reduce E o which will go conflict but it is allow
2. In a SLR(1) parser, it is allowable for
under of ‘E’ which ^a, b` and also at state 0,
multiple reduce items to be in the same
There is shift at ‘a’ and ‘b’. Hence, there is shift state even though it leads to sr conflict
reduce conflict. but it is allow
45. (D) 3. All SLR(1) grammars are LR(0) this
statement is wrong Reason is
S o aabc | ab
LR(0) <SLR(1) < LALR(1) < CLR(1)
There is left factoring in LL (1). Hence, not LL → If a grammar is LR(0) then it is also
(1) but it is LL (2). SLR(1), LALR(1), CLR(1).
Every regular language is LL (1) is true. There → If a grammar is SLR(1) then it is also
exist a regular grammar which is LL (1). LALR(1), CLR(1).
Every regular grammar is LL (1) is false, → If a grammar is LALR(1) then it is also
because regular grammar may contain left CLR(1).
recursion, left factoring ambiguity. 4. All LR(0) grammars are SLR(1)
Therefore C is incorrect.
46. (D)
50. (D)
A grammar G is said to be operator grammar if
Since LR(0) parser places reduce-moves in the
(a) it does not contain null production
entire row of “Action”, having anything more
(b) it does not contain 2 adjacent variables than just the reduce-move in the state having
on right hand side final-item would lead to SR or RR conflict.
So, both G1 and G2 are not operator grammar. So, Options A, B, and C are true.
47. (B) 51. (A)
To go from CLR(1) parsing table to LALR(1) A top-down parse produce a leftmost derivation
parsing table, we merge the states that have the of a sentence.
same final items but different lookaheads. A bottom-up parse produces a rightmost
derivation of a sentence but in reverse.
In doing so, we can only introduce RR conflicts.

20 Compiler Design
52. (D) LR(1) is bottom-up or DCFL, since LR(K)
accepts DCFL
LL(0) grammars have no lookhead. And since
they follow Leftmost derivation, at each step the 54. (C)
parser has to derive the string by seeing 0  free LL(1)  SLR(1)  LR(1)
symbols ! Parser sees nothing. Because every  free LL(1) are SLR(1) and
So whenever we have multiple choices for any every SLR(1) are LR(1)
Variable in the grammar, LL(0) fails.
55. (C)
Hence, LL(0) parser can only parse grammars
that strictly generate one single string. In operator relation table if we have 4 operator
then we got is 16 cells so if we have n operator
53. (A)
then n 2 is table size.
LL(1) is a top-down or predictive parser But in operator function table if we have 4
REC Decent is predictive operators then we got 8 cells so if we have n
REC Ascent is a technique for implementing an operator then we got 2n table size.
LALR parser so Bottom up So the option C is the correct option
56. 10

I4 I5
I7
F a b
I9 E o E  T
I6
T T o T .F
E o E  .T F o .F * /.a / .b
I1 T o .TF / .F
T b
I0 E ' o E. F o .F * /.a / .b I5
E E ' o E.  T I7 I
E ' o.E I2 F 3
E o. E  T T a I4
E o T. F T o TF .
/.T T o T .F F o F .*
T o.TF E o .F * I8
/.a / .b
/.F F I3
F o .F * T o F. F o F *.
/.a F o F.* *
a
/.b a
b F o a.
T I4
b b
I5
So total 10 states are required
57. (B)

RR conflict:- Means reduce reduce conflict, that means a single state have more than one final
production.
SR conflict means in a state there is a final production and here shift more are also occurred. So we find
RR & SR conflict in our LR (0) item

Compiler Design 21
S o SS | a |

In the above LR (0) item I 0 has a final item and it shift to I 2 So I 0 have one SR conflict
in the state I1 has two Final item so here RR conflict occurred and in I1 have shift move also, it moves
to I 2 So it’s have one SR conflict state
I 2 & I3 have no conflict so total SR conflicts are 2 (1 in I 0 &1in I1 )
And RR conflicts are only 1 (in I1 )
So the option B is correct option.
58. 85
So it is a logic of binary to decimal conversion
L.Val 85 N

L.Val 85 L

B
L.Val 42 L

1
L.Val 21 L B

B 0
L.Val 10 L

B 1
L.Val 5 L

B 0
L.Val 2 L

L.Val 1 L B 1

B 0

1
Finally N.val is 85 so the answer is 85
™™™™

22 Compiler Design

You might also like