CSC376 · TU past paper
Compiler Design and Construction 2075 question paper
The complete TU 2075 exam paper for Compiler Design and Construction (CSC376), all 10 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- Q1.6 marksBasic concepts related to Compiler such asHideAnswer
Difference between compiler and interpreter. "Symbol table is necessary component of compiler". Justify this statement with examples.[6]
- Q2.6 marksNumericalFinite Automata relevant to compiler constHideAnswer
List out the major tasks carried out in Lexical Analysis Phase. Convert the following NFA to DFA.[6]
- Q3.6 marksNumericalTop-down parsingHideAnswer
Differentiate between recursive descent and non-recursive predictive parsing method. Find first and follow of all the non-terminals in the following grammar.E→TA;A→+TA∣ε;T→FB;B→∗FB∣ε;F→(E)∣idE \rightarrow TA; A \rightarrow +TA|\varepsilon; T \rightarrow FB; B \rightarrow *FB|\varepsilon; F \rightarrow (E)|idE→TA;A→+TA∣ε;T→FB;B→∗FB∣ε;F→(E)∣id[6]
- Q4.6 marksNumericalLR parsingHideAnswer
Construct SLR parse table for the following grammarS→ES \rightarrow ES→EE→E+T∣TE \rightarrow E+T|TE→E+T∣TT→T∗F∣FT \rightarrow T*F|FT→T∗F∣FF→idF \rightarrow idF→id[6]
- Q5.6 marksNumericalAnnotated Parse TreeHideAnswer
Syntax Directed Definition and Annotated Parse Tree
- Q6.6 marksType Checking and ConversionHideAnswer
Differentiate between static and dynamic type checking. How can we carry out type checking for the following expression using syntax-directed definition?
$S \rightarrow id = E$
$S \rightarrow \text{if } E \text{ then } S1$
$S \rightarrow \text{while } E \text{ do } S1$
$S \rightarrow S1; S2$
[6]
- Q7.6 marksNumericalThree-address codeHideAnswer
Define three address codes. write three address codes for
S = do m = n + p while a <= b[6]Three Address Code
Definition (Step 1 - Extract)
Given data (from the question):
- Statement to translate:
S = do m = n + p while a <= b - Structure: a
do ... whileloop with bodym = n + pand conditiona <= b.
No numeric matrices or values are involved; this is a compiler-design (intermediate code generation) problem.
Solution
Definition of Three Address Code
Three Address Code (TAC) is an intermediate code representation in which each instruction contains at most three addresses -- normally two operands and one result. The general form is:
$$x = y ; op ; z$$
where:
- $x, y, z$ are names, constants, or compiler-generated temporaries,
- $op$ is an operator (arithmetic, logical, relational, etc.).
It is called three address code because each statement references at most three "addresses." Complex expressions are broken into a sequence of such simple instructions using temporaries. TAC can be implemented using quadruples, triples, or indirect triples.
Common statement forms:
- Assignment:
x = y op z,x = op y,x = y - Unconditional jump:
goto L - Conditional jump:
if x relop y goto L - Labels:
L:
Translating:
S = do m = n + p while a <= bThis is a do-while loop:
- Body:
m = n + p(executed at least once) - Condition:
a <= b(checked after each iteration; if true, repeat)
The body executes first, then the condition is tested. If the condition holds, control loops back to the beginning of the body.
Generated Three Address Code
Line Instruction Meaning 1 L1:Start of loop body (control always enters here first) 2 t1 = n + pEvaluate n + pinto temporaryt13 m = t1Assign result to m4 if a <= b goto L1Test condition; if true, repeat the body 5 L2:Exit point of the loop In pure sequential form:
L1: t1 = n + p m = t1 if a <= b goto L1 L2: ...
Explanation
L1:marks the beginning of the body. Since it is ado-while, the body must run at least once, so control entersL1unconditionally.t1 = n + puses a new temporaryt1 = newtemp()to store the arithmetic result (three-address form: one operator, two operands, one result).m = t1completes the assignmentm = n + p.if a <= b goto L1evaluates the relational condition. If true, jump back toL1and repeat; if false, fall through.L2:is the loop-exit label, where execution continues after the loop.
Quadruple Representation (optional supporting form)
# op arg1 arg2 result (1) + n p t1 (2) = t1 - m (3) if<= a b L1 This confirms each instruction uses at most three addresses.
- Statement to translate:
- Q8.6 marksCode OptimizationHideAnswer
Define code optimization. Discuss about any three code optimization techniques with example.[6]
- Q9.6 marksRun-time storage managementHideAnswer
What is activation record? Discuss the different activities performed by caller and callee during procedure call and return.[6]
- Q10.6 marksCode GeneratorHideAnswer
Discuss about the different factors affecting target code generation.[6]