CSC376 · TU past paper
Compiler Design and Construction 2078 question paper
The complete TU 2078 exam paper for Compiler Design and Construction (CSC376), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- Q1.10 marksNumericalFinite Automata relevant to compiler constHideAnswer
List out the tasks performed by Lexical Analyser. Define DFA. Convert the Regular Expression (a+b)∗a(a+b)(a+b)^*a(a+b)(a+b)∗a(a+b) to DFA directly.[10]
- Q2.10 marksNumericalLR parsingHideAnswer
Differentiate between LR(0) and LR(1) algorithm. Construct LR(1) parse table of the following grammar.S→AAS \rightarrow AAS→AAA→aA∣bA \rightarrow aA | bA→aA∣b[10]
- Q3.10 marksType Checking and ConversionHideAnswer
Define type checking. Differentiate between type casting and coercion? Write SDT to carry out type checking for the following expression. $E \rightarrow n \mid E * E \mid E == E \mid E[E] \mid E \uparrow$ [10]
- Q4.5 marksBasic concepts related to Compiler such asHideAnswer
Define compiler and differentiate it with an interpreter. [5]
- Q5.5 marksInformation provided by Symbol TableHideAnswer
What are the typical entries made in symbol table? Explain. [5]
- Q6.5 marksNumericalBasic parsing techniquesHideAnswer
Define Left recursive grammar. Remove left recursion from the following grammar. S→SB∣CaS \rightarrow SB | CaS→SB∣CaB→Bb∣cB \rightarrow Bb | cB→Bb∣cC→aB∣aC \rightarrow aB | aC→aB∣a[5]
- Q7.5 marksNumericalBottom-up parsingHideAnswer
What are the disadvantages of shift reduce parsing. Perform shift reduce parsing of string w = (x-x). (x/x) for given grammar. E→E−E∣E/E∣(E)∣xE \rightarrow E-E | E/E | (E) | xE→E−E∣E/E∣(E)∣x[5]
- Q8.5 marksAttribute TypesHideAnswer
Define attribute grammar with example of inherited and synthesized attributes. [5]
- Q9.5 marksNumericalQuadruplesHideAnswer
Define three address codes. Write down Quadruples for: $a = -b*(c+d)/e$. [5]
Three Address Code and Quadruples
Given
- Expression to translate: $a = -b * (c + d) / e$
- Required output format: Quadruples
- Also required: Definition of three address code
No numeric data is involved; this is a code-generation problem. All required information is present.
Solution
Definition of Three Address Code
Three address code (TAC) is an intermediate representation of a program in which each statement contains at most three addresses (or operands): two for the operands and one for the result. The general form is:
$$x = y ; \text{op} ; z$$
where $x$, $y$, $z$ are names, constants, or compiler-generated temporary variables, and $\text{op}$ is an operator (arithmetic, logical, etc.). Each statement performs at most one operation, which makes it easy to translate into machine code and to optimize.
Three address code can be implemented in three ways: Quadruples, Triples, and Indirect Triples.
Breaking the Expression into Three-Address Statements
Expression: $a = -b * (c + d) / e$
Applying operator precedence (parentheses first, then unary minus, then $*$ and $/$ left to right):
Step Statement Explanation 1 $t_1 = -b$ unary minus on $b$ 2 $t_2 = c + d$ evaluate parenthesis 3 $t_3 = t_1 * t_2$ multiply 4 $t_4 = t_3 / e$ divide by $e$ 5 $a = t_4$ assign to $a$
Quadruple Representation
A quadruple has four fields: (op, arg1, arg2, result).
Index op arg1 arg2 result (0) uminusb _ $t_1$ (1) +c d $t_2$ (2) *$t_1$ $t_2$ $t_3$ (3) /$t_3$ e $t_4$ (4) =$t_4$ _ a Notes:
uminusdenotes the unary minus operator (distinguished from binary subtraction)._indicates an unused field.
Final Result
The expression $a = -b*(c+d)/e$ produces 5 quadruples using 4 temporary variables ($t_1, t_2, t_3, t_4$), matching the standard textbook decomposition.
- Q10.5 marksRun-time storage managementHideAnswer
List out different types of run time storage management techniques. Explain any one of them. [5]
- Q11.5 marksCode OptimizationHideAnswer
What is the advantages of code optimization? Explain about dead-code elimination. [5]
- Q12.5 marksCode GeneratorHideAnswer
Explain about the factors affecting target code generation. [5]