CD Question Bank
CD Question Bank
Cd question bank
MODULE 1
1. Explain in detail the various phases of the compiler with a neat diagram.
Illustrate the output of each phase for the input,
sum := a + b * 30
where a and b are float variables. (10)
3. Differentiate tokens, patterns and lexemes with the help of an example. (5)
MODULE 2
MODULE 3
1. Different parsing conflicts in the SLR parsing table?
A shift-reduce conflict occurs in a state that requests both a shift action and a reduce action.A
reduce-reduce conflict occurs in a state that requests two or more different reduce actions.
Write Any example also
S→R
L→*R
L → id
R→ L
Also identify the shift reduce conflict here.
3. Problem from LR/ CLR parsing table construction. Study the method of parsing also
Eg: Derive LR (1) parsing table for following grammar
S → Aa | bAc | Bc | bBa
A→d
B→d
Step1 − Construct Augment Grammar Step 2: LR(1) items
(0) S′ → S
(1) S → A a
(2) S → b A c
(3) S → B c
(4) S → b B a
(5) A → d
(6) B → d
EXAMPLE
Consider the grammar
E E+E
E E*E
E (E)
E id
And the input string is id1 + id2 * id3
The rightmost derivation is:
EE+E
E+E*E
E + E * id3
E + id2 * id3
Id1 + id2 * id3
HANDLE PRUNING
The process of obtaining rightmost derivation in reverse order is called “handle pruning”.
(i.e.) if w is a sentence or string of the grammar at hand, then w = n where n is the nth right
sentinel form of some rightmost derivation.
MODULE 4
1.Write the SDD for a simple type declaration and draw the annotated parse tree for the
declaration int a,c
2.Write the SDD for a desk calculator and draw the annotated parse tree for the expression:
4*5 + 6
Annotated parse tree
The parse tree containing the values of attributes at each node for given input string is
called annotated or decorated parse tree.
3.Define the
following terms and give suitable example for each.
i) Three-address code ii) Triples iii) Quadruples
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
Eg: a=(b*-c)+(b*-c)
t1 = uminus c
t2 = b * t1
t3 = uminus c
t4 = b * t3
t5 = t2 + t4
a = t5
HEAP
ALLOCATION
MODULE 5
1.llustrate the role of register descriptor and address descriptor in code generation phase.
Register and Address Descriptors:A register descriptor is used to keep track of what is currently
in each registers. Theregister descriptors show that initially all the registers are empty.
Write an example
An address descriptor stores the location where the current value of the name can befound at run
time.
Write an example
(Refer print 1)
4.Write the code generation algorithm and getreg function. Using this algorithm generate
code sequence for the expression x = (a –b) + (a + c) + (a + c)
The algorithm takes a sequence of three-address statements as input. For each three address
statement of the form x := y op z perform the various actions. These are as follows:
Code generator uses getReg function to determine the status of available registers
and the location of name values. getReg works as follows:
If variable Y is already in register R, it uses that register.
Else if some register R is available, it uses that register.
Else if both the above options are not possible, it chooses a register that
requires minimal no of load and store. It takes register which is already occupied, move its contents
into some memory using the instruction MOV R, M. and then uses the register R.