Course Files
Course Files
BRANCH:CSE
COURSE: III/IV B.TECH-II SEM
FACULITY:G.ANILKUMAR
HOD PRINCIPAL
TABLE OF CONTENTS
1. TITLE
2. COURSE OBJECTIVE
3. APPLICATION
4. PRE-REQISITE
5. SYALLABUS
6. REFERENCES
i). BOOKS
ii).JOURNALS
7. ACADEMIC CALENDAR
8. TIME TABLE
9. LESSON/TEACHING SCHEDULE
10. TOPICS COVERED BEYOND THE SYALLABUS
11. WEB LINKS
12. LECTURE NOTES
13. ASSIGNMENTS
14. Data BANK OF QUESTIONS
i). PREVIOUS YEAR QUESTION PAPERS
ii). RELATED QUESTIONS IN GATE&IES
iii). INTERNAL QUESTION PAPERS
iv). EXTERNAL QUESTION PAPERS
15. EXPERT LEACTURES/SEMINARS
16. LATEST FINDINGS
17. LAB JOURNALS AND MANUALS
18. RESULT ANALYSIS ON INTERNAL EXAMINATIONS(GRADING OF STUDENTS INTO A/B/C CATAGORIES)
19. COUNSELLING OF B/C CATEGORY STUDENTS)
1. TITLE : COMPILER DESIGN
2.COURSE OBJECTIVE :
This course introduces the basics of building a compiler using a multiphase translation process. It covers lexical analysis, parsing and translation
to abstract syntax using modern parser generator technology. It discusses binding of identifiers and symbol table organization, and a variety of
intermediate representations that are suitable for back-end analysis. It investigates back-end transformations and optimizations for a number of
languages. Other topics include type checking, static analysis and basic run time support. Compiling is essentially a process of symbolically
manipulating program representations represented by tree and graph-like data structures. Because of this, we will use tools that facilitate
symbolic manipulation and definition of such structures as parser and lexical generators, and tools for generating code from pattern-based
descriptions.
i. to introduce the major concept areas of language translation and compiler design.
ii. to develop an awareness of the function and complexity of modern compilers.
iii. to provide practical, hands-on experience in compiler design, writing and modification.
Topics:
• Lexical Analysis, regular grammars, scanners.
• Syntactic Analysis, BNF notation, parsing theory.
• LL(k) and LR(k) parsers. Grammar transformations for parser compliance.
• Parser generators, translator writing systems.
• Semantic Analysis, symbol tables, intermediate text forms.
• Runtime machine models. Native code. Virtual machines. P-Code.
• Overview of Code Generation techniques, non-optimized.
• Register management, storage allocation.
APPLICATION :
Aims and Objectives of the Course
- Describe the organization of a modern compiler.
- Create efficient and correct compilers for simple procedural languages.
- Understand and apply a variety of optimizations to improve the code generated by compilers.
- Develop good programming skills by writing, organizing, and documenting a significant programming project.
4.Pre-Requisite:
5. Syllabus
COMPILER DESIGN
UNIT – I
Overview of Compilation: Phases of Compilation – Lexical Analysis, Regular Grammar and regular
expression for common programming language features, pass and Phases of translation, interpretation,bootstrapping,
data structures in compilation – LEX lexical analyzer generator.
UNIT – II
Top down Parsing : Context free grammars, Top down parsing – Backtracking, LL (1), recursive descentparsing, Predictive
parsing, Preprocessing steps required for predictive parsing.
UNIT – III
Bottom up parsing : Shift Reduce parsing, LR and LALR parsing, Error recovery in parsing , handling ambiguous grammar,
YACC – automatic parser generator.
UNIT – IV
Semantic analysis : Intermediate forms of source Programs – abstract syntax tree, polish notation and three address
codes. Attributed grammars, Syntax directed translation, Conversion of popular
Programming languages language Constructs into Intermediate code forms, Type checker.
UNIT – V
Symbol Tables : Symbol table format, organization for block structures languages, hashing, tree
structures representation of scope information. Block structures and non block structure storage
allocation: static, Runtime stack and heap storage allocation, storage allocation for arrays, strings and records.
UNIT – VI
Code optimization : Consideration for Optimization, Scope of Optimization, local optimization, loop
optimization, frequency reduction, folding, DAG representation.
UNIT – VII
Data flow analysis : Flow graph, data flow equation, global optimization, redundant sub expression
elimination, Induction variable elements, Live variable analysis, Copy propagation.
UNIT – VIII
Object code generation : Object code forms, machine dependent code optimization, register allocation and assignment
generic code generation algorithms, DAG for register allocation.
6.REFERENCES:
I. TEXT BOOKS :
1. Principles of compiler design -A.V. Aho . J.D.Ullman; Pearson Education.
2. Modern Compiler Implementation in C- Andrew N. Appel, Cambridge
University Press.
REFERENCES :
1. lex &yacc – John R. Levine, Tony Mason, Doug Brown, O’reilly
2. Modern Compiler Design- Dick Grune, Henry E. Bal, Cariel T. H. Jacobs,
Wiley dreamtech.
3. Engineering a Compiler-Cooper & Linda, Elsevier.
4. Compiler Construction, Louden, Thomson
II. JOURNALS:
7. ACADAMIC CALENDAR
CSE DEPARTMENT
III-B.TECH (CSE-A) Time Table
Hours 10.00am 10.50am 11.40am L 1.30pm 2.20pm 3.20pm 4.10pm
To To To U To To To To
Week
10.50am 11.40am 12.40pm N 2.20pm 3.10pm 4.10pm 5.00pm
C CN LIBRARY/
MON AI&NN OS IS CD OOAD
H INTERNET
CN LIBRARY/
TUES CD OOAD OS B IS AI&NN
INTERNET
R
WED OOAD IS AI&NN E OS CN& CT LAB
A
OS IS CD K
THUR CN SEMINAR
AI&NN
LIBRARY/
FRI IS OOAD CN CD OS AI&NN
INTERNET
LIBRARY/
SAT OS&CD LAB CD OOAD CN
INTERNET
9.LESSON/TEACHING SCHEDULE
Dr.SAMUEL GEORGE INSTITUTE OF ENGINEERING & TECHNOLOGY, MARKAPUR-523316.
TEACHING PLAN
NAME OF THE FACULTY:G.ANIL KUMAR SUB: COMPILER DESIGN BRANCH:C.S.E(A) YEAR: III SEMISTER:2nd
SNO
WEEK TOPICS TO BE COVERED
Parsing, LR
5 4/12/10 to 9/1/10 LALR Parsing, Error Recovery in Parsing, Handling Ambigous Grammer
6 18/1/10 to 23/1/10 YACC-automatic parser generator.Semantic Analysis:-Intermediate forms of source P
storage allocation
12 1/3/10 Storage allocation for arrays,strings and records,
to13/3/10
14 15/3/10 to 20/3/10 Data flow analysis:- Fluro graph,data flow equation, global optimization,redundant su
expression elimination
15 22/3/10 to Induction variable elements, Live variable analysis,copy propogation
16 29/3/2010 to Register allocation and assignment generic code operation algorithms,DAG for registe
3/04/2010 allocation
• ONLINE HELP
o GNU Manuals
http://www.delorie.com/gnu/docs/
12.LECTURE NOTES
13.ASSIGNMENTS
ASSIGNMENT-1
1. Explain the input buffer scheme for scanning the source program.How the use of sentinels can improve its
performance?Describe in detail
2. a) Explain the different phases of a compiler,showing the output of each phase, using the example of the
following statement: position:=initial+rate*60.
b) Compare compiler and interpreter with suitable diagrams?
3. a) Explain in detail, lexical analyzer generator?
b) Describe the lexical errors and various error recovery strategies with suitable examples?
4. a) Consider the following fragment of 'c' code:
float i,j;
i=i*70+j+2;
write the output at all phases of the compiler for the above 'c' code
b) Write short notes on: input buffering?
5. a) Explain the boot strapping process with suitable diagrams?
b) Explain how input buffering helps lexical analyzer in compilation process?
6 Explain with an example, how LEX program performs lexical analysis for the following patterns in c:
identifiers,comments,constants,and arthmetic operators.
ASSIGNMENT-2
2. what is recursive decent parser?construct recursive desecnt parser for the following grammar
E ->E+T/T
T ->TF/F
F ->F*/a/b
3. What is ambiguous grammar?Eliminate ambiguties for the grammar:
E->E+E/E*E/(E)/id.
4. consider the following grammar
s ->0A|1B|0|1
A ->0S|1B|1
B ->0A|1S
construct the left most derivations and parse trees for the following sentences
i)0101
ii)1100101
5. Consider the following grammar
E ->T+E|T
T ->V*T|V
V ->id
Write down the procedures for the nonterminals of the grammar to make a recursive decent parser.
6. Give the rules for computation of FIRST(X) and FOLLOW(X). Construct FRIST and FOLLOW sets for the
following grammar.
E ->TE'
E' ->+TE'|e
T -> FT'
T' ->*FT'|e
F ->(E)/id
7.write an algorithm for construction of predictive parsing table?
ASSIGNMENT-3
ASSIGNMENT-4
ASSIGNMENT-5
ASSIGNMENT-6
ASSIGNMENT-7
ASSIGNMENT-8
1.X=f(a)+f(a)+f(a)
2.X=f(a)/g(b,c)
3.X=f(f(a))
4.X=++f(a);
main()
{
int i,a[10];
while(i<=10)a[i]=0
}
6 . Describe,how addressing modes can be used for reducing the memory access time.
7.Generate the code sequence using code generation algorithm for the following expression
w:=(A-B)+(A-C)+(A-C);
Code No: R05320502
Set No. 2
III B.Tech Supplimentary Examinations, Aug/Sep 2008
COMPILER DESIGN
(Computer Science & Engineering)
Time: 3 hours Max Marks: 80
Answer any FIVE Questions
All Questions carry equal marks
1. Explain with an example, how LEX program performs lexical analysis for the fol-
lowing patterns in C :
identifier, comments, constants, and arithmetic operators.
[16]
3. Define precedence functions. Construct precedence table and directed graph rep-
resenting precedence functions for operators +, *, id, $. [16]
4. (a) Describe the overloading of functions and operators with suitable examples.
(b) Write a note on polymorphic functions. [8+8]
5. (a) What is an ordered and unordered symbol table? What is the function of
symbol table in the compliation process? Explain.
(b) What are the various attributes of a Symbol Table? [10+6]
1 of 2
Code No: R05320502
Set No. 2
c[i, j] :=0;
for i := 1 to n do
for j:=1 to n do
for k :=1 to n do
c[i, j] :=c[i , j] +a[i ,k] *b [k ,j]
end
(a) Assuming a, b, and c are allocated static storage and there are four bytes per
word in a byte-addressed memory, produce three-address statements for the
above program.
(b) Find the induction variables of each loop and eliminate them wherever possible
[8+8]
2 of 2
Code No: R05320502
Set No. 3
III B.Tech Supplimentary Examinations, Aug/Sep 2008
COMPILER DESIGN
(Computer Science & Engineering)
Time: 3 hours Max Marks: 80
Answer any FIVE Questions
All Questions carry equal marks
1. Explain with an example, how LEX program performs lexical analysis for the fol-
lowing patterns in C :
identifier, comments, constants, and arithmetic operators.
[16]
3. (a) Explain the stack implementation of shift reduce parsing method with an
example.
(b) Define handle. Give suitable example. [10+6]
5. Only one occurrence of each ob ject is allowable at a given moment during program
execution. Justify your answer with respect to static allocation. [16]
1 of 1
Code No: R05320502
Set No. 4
III B.Tech Supplimentary Examinations, Aug/Sep 2008
COMPILER DESIGN
(Computer Science & Engineering)
Time: 3 hours Max Marks: 80
Answer any FIVE Questions
All Questions carry equal marks
1. (a) Explain the di erent phases of a compiler, showing the output of each phase,
using the example of the following statement:
position : = initial + rate * 60
(b) Compare compiler and interpreter with suitable diagrams. [10+6]
2. (a) Give the rules for computation of FIRST(X) and FOLLOW(X). Construct
FIRST and FOLLOW sets for the following grammar.
E TE
E +T E |
T FT
T FT|
F (E)|id
(b) Write an algorithm for construction of predictive parsing table. [10+6]
5. (a) Explain the hash table with temporary and permanent storage.
(b) Reusing the storage space for names. [8+8]
6. (a) What is DAG? Construct the DAG for the following basic block
D := B C
E :=A+B
B := B+C
A := E-D
(b) What are the legal evaluation orders and names for the values at the nodes
for the DAG of problem (a).
i. Assuming A, B and C are alive at the end of the basic block?
ii. Assuming only A is live at the end? [6+10]
1 of 2
Code No: R05320502
Set No. 4
time
(b) Generate the co de sequence using Code generation algorithm for the following
expression [8+8]
W:=(A-B)+(A-C)+(A-C)
Code No: R05320502
Set No. 21
8. (a)III
Describe, how
B.Tech II addressing
Semester modes
Regular can be used Apr/May
Examinations, for reducing
2008the memory access
COMPILER DESIGN
(Computer Science & Engineering)
Time: 3 hours Max Marks: 80
Answer any FIVE Questions
All Questions carry equal marks
Explain
1. (a) the the
Explain input
di bu er scheme
erent phases of fora scanning
compiler,the sourcethe
showing program.
output How thephase,
of each use of
sentinels canthe
using improve
exampleits of
performance?
the followingDescribe in detail. [16]
statement:
position : = initial + rate * 60
2. Construct predictive parsing table for the following grammar.
(b)
E T Compare
E compiler and interpreter with suitable diagrams. [10+6]
E +T E |
2. (a) What is recursive descent parser? Construct recursive descent parser for the
T FT [16]
following grammar.
T FT|
E E + T|T
F (E)|id
T TF|F
F isFan operator
3. (a) What * |a|b
grammar? Give an example.
(b)
(b) What
Write is
anambiguous grammar?parsing
operator precedence Eliminate ambiguities
algorithm. for the grammar:
[6+10]
E E + E|E * E|(E)|id. [8+8]
4. (a) Write a note on the specification of a simple type checker.
3. Construct SLR parsing table for the following grammar.
(b) What is a type expression? Explain the equivalence of type expressions with
S AS|b
an appropriate examples. [8+8]
[16]
A SA|a
5. (a) Compare three di erent storage allocation strategies.
4. (a) Write the quadruple, triple, indirect triple for the statement a := b * -c+b * -c
(b) Consider the following array declaration in ‘c’;
(b) Explain the role of intermediate code generator in compilation process. [8+8]
oat a[100][100];
Assume
5. Write an thattotheperform
algorithm main memory
the tableinlookup
byte addressable andoperation
and insertion that the array is stored
for hashed
symbolstarting
table. from the memory address 100. What is the address of a[40][50]?[8+8] [16]
6.
6. Explain
(a) Whatdiiserent
code principal sources
optimization? of optimization
What technique with suitable exam-
are its advantages?
ples. [16]
(b) Explain brie y about folding.
7. (a)(c)
Write and
What areexplain live variable
the problems analysis compiler
in optimizing algorithm.design? [5+5+6]
(b) Explain the use of algebraic transformations with an example [8+8]
7. (a) Explain reducible and non-reducible ow graphs with an example.
8. (a)(b)
Explain thenatural
Explain di erentloops
issues
andininner
the design
loops of
of aa co
owdegraph
generator.
with an example. [8+8]
(b) Generate co de for the following C statements:
8. (a) Explain the concept of object code forms.
i. x= f(a) + f(a) + f(a)
(b) Generate optimal machine code for the following C program. [6+10]
ii. x= f(a) /g(b,c)
main()
{iii. x= f(f(a))
iv. int
x= i,++f(a)
a[10]; [8+8]
while (i =10) a[i] =0
}
1 of 21
Code No: R05320502
Set No. 3
III B.Tech II Semester Regular Examinations, Apr/May 2008
COMPILER DESIGN
(Computer Science & Engineering)
Time: 3 hours Max Marks: 80
Answer any FIVE Questions
All Questions carry equal marks
4. (a) Write the quadruple, triple, indirect triple for the statement a := b * -c+b * -c
(b) Explain the role of intermediate code generator in compilation process. [8+8]
5. (a) What is an ordered and unordered symbol table? What is the function of
symbol table in the compliation process? Explain.
(b) What are the various attributes of a Symbol Table? [10+6]
1 of 2
Code No: R05320502
Set No. 3
(b) Generate optimal machine code for the following C program. [6+10]
main()
{
int i, a[10];
while (i =10) a[i] =0
}
2 of 2
Set No. 4
Code No: R05320502
1. (a) Explain the di erent phases of a compiler, showing the output of each phase,
using the example of the following statement:
position : = initial + rate * 60
(b) Compare compiler and interpreter with suitable diagrams. [10+6]
2. (a) Give the rules for computation of FIRST(X) and FOLLOW(X). Construct
FIRST and FOLLOW sets for the following grammar.
E TE
E +T E |
T FT
T FT|
F (E)|id
(b) Write an algorithm for construction of predictive parsing table. [10+6]
5. (a) Explain the hash table with temporary and permanent storage.
(b) Reusing the storage space for names. [8+8]
6. (a) What is DAG? Construct the DAG for the following basic block
D := B C
E :=A+B
B := B+C
A := E-D
(b) What are the legal evaluation orders and names for the values at the nodes
for the DAG of problem (a).
i. Assuming A, B and C are alive at the end of the basic block?
ii. Assuming only A is live at the end? [6+10]
1 of 21
Code No: R05320502
Set No. 4
8. (a) Describe, how addressing modes can be used for reducing the memory access
time
(b) Generate the co de sequence using Code generation algorithm for the following
expression [8+8]
W:=(A-B)+(A-C)+(A-C)
2 of 2
III).INTERNAL QUESTION PAPERS :
1. (a) Explain the di erent phases of a compiler, showing the output of each phase,
using the example of the following statement:
position : = initial + rate * 60
(b) Compare compiler and interpreter with suitable diagrams. [10+6]
(a) An array of pointers to reals, where array index ranages from 1 to 100.
(b) A two dimensional array of integers (i.e. an array of array) whose rows are
indexed from 0 to 9 and whose columns are indexed from -10 to 10.
(c) Functions whose domains are functions from integers to pointers to integers
and whose ranges are records consisting of an integer and a character. [5+5+6]
6. (a) What is DAG? Construct the DAG for the following basic block
D := B * C
E :=A+B
B := B+C
A := E-D
(b) What are the legal evaluation orders and names for the values at the nodes
for the DAG of problem (a).
i. Assuming A, B and C are alive at the end of the basic block?
ii. Assuming only A is live at the end? [6+10]
7. (a) Explain in detail the procedure that eliminate global common sub - expression.
21 of 2
Code No: R05320502
Set No. 2
III B.Tech II Semester Regular Examinations, Apr/May 2009
COMPILER DESIGN
(Computer Science & Engineering)
Time: 3 hours Max Marks: 80
Answer any FIVE Questions
All Questions carry equal marks
2. (a) What is recursive descent parser? Construct recursive descent parser for the
following grammar.
E E + T|T
T TF|F
F F * |a|b
(b) What is ambiguous grammar? Eliminate ambiguities for the grammar:
E E + E|E * E|(E)|id. [8+8]
4. (a) Which of the following recursive type expressions are equivalent? Justify your
answer?
e1 =integer e 1
; e 2 =integer ( integer e 2
); e 3 =integer ( integer
e1 ).
(b) Suppose that the type of each identifier is a sub range of integers for expressions
with the operators +,-,*,div and mod as in pascal. Write type-checking rules
that assign to each sub expression, the subrange its value must lie in.
[8+8]
5. Draw and explain the symbol table organization for C language with a program
block. [16]
7. Consider the following program which counts the primes form 2 to n using the sieve
method on a suitably large array
begin
read n
for i : = 2 to n do
a[i] : = true / * initialize */
count: = 0;
1 of 2
Code No: R05320502
Set No. 2
for i : 2 to n ** .5 do
if a [i] then /* i is a prime */
begin
count := count +1
for j : = 2 * i to n by i do
a[j] : =false
/ * j is divisible by i */
end ;
print count
end
8. (a) What are the various addressing modes available? GIve some example machine
instructions which reduces memory access time.
(b) Explain the concept of label tree of code generation. [8+8]
2 of 2
Code No: R05320502
Set No. 3
III B.Tech II Semester Regular Examinations, Apr/May 2009
COMPILER DESIGN
(Computer Science & Engineering)
Time: 3 hours Max Marks: 80
Answer any FIVE Questions
All Questions carry equal marks
3. (a) What are the common con icts that can be encountered in shift - reduce
parser.
(b) Construct SLR parsing table for the following grammar.
| | | |( ) | | [8+8]
4. (a) What is a syntax tree? Write syntax directed definition for constructing a
syntax tree for an expression. The grammar for an expression is given below.
+|-|
()||
(b) Write a detail notes on type conversion. [8+8]
1 of 2
Code No: R05320502
Set No. 3
(b) Write an algorithm for partition of basic blocks and apply it on the above
derived three-address code. [8+8]
2 of 2
Code No: R05320502
Set No. 4
III B.Tech II Semester Regular Examinations, Apr/May 2009
COMPILER DESIGN
(Computer Science & Engineering)
Time: 3 hours Max Marks: 80
Answer any FIVE Questions
All Questions carry equal marks
2. (a) What are the di culties in top down parsing? Explain in detail.
(b) Consider the following grammar
()|
|
Construct leftmost derivations and parse trees for the following sentences:
i. (a,(a,a))
ii. (a,((a,a),(a,a))). [8+8]
4. (a) Write a S - attributed grammar to connect the fopllowing grammar with prefix
rotator
L E
E E+T | E-T | T
T T*F | T/F | F
F P F|P
P (E)
P id.
(b) Construct triples of an expression: * - ( + ). [8+8]
1 of 2
Code No: R05320502
Set No. 4
(b) What is a DAG. Explain its applications. [8+8]
2 of 2
15. EXPERT LEATURES/ SEMINARS
16. LATEST FINDINGS :
17. LAB JOUNALS AND MANUALS
18. RESULT ANALYSIS ON INTERNAL EXAMINATIONS(GRADING OF
STUDENTS INTO A/B/C CATEGORIES)