CSC376 · TU past paper
Compiler Design and Construction 2080 question paper
The complete TU 2080 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 marksNumericalTop-down parsingHideAnswer
Differentiate between one-pass and multi-pass compiler. Construct the LL(1) parsing table for the following grammar.S→ABCDS \rightarrow ABCDS→ABCDA→a∣εA \rightarrow a | \varepsilonA→a∣εB→bB \rightarrow bB→bC→0∣εC \rightarrow 0 | \varepsilonC→0∣εD→d∣εD \rightarrow d | \varepsilonD→d∣ε[10]
- Q2.10 marksNumericalSpecification and Recognition of tokensHideAnswer
How does a Lexical Analyzer recognize a token? Give an example to make it clear. Convert the regular expression $a(a+b)(b+c)â’a# to DFA.[10]
- Q3.10 marksNumericalLR parsingHideAnswer
Construct the LR(1) parsing table for the following grammar.S→AaAbS \rightarrow AaAbS→AaAbA→BbBaA \rightarrow BbBaA→BbBaA→εA \rightarrow \varepsilonA→εB→εB \rightarrow \varepsilonB→ε[10]
- Q4.5 marksSyntax directed definitionsHideAnswer
Describe about syntax directed translation with an example. [5]
- Q5.5 marksNumericalLR parsingHideAnswer
Given the following grammar with SLR parsing table, test whether the string "int * (int + int)" will be accepted or rejected.
$$ \begin{aligned} E &\to T + E \quad \ldots (1) \ E &\to T \quad \ldots (2) \ T &\to int * T \quad \ldots (3) \ T &\to int \quad \ldots (4) \ T &\to (E) \quad \ldots (5) \end{aligned} $$
$$\begin{array}{|c|c|c|c|c|c|c||c|c|}\hline & \text{ACTION} & & & & & & \text{GOTO} & \ \hline \text{STATE} & \text{int} & * & + & ( & ) & $ & E & T \ \hline 1 & S5 & & & S4 & & & 2 & 3 \ \hline 2 & & & & & & \text{ACC} & & \ \hline 3 & & S6 & & & R2 & R2 & & \ \hline 4 & S5 & & & S4 & & & 7 & 3 \ \hline 5 & & S8 & R4 & & R4 & R4 & & \ \hline 6 & S5 & & & S4 & & & 9 & 3 \ \hline 7 & & & & & S10 & & & \ \hline 8 & S5 & & & S4 & & & & 11 \ \hline 9 & & & & & R1 & R1 & & \ \hline 10 & & & R5 & & R5 & R5 & & \ \hline 11 & & & R3 & & R3 & R3 & & \ \hline \end{array} $$
- Q6.5 marksBasic blocks and flow graphsHideAnswer
How do you recognize basic block? Discuss about the factors that affect code generator. [5]
- Q7.5 marksNumericalTop-down parsingHideAnswer
Find the FIRST and FOLLOW of all the non terminals in following grammar. S→aAbcD∣εS \rightarrow aAbcD | \varepsilonS→aAbcD∣εA→SD∣εA \rightarrow SD | \varepsilonA→SD∣εC→SaC \rightarrow SaC→SaD→aBD∣εD \rightarrow aBD | \varepsilonD→aBD∣ε[5]
- Q8.5 marksCode OptimizationHideAnswer
Why code optimization is needed? Describe any two techniques for loop optimization. [5]
- Q9.5 marksThree-address codeHideAnswer
What is three-address code? How high level code is converted to three address code? Illustrate with an example. [5]
- Q10.5 marksRun-time storage managementHideAnswer
What is activation tree? Define type checking system with examples. [5]
Activation Tree and Type Checking System
Part 1: Activation Tree
An activation tree is a tree data structure used to represent the sequence and nesting of procedure/function calls during the execution of a program at runtime.
Key Properties:
- Each node in the tree represents an activation (call) of a procedure.
- The root represents the activation of the main program.
- If procedure
Acalls procedureB, then the node forBis a child of the node forA. - The execution of a called procedure must complete before the calling procedure resumes (LIFO order), which is why a stack is used to manage activations at runtime.
Example:
Consider the following program:
main() { A(); B(); } A() { C(); } B() { } C() { }The activation tree is:
main / \ A B | CmaincallsAandBsequentially.AcallsC.- The left-to-right order of children represents the order of calls.
Note: The activation tree directly corresponds to the stack storage allocation model, where activation records are pushed and popped in LIFO order as activations begin and end.
Part 2: Type Checking System
Definition:
A type checking system is a component of the semantic analyzer in a compiler that verifies whether the operands and operators in expressions are type-compatible according to the rules of the source language. It ensures that operations are applied to correct types of data, preventing type errors.
As per the notes: "Type-checking is an important part of semantic analyzer."
Types of Type Checking:
Type Description Static Type Checking Performed at compile time Dynamic Type Checking Performed at runtime Type Checking Rules:
- Type of an expression is determined by the types of its sub-expressions.
- If an operator is applied to incompatible types, a type error is reported.
- Type coercion (implicit conversion) may be applied where allowed.
Examples:
Example 1: Arithmetic Expression
int a = 5; float b = 3.2; float c = a + b; // Valid: int is coerced to float- Type checker detects
aisintandbisfloat. - Applies widening coercion:
int→float. - Result type is
float. No error.
Example 2: Type Mismatch Error
int x = 10; char *p = x; // Type Error: cannot assign int to pointer- Type checker detects incompatible types (
intvschar *). - Reports a type error at compile time.
Example 3: Function Call Type Checking
int add(int a, int b) { return a + b; } add(3, "hello"); // Type Error: second argument is string, expected int- Type checker verifies that actual parameter types match formal parameter types.
- Reports a type mismatch error.
Type Checking System Summary:
Source Code | v Lexical Analyzer --> Tokens | v Syntax Analyzer --> Parse Tree | v Semantic Analyzer (Type Checker) --> Checks type compatibility --> Reports type errors --> Annotates parse tree with type info | v Intermediate Code GeneratorThe type checking system ensures type safety and produces type-annotated intermediate representations for further compilation phases.
- Q11.5 marksInformation provided by Symbol TableHideAnswer
What are the advantages of intermediate code? What types of information are provided by symbol table? [5]
- Q12.5 marksAnnotated Parse TreeHideAnswer
What is annotated parse tree? Define S-attributed grammar with an example. [5]