CS3501 Compiler Design
CS3501 Compiler Design
CS3501-Compiler Design
Question Bank
Unit-I
INTRODUCTION TO COMPILERS & LEXICAL ANALYSIS
Structure of a compiler – Lexical Analysis – Role of Lexical Analyzer – Input Buffering –
Specification of Tokens – Recognition of Tokens – Lex – Finite Automata – Regular Expressions to
Automata – Minimizing DFA.
Part-A
1. Mention few cousins of the Compiler. M/J 2012
Pre-processor
Assembler
Loader
Link-Editor
4. What are the possible error recovery actions in lexical analyzer? M/J 2012, A/ M’2015
1. Deleting an extraneous character
2. Inserting a missing character
3. Replacing an incorrect character by a correct character
4. Transposing two adjacent characters
Lex
lex.l lex.yy.c
Compiler
C compiler
lex.yy.c a.out
8. State any two reasons as to why phases of compiler should be grouped. M/J’2014
(i) Operation of compilation.-Analysis (Front End ) and Synthesis(Back End)
(ii) Number of Passes- One Pass and Multi-Pass
11. State the interactions between the lexical analyzer and the parser. N/D’2015
Lexical token Parser
analyser
getNextToken
get next
token
Symbol
table
Its main task is to read the input characters and produces output a sequence of tokens that the
parser uses for syntax analysis.
As in the figure, upon receiving a “get next token” command from the parser the lexical analyzer
reads input characters until it can identify the next token.
Whenever an identifier is detected in any of the phases, it is stored in the symbol table.
Ex: Var posi, init, rate : real;
pos Real …
init Real …
rate Real …
Error messages
16. What are the functions of preprocessors?
Macro processing
File inclusion
Rational preprocessors
Language Extensions
20. What are the tools available for analysis part? Describe about any two.
STRUCTURE EDITORS: The structure editor not only performs the text-creation and modification
functions of an ordinary text editor, but it also analyzes the program text, putting an appropriate
hierarchical structure on the source program.
Pretty Printers: A pretty printer analyses a program and prints it in such a way that the structure
of the program becomes clearly visible
Static Checkers: A static checker reads a program, analyzes it, and attempts to discover
potential bugs without running the program.
Interpreters: An interpreter might build a syntax tree and then carry out the operations at the
nodes as it walks the tree.
PART B
1. Explain the various phases of compiler in detail, with a neat sketch A/M2012, N/D 2012,
M/J’2013, N/D’2013, M/J’2014,M/J’2016, N/D’2016
Contents:
Explain all the Phases of Compiler
Neatly Represent the Diagram
2. Explain language processing system with neat diagram.(8) (cousins of compiler) M/J’2016
Contents:
Explain all the Cousins of Compiler
Neatly Represent the Diagram
Ans:
Steps for converting NFA to DFA:
Step 1: Convert the given NFA to its equivalent transition table
Step 2: Create the DFA‟s start state
Step 3: Create the DFA‟s transition table
Step 4: Create the DFA‟s final states
Step 5: Simplify the DFA
Transition Table
Transition Diagram
4. Convert the regular expression abb (a|b) to DFA using direct method and minimize it.(AU–
April / May 2017)
Contents:
Define Regular expression
Steps followed to the convention
5. Draw the Transition Diagram for relational operators and unsigned numbers.(AU
–April /May 2017)
Contents:
Define Finite Automata
Write down the Two Notations
Explain in detail the both notations
Give any one example
Part–C (1 x 15 = 15 Marks)
1. What are the Phases of compiler? Explain the Phases in detail. Write down the output
ofeach phases for the expression a: = b + c * 60(AU–April / May 2017)
Contents:
o Define Compiler
o Explain all the Phases of Compiler
o Neatly Represent the Diagram
o Write the Example Expression to be match with all phases ofcompiler
Unit-II
SYNTAX ANALYSIS
Role of Parser – Grammars – Context-free grammars – Writing a grammar Top Down Parsing –
General Strategies – Recursive Descent Parser Predictive Parser-LL(1) – Parser-ShiftReduce Parser-
LR Parser- LR (0)Item Construction of SLR Parsing Table – Introduction to LALR Parser – Error
Handling and Recovery in Syntax Analyzer-YACC tool – Design of a syntax Analyzer for a Sample
Language
Part-A
1. Define Define Recursive Descent Parsing?
A Recursive Descent Parser (RDP) is a type of top-down parsing technique used incomputer
science to analyze and process a language's syntax.
During analysis, the operations implied by the source program are determined and recorded in a
hierarchical structure called a tree. A special kind of tree called a syntax tree is used, in which each node
represents an operation and the children of a node represent the arguments of the operation.
Parse Tree:
A parse tree may be viewed as a graphical representation for a derivation that filters out the choice
regarding replacement order. Each interior node of a parse tree is labeled by some nonterminal A and that the
children of the node are labeled from left to right by symbols in the right side of the production by which this
A was replaced in the derivation. The leaves of the parse tree are terminal symbols.
A → βA’
A’→ αA’ | ε without changing the set of strings derivable from A
For example, if y was declared to be a real and x3 an integer, We need to insert (unary, i.e., one operand)
conversion operators “inttoreal” and “realtoint” as shown on the right.
6. Draw the syntax tree for the expression: a=b*-c +b*-c N/D 2012
A syntax tree depicts the natural hierarchical structure of a source program. A DAG (Directed Acyclic
Graph) gives the same information but in a more compact way because common sub-expressions are
identified. A syntax tree for the assignment statement a:=b*-c+b*-c appear in the figure.
.
Fig: Graphical Representation of a := b * -c + b * -c
7. Eliminate left recursion from the following grammar A->Ac/Aad/bd/€. M/J’2013
A->bdA‟/A‟
A‟->cA‟/adA‟/€
8. Eliminate left recursion from the grammar S-> Aa | b ; A-> Ac | Sd | €. N/D’2013
Ans: S-> Aa | b ; A-> Ac|Aad |bd|€
Equivalent Rule:
S-> Aa | b
A->bdA‟/A‟
A‟->cA‟/adA‟/€
9. Write a CF grammar to represent palindrome. N/D’2014
S->aSa | bSb | a | b| €
String={aba,bab,abba,ababa,...}
Part-B
1. Discuss in detail about the role of parser.
Contents:
Explain the process of Parser work
Types of Parser and explain
Neatly Represent the Diagram
2. Write an Algorithm and construct SLR Parsing Table for the following context freegrammar.
Check whether the string id + id id * is a valid string (AU–April / May 2013)
E→E+T | T
T→T*F | F
F→F*| a| b
Contents:
Define SLR
Steps involved for SLR Parser
Neatly Represent the given CFG Grammar
Construct the Parsing table
Check the Input String with stack implementation
Part – C
1. Construct Stack implementation of shift reduce parsing for the grammarE->E+EE->E*EE-
>(E)E->id and the input string id1+id2*id3
Contents:
Explain the Concepts of Shift Reduce parser
Neatly Represent the given CFG Grammar
Check the Input String with stack implementation
Unit-III
SYNTAX DIRECTED TRANSLATION & INTERMEDIATE CODE GENERATION
Syntax directed Definitions-Construction of Syntax Tree-Bottom-up Evaluation of S-Attribute
Definitions- Design of predictive translator – Type Systems-Specification of a simple type Checker
Equivalence of Type Expressions-Type Conversions. Intermediate Languages: Syntax Tree, Three
Address Code, Types and Declarations, Translation of Expressions, Type Checking, Back patching.
Part – A
1. What are the various methods of implementing three address statements? M/J’2013
There are three types of intermediate representation:-
1. Syntax Trees
2. Postfix notation
3. Three Address Code
2. Translate the arithmetic expression a * -(b+c) into syntax tree and postfix notation. N/D’201
3. Construct a decorated parse tree according to the syntax directed definition for the following
input statement: (4+7.5*3)/2. A/ M’2015
5. Write down syntax Directed definition of a simple desk calculator(AU –Nov / Dec 2016)
Syntax Directed Definition (SDD) is a kind of abstract specification. The combination of
context free grammar and semantic rules
7. Mention the rules for type checking (AU– April / May 2016)
A compiler must check that the source program should follow the syntactic and semantic
conventions of the source language and it should also check the type rules of the language.
13. What is the significance of intermediate code? MAY/JUNE 14, APRIL/MAY 11, APRIL/MAY
Retargeting is facilitated; a compiler for a different machine can be created by attaching a
back end for the new machine to an existing front end.
A machine-independent code optimizer can be applied to the intermediate representation.
14. What are the functions used to create the nodes of syntax trees?
Mknode (op, left, right)
Mkleaf (id,entry)
Mkleaf (num, val)
15. What are the functions for constructing syntax trees for expressions?
The construction of a syntax tree for an expression is similar to the translation of the
expression into postfix form.
Each node in a syntax tree can be implemented as a record with several fields.
Part – B
1. Explain in detail about Specification of a simple type checker. (AU – April / May 2017)
Contents:
Explain the concept of type checking expression
Explain in Types checking statement
Explain in Equivalence of type expression
Neatly Represent the Diagram
2. Discuss the following in detail about the Syntax Directed Definitions.Inherited Attributes and
Synthesized attributes
Contents:
Define SDD
Steps involved for SDD
3. Create variants of Syntax tree. Explain in detail about it with suitable examples
Contents:
Define Syntax Tree
Explain the Concepts
Explain in details the variants of Tree Structure
5. What is Type conversion? What are the two types of type conversion? Formulate therules for
the type conversion.
Contents:
Explain the concept of type conversion or casting
Explain the Concepts of two types of conversion
Give an Example of each types.
Part– C
1. Write the Translation scheme for translating assignment statement having scalarvariables and
array reference to three - address statements(AU April / May 2013)
Contents:
Explain the Concepts of Three Address code
Write the applications
Implementation of Three address code and types
Give an Example of each types.
Unit-IV
RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation – Parameter Passing-Symbol Tables – Dynamic
Storage Allocation – Issues in the Design of a code generator – Basic Blocks and Flow graphs –
Design of a simple Code Generator –Optimal Code Generation for Expressions– Dynamic
Programming Code Generation.
Part -A
1. Define Flow Graph. A/M 2012
Basic block
A sequence of consecutive statements which may be entered only at the beginning and when
entered are executed in sequence without halt or possibility of branch , are called basic blocks.
Flow graph
The basic block and their successor relationships shown by a directed graph is called a
flow graph.
The nodes of a flow graph are the basic blocks.
8. Brief about the techniques for parameter passing.(AU –Nov / Dec 2013)
When using call by value, the compiler adds the R-value of the actual parameters that were passed
to the calling procedure to the called procedure's activation record.
| While E do S1.
goto L1
….
L1: gotoL2
by the sequence
goto L2
….
L1: goto L2
If there are now no jumps to L1, then it may be possible to eliminate the statement L1:goto
L2 provided it is preceded by an unconditional jump .Similarly, the sequence
if a < b goto L1
….
L1: goto L2
can be replaced by
Ifa < b goto L2
….
L1: goto L2
20. Write three address code sequence for the assignment statement. M/J’2016
D= (a-b)+(a-c)+(a-c)
temp1:= a-c
temp2:=a-b
temp3:=temp2+temp1
D:=temp3+temp1
Part-B
1. Explain in detail the following with respect to code generation phase . (AU – Nov /Dec 2016)
Contents:
Explain the concept of type checking expression
Explain in Types checking statement
Explain in Equivalence of type expression
Neatly Represent the Diagram
2. What are the different storage allocation strategies?(AU April / May 2017)
Contents:
Define Storage allocation
Write the three Strategies
Explain in detail all three strategies
Neatly Represent with advantage and disadvantage
3. Explain in detail about the parameter passing (AU– April / May 2017)
Contents:
Define Syntax Tree
Explain the Concepts
Explain in details the variants of Tree Structure
5. Generate optimal code using Dynamic Programming techniques for the assignmentstatement
, X: = ( a/b - c ) / d. assume unit instruction costs (AU Nov / Dec 2013)
Contents:
Explain the concept of type conversion or casting
Explain the Concepts of two types of conversion
Give an Example of each types.
Part– C
1. Discuss the Various issues in Design of code generator (AU –April / May 2017)
Contents:
Explain the Concepts of Three Address code
Write the applications
Implementation of Three address code and types
Give an Example of each types.
Unit-V
CODE OPTIMIZATION
Principal Sources of Optimization – Peep-hole optimization – DAG- Optimization of Basic Blocks
– Global Data Flow Analysis – Efficient Data Flow Algorithm – Recent trends in Compiler Design
Part -A
1. List out two properties of reducible flow graph. A/M 2012
Reducible flow graphs are special flow graphs, for which several code optimization
transformations are especially easy to perform, loops are unambiguously defined, dominators
can be easily calculated.
Data flow analysis problems can also be solved efficiently.
3. What are the uses of register and address descriptor in code generator? N/D 2012
A register descriptor keeps track of what is currently in each register.
An address descriptor keeps track of the location where the current value of the name can be
found at run time.
as constant folding.
One advantage of copy propagation is that it often turns the copy statement into dead code.
For example,
a=3.14157/2 can be replaced by
a=1.570 thereby eliminating a division operation.
12. How would you represent the dummy blocks with no statements indicated in global data flow
analysis. M/J’2014
We say that the beginning points of the dummy blocks at the entry and exit of a statement‟s
region are the beginning and end points, respectively, of the statement. The equations are
inductive, or syntax-directed, definition of the sets in[S], out[S], gen[S], and kill[S] for all
statements S.
14. What role does the target machine play on the code generation phase of the compiler? A/
M’2015
Familiarity with the target machine and its instruction set is a prerequisite for designing a
good code generator.
The target computer is a byte-addressable machine with 4 bytes to a word.
It has n general-purpose registers, R0, R1, . . . , Rn-1.
It has two-address instructions of the form:
op source, destination
where, op is an op-code, and source and destination are data fields.
It has the following op-codes :
MOV (move source to destination)
ADD (add source to destination)
SUB (subtract source from destination)
The source and destination of an instruction are specified by combining registers and
memory locations with address modes.
15. Generate code for the following C statement assuming three registers are available:
x=a/(b+c)-d*(e+f) A/ M’2015
Three address code:
t1= e+f
t2=b+c
t3=a/t2
t4=d*t1
t5=t3-t4
x=t5
Assembly Code (Target Code):
MOV e, R1
ADD f,R1
MOV b,R2
ADD c,R2
MOV a,R3
DIV R3,R2
MUL d,R1
SUB R1,R2
MOV R2,x
16. Write the algorithm that orders the DAG nodes for generating optimal target code. A/ M’2015
The heuristic ordering algorithm attempts to make the evaluation of a node immediately
follow the evaluation of its leftmost argument.
The algorithm shown below produces the ordering in reverse.
Algorithm:
1. while unlisted interior nodes remain do begin
2. select an unlisted node n, all of whose parents have been listed;
3. list n;
4. while the leftmost child m of n has no unlisted parents and is not a leaf
do begin
5. list m;
6. n:=m
end
end
18. What are the issues in the design of code generator? N/D’2015
The following issues arise during the code generation phase :
1. Input to code generator
2. Target program
3. Memory management
4. Instruction selection
5. Register allocation
6. Evaluation order
Part -B
1. Explain in detail about Global Data flow analysis of structural programs.(16) NOV/DEC2012,
M/J’2013, N/D’2013, M/J’2014, N/D’2015, N/D’2016
Contents
Define Global Data flow analysis
Explain Points and Paths:
Draw Diagram Neatly
Write Data-flow analysis of structured programs:
2. For the flow graph shown below, write the three address code and construct the DAG. (8)
M/J’2013
(1) t1 = 4*i
(2) t2 = a[t1]
(3) t3 = 4 *i
(4) t4 = b[t2]
(5) t5= t2 * t4
(6) t6 = prod + t5
(7) prod = t6
(8) t7 = i+1
(9) i = t7
(10) if i<=20 goto (1)
4. Construct DAG and three address statement for the following C Program. N/D’2013, N/D’2014
i=1;
s=0;
while(i<=10)
{
s=s+a[i][i]
i=i+1;
}
Contents
Define DAG
Write the three address code
Draw the DAG disgram
5. Define a directed acyclic graph. Construct a DAG and write the sequence of instructions for
the expression a+a*(b-c)+(b-c)*d. (16)MAY/JUNE 14
Contents
Construct a DAG and Equivalence of Instructions
Three address code for given ecpression
Write the Instruction code
6. Explain the steps carried out for generating code from DAGs with an example.(8) N/D’2015
Contents
Rearranging the order
Write Generated code sequence for basic block
Write the algorithm
Give a suitable example
PART C
1. Discuss about the following:
i). Copy Propagation ii) Dead-code Elimination and iii) Code motion(6) NOV/DEC2012,
MAY/JUNE 2012
Contents
Copy Propagation
Dead-Code Eliminations
Constant folding
Loop Optimizations
Code Motion
2. Explain peephole optimization. (8) NOV/DEC2013 ,NOV/DEC2012, MAY/JUNE 2012
Contents
Write the Reduntant Loads And Stores
Write the Unreachable Code
Elimination Of Common Subexpressions
Elimination Of Dead Code
Reduction In Strength
Use Of Machine Idioms
Getting Better Performance