1200CST302052302
Scheme of Valuation/Answer Key
(Scheme of evaluation (marks in brackets) and answers of problems/key)
APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY
SIXTH SEMESTER B.TECH DEGREE (R,S)EXAMINATION, May/July 2023(2019 Scheme)
B.Tech Degree S6 (R, S) / S6 (PT) (R) Examination June 2023 (2019 Scheme)
Course Code: CST302
Course Name: COMPILER DESIGN
Max. Marks: 100 Duration: 3 Hours
PART A
Answer all questions, each carries 3 marks. Marks
1 Input buffering definition – 1.5 marks (3)
Relevance in lexical analysis – 1.5 marks
Lexical analyzer will detect the tokens from the source language with the help of input
buffering. The reason is, the lexical analyzer will scan the input character by character,
it will increase the cost file read operation. So buffering is used. The buffer is a pair in
which each half is equal to system read command.
2 Diagram – 3 marks (3)
3 Definition with example – 3 marks (3)
A Grammar G (V, T, P, S) is left recursive if it has a production in the form.
A → A α |β.
The above Grammar is left recursive because the left of production is occurring at a first
position on the right side of production.
Page 1of 12
1200CST302052302
4 Definition – 1.5 marks (3)
Left factoring is a grammar transformation that is useful for producing a grammar
suitable for predictive parsing. The basic idea is that when it is not clear which of two
alternative productions to use to expand a nonterminal “A”, we may be able to rewrite
the “A” productions to refer the decision until we have seen enough of the input to
make the right choice.
Left factoring – 1.5 marks
( )
5 Comparison (any three) - 3 marks (3)
CLR LALR
It is expensive and difficult to It is easy and cheap to implement.
implement.
CLR Parser is the largest in size. As LALR Parser is the largest in size.
the number of states is very large. Because LALR has less number of
states.
Error detection can be done Error detection is not immediate in
immediately in CLR Parser. LALR.
It is very powerful and works on a It is intermediate in power between
large class of grammar. SLR and CLR
6 Actions – Shift, Reduce, Accept, Error - 1.5 marks (3)
Explanation – 1.5 marks
7 t1 = uminus c (3)
t2 = b * t1
t3 = uminus c
t4 = b * t3
t5 = t2 + t4
a = t5
Conversion steps – 2 marks
Final answer -1 mark
8 Definition – 2 marks (3)
Page 2of 12
1200CST302052302
Syntax directed definition specifies the values of attributes by associating semantic
rules with the grammar productions. It is a context free grammar with attributes and
rules together which are associated with grammar symbols and productions
respectively.
Example - 1 marks
E --> E1 + T { E.val = E1.val + T.val}
9 Explanation with example – 3 marks (3)
The expression or sub-expression that has been appeared and computed before and
appears again during the computation of the code is the common sub-expression.
Elimination of that sub-expression is known as Common sub-expression elimination.
1. Local Common Sub-expression elimination– It is used within a single basic block.
Where a basic block is a simple code sequence that has no branches.
2. Global Common Sub-expression elimination– It is used for an entire procedure of
common sub-expression elimination.
Eg:
Before elimination After elimination
x= 10; x = 10;
y = x + 1 * 2; y = x + 1 * 2;
z = x + 1 * 2; w = y + x;
//‟z‟ has common expression as „y‟
w = z + x;
(If example is not given then give 1.5 marks.)
10 Peephole optimization definition – 1.5 marks (3)
Peephole optimization is a type of code Optimization performed on a small part of the
code. It is performed on a very small set of instructions in a segment of code.
Techniques – 1.5 marks
1. Redundant load and store elimination
2. Constant folding
3. Strength Reduction
4. Null sequences/ Simplify Algebraic Expressions
5. Combine operations
6. Deadcode Elimination
(Any three methods)
PART B
Page 3of 12
1200CST302052302
Answer one full question from each module, each carries 14 marks.
Module I
11 a) Phases – 4 marks (8)
Explanation using the given example - 4
b) Definition – 3 marks (1 mark for each) (6)
Tokens- Sequence of characters that have a collective meaning.
Patterns- There is a set of strings in the input for which the same token is produced as
output. This set of strings is described by a rule called a pattern associated with the
token ·
Lexeme- A sequence of characters in the source program that is matched by the pattern
for a token.
Explanation with example – 3 marks
OR
12 a) 1. Parse generator (6)
Page 4of 12
1200CST302052302
2. Scanner generators
3. Syntax-directed
4. Translation engines
5. Automatic code generator
6. Data flow engines.
b) Buffer pairs – 4 marks (8)
A specialized buffering technique can decrease the amount of overhead, which is
needed to process an input character in transferring characters. It includes two buffers,
each includes N-character size which is reloaded alternatively.
Sentinels - 4 marks
A Sentinel is a special character that cannot be part of the source program. Normally we
use „eof‟ as the sentinel. This is used for speeding-up the lexical analyzer.
Module II
13 a) First - 3marks (6)
Follow – 3 marks
S B C D E F
FIRST a c b,∊ g, f,∊ g,f, ∊ f, ∊
FOLLOW $ g, f, h,c g, f, h,c h f, h h
b) Four important strategies (8)
1. Panic mode
2. Phrase level
3. Error productions
4. Global correction
Page 5of 12
1200CST302052302
(2 marks for each)
OR
14 a) Ambiguity proving - 3 marks (7)
If the grammar produces more than one parse tree for the given input string then it is
called ambiguous grammar.
Removal – 4 marks
E-> E+T | E-T |T
T-> T * P | T/P | P
P-> F ^ P | F
F-> ( E ) | id
b) Removing left recursion – 1 mark (7)
First -1 mark
Follow – 1 mark
Parsing table – 2 marks
( ) a $
S S-> (L) S->a
L L->S L->S
->) S
Proof – 2 marks
Here, we can see that there are two productions in the same cell. Hence, this grammar is
not feasible for LL(1) Parser. Although the grammar satisfies all the essential conditions
in the above steps , it is still not feasible for LL(1) Parser.
Module III
15 a) Parser Explanation – 6 marks (8)
Definition
Data structures used
Possible Actions
2 Conflicts – 2 marks (2 marks each)
1. A shift-reduce conflict occurs in a state that requests both a shift action and a
reduce action.
2. A reduce-reduce conflict occurs in a state that requests two or more different
reduce actions.
Page 6of 12
1200CST302052302
b) Steps- 4 marks (6)
Correct answer - 2 marks
OR
16 Augmented grammar -1 mark (14)
LR(1 ) items – 4 marks
Page 7of 12
1200CST302052302
Flow diagram – 2 marks
Parsing table – 3
Parsing of String "bdc" – 4
Module IV
17 a) 2 marks for each (6)
Quadruple – It is a structure which consists of 4 fields namely op, arg1, arg2 and
result. op denotes the operator and arg1 and arg2 denotes the two operands and result is
used to store the result of the expression.
Triples – This representation doesn‟t make use of extra temporary variable to represent
a single operation instead when a reference to another triple‟s value is needed, a pointer
to that triple is used. So, it consist of only three fields namely op, arg1 and arg2.
Indirect Triples – This representation makes use of pointer to the listing of all
references to computations which is made separately and stored.
Eg:-
Page 8of 12
1200CST302052302
t1 = uminus c
t2 = b * t1
t3 = uminus c
t4 = b * t3
t5 = t2 + t4
a = t5
b) Stack allocation – 4 marks (8)
The stack allocation is a runtime storage management technique. The activation records
are pushed and popped as activations begin and end respectively.
Heap allocation -4 marks
It enables the allocation of memory in a Non-nested design. Storage can be allocated &
freed arbitrarily from an area known as Heap.
Page 9of 12
1200CST302052302
OR
18 a) SDD -3 marks (7)
Production Semantic Rule
S -> E S.val=E.val
E -> E1 + E2 E.val=E1.val+E2.val
E -> E1 * E2 E.val=E1.val*E2.val
E -> - E1|E2 E.val=E1.val
E.val=E2.val
E -> (E1) E.val=E1.val
E -> id E.val=id.lexval
Annotated parse tree - 4 marks
b) Translation scheme – 3 marks (7)
Intermediate code - 4 marks
Syntax directed translation scheme for if E then S1 else S2:
E.true:= newlabel;
E.false:=newlabel;
Page 10of 12
1200CST302052302
S1.next:=S.next;
S2.next:=S.next;
S.code:=E.code || gen(E.true “:”) || S1.code || gen(„goto‟ S.next) || gen(E.false
“:”) || S2.code
Intermediate code generated:
if a>b got L1
goto L2
L1: t1:=inttoreal(b)
x:=a+t1
goto L3
L2: t2:=inttoreal(b)
x:=a-t2
L3:
Module V
19 a) 4 marks for each code optimization technique. (8)
b) Three address code – 2 marks (6)
Code sequence – 4 marks
OR
20 a) Input to the code generator (7)
Target programs
Memory management
Instruction selection
Page 11of 12
1200CST302052302
Register allocation
Choice of evaluation order
Approaches to code generation
b) 1. Structure preserving transformations (7)
2. Algebraic transformations
(3.5 marks for each)
****
Page 12of 12