19ecs332: Compiler Design
19ecs332: Compiler Design
L T P C
3 0 2 4
Compilers play a significant role in fulfilling user’s computing requirements, specified in terms of
programs in a high-level language, which translate into machine-understandable form. The process
involved in such a transformation of a program is quite complex. This course enables the students to
gain knowledge on various phases involved in designing a compiler. Automata Theory and Formal
Languages provides the basis for this course in which several automated tools help construct various
phases. Advanced computer architecture, memory management, and operating systems help the
compiler designer generate efficient code.
Course objectives:
Explore the basic techniques that underlie the principles, algorithms and data structures
involved in the Compiler Construction.
Gain experience in using automated tools that helps in transforming various phases of the
compiler.
Learning Outcomes:
After completion of this unit, the student will be able to
summarizing various phases involved in the design of compiler construction(L2)
comparing methods involved in constructing the compiler(L2)
highlighting how regular expressions help to design the Lexical Analysis phase(L1)
exploring how LEX Tool simplifies the design of the Lexical Analysis phase(L2)
Learning Outcomes:
1
After completion of this unit, the student will be able to
2
identifying the issues involved in designing efficient Bottom-Up (LR) Parses(L1)
predicting the role of look-ahead in LR parsers(L2)
illustrating the significance of using ambiguous grammars in LR Parses(L4)
assessing how YACC Tool simplifies the design of Parser(L5)
UNIT IV: Syntax Directed Translation & Intermediate Code Generation 8+2 L+P
Syntax Directed Translation: Syntax Directed Definitions, Evaluation Orders for SDD’s,
Applications of Syntax-Directed Translation.
Intermediate Code Generation: Three Address codes, Types & Declarations, Translation of
Arithmetic Expressions, Back patching for Boolean Expressions, and Flow of Control.
Learning Outcomes:
After completion of this unit, the student will be able to
summarizing the notational framework of Syntax Directed Definitions(L2)
designing Syntax Directed Definitions to generate intermediate code(L3)
illustrating various techniques to store three address statements(L3)
designing Syntax Directed Definitions for Boolean expressions using back patching (L3)
Learning Outcomes:
After completion of this unit, the student will be able to
identifying issues involved in the machine-independent code optimization(L1)
illustrating with a suitable example on local and loop optimization(L4)
determining the issues involved in global optimization techniques(L3)
estimating the processes involved in obtaining the final code, register allocation, and
assignment(L2)
Text Books(s)
1. Alfred.V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey. D. Ullman, Compilers Principles,
Techniques and Tools, 2/e, Pearson Education, 2008.
References
1. Alfred.V. Aho, J.D.Ullman, Principles of compiler design, Narosa Publications, 2002
2. John R. Levine, Tony Mason, Doug Brown, Lex &yacc, O’reilly ,2/e, 1992
3. Keith Cooper, Linda Torczon, Engineering a compiler, Morgan Kaufmann, 2/e, 2011.
Course Outcomes:
After completion of this course, the student will be able to
define and analyse various phases involved in designing a compiler(L1)
compare and contrast between bottom-up and top-down parsing techniques(L2)
illustrate the usage of Syntax Directed Definition in generating intermediate code(L3)
identify various machine independent optimization techniques(L4)
explore techniques involved in obtaining the final code(L4)
3
COMPILER DESIGN LABORATORY
For Laboratory courses: List of Experiments:
1. Implement transition diagram for identifying an identifier and classify whether it is either
variable or array or function or structure.
2. Implement transition diagram for identifying constant and classify whether it is integer or
real.
3. Write a LEX specification which takes input from a file (a C’ program) and recognize valid
identifiers, keywords contained in the program and store them in a file.
4. Write a LEX specification for lexical analyzer of C’ language in which it recognizes all
possible tokens in a given program taken as a file and output all the tokens into another
file.
5. The production “A → α1 / α2 /α3 ...” of the CFG uses the following structure/class
representation.
struct/class production
{
char left, //Head of the production ie., left hand side
char right(10)(10), //body of prod ie., all alternatives are represent as array of
strings int noa, //noa means no of alternatives in the production
},
6. Write a program to implement Recursive Descent Parser for any given grammar.
8. Consider the following Expression language that used to describe the Arithmetic expressions
in a Calculator The syntax of the language is defined by following grammar
<line> <exp>\n
<exp> <exp>+ <term>| <exp>-<term>|<term>
<term> <term>*<factor>|<term>/<factor>/<factor>
<factor> (<exp>)|<value>
<value> <letter>|<digit>
A sample arithmetic expression written in this language is(a+b)*c\n or 2 + 4 * 5 -6 /2\n
a. Design a LEX specification for the above language. (Ignore redundant spaces, tabs and
newlines). Although the syntax specification states that value can be arbitrarily long, you
may restrict the length to some reasonable value. (Implement the lexical analyser using
JLex, flex or lex or any other tools)
b. Write YACC Specification to validate any given arithmetic expression accepted by the
above grammar.
c. Write YACC specification to evaluate the given arithmetic expression accepted by the
above grammar.
d. Write YACC specification to generate three address code and quadruples for any given
expression.
e. Write YACC specification to convert an infix expression to a postfix expression.
9. Consider the following grammar which is used to describe the X language which might be
used in next generation programmable calculators. It supports integer, real and complex
numbers This language uses something called Hungarian notation the name of the variable
itself tells you about the type of the data it contains if the starting letter is 'i’ then integer , 'r'
then real , 'c' then complex number
<program> begin<stmts>end
<stmts> <statement>,<stmts>| <statement>,
<statement> <identifer>=<expr> | <conditional>
<expr> <expr> + <term> |<expr>-<term>|<term>
<term> <term>*<fact>|<fact>
<fact> <identifier><conditional>--->if <cexpr>thenbegin<stmts>end
<cexpr> <identifier>==<identifier> |<identifier>!=<identifier>
<identifier> i<letters>|r<letters>|c<letters>
<letters> <letter><letters>|<letter>
<letter> a|b|....|z|A|B|.....|Z
a. Design a LEX specification for the above language. (Ignore the redundant spaces, tabs and
newline Although the syntax specification states that value can be arbitrarily long, you may restrict
the leng to some reasonable value. (Use JLex, flex or lex or any other lexical analyser generating
tools).
b. Write YACC specification to generate three address code and quadruples for the given
arithmetic statement.
5
c. Write YACC specification to validate the statements of the above language.
d. Write program to generate 8086 assembly code from the abstract syntax tree or three address
code generated by the parser. The target assembly instructions can be simple move, add, sub,
and jump. Also simple addressing modes are used.