ch1-intro1
ch1-intro1
Construction
Dr. Preeti Kaur
Syllabus
• Prerequisites: Computer Architecture, Data Structure, Microprocessor
2
COURSE CONTENTS
Unit 1
Introduction: Language processors, structure of a compiler, compiler-construction tools, evolution of programming
languages, applications of compiler technology, Transition diagrams, bootstrapping, just-in-time compilation.
Unit 2
Lexical analysis: Input buffering, specification and recognition of tokens, lexical analyzer generator.
Unit 3
Syntax analysis: Specification of syntax using grammar. Top-down parsing – recursive-descent, predictive. Bottom-up
parsing – shift-reduce, SLR, CLR, LALR. Parser generator.
Unit 4
Unit 5
Code optimization: Sources of optimization, basic blocks, optimization of basic blocks, data-flow analysis, loop
optimizations. Code generation: Issues, register allocation and assignment, peephole optimization
3
Practicals
• Develop simple language processors like desk calculator and
assembler.
• Design a small high-level language.
• Develop a lexical analyzer and a syntax analyzer for the same using
the LEX and YACC tools. Also implement the bookkeeper module.
• Design a small high-level language and implement a compiler for the
same. If the target machine of the compiler is a hypothetical machine,
then implement a simulator for it.
• Develop a simple calculator using LEX and YACC tools.
• Implement a program for symbol table using hashing
• Implement a two-pass assembler
• Implement a bottom-up parser using YACC tool.
• Represent ‘C’ language using Context Free Grammar
• Add assignment statement, If then else statement and while loop to the
calculator and generate the three address code for the same.
4
SUGGESTED READINGS
• Aho, A. V., Lam, M. S., Sethi, R. and Ullman J. D.,
“Compilers – Principles,Techniques and Tools (2nd ed.)”,
Pearson.
• Chattopadhyay, S. 2005, “Compiler Design, PHI”.
• Appel, A. W. 200, “Modern Compiler Implementation in
C", Cambridge University Press.
• Kenneth C. Louden (1997), Compiler Construction–
Principles and Practice, 1st edition, PWS Publishing.
5
Objectives
• To learn about translators
• To build a compiler for a programming language
• To learn and use compiler construction tools, such as generators of
scanners and parsers
• To learn about grammars and to define LL(1), LR(1), and LALR(1)
grammars
• To learn compiler analysis and optimization techniques
6
What do we expect to achieve by the end of
the course?
• Knowledge to design, develop, understand, modify/enhance, and
maintain compilers for (even complex!) programming languages
• Confidence to use language processing technology for software
development
Organization of the course
CACSC14 is a 3 2 0 course i.e.
Marks for
• TCA Theory Continuous assesment-15
5 (class test)+5(assignment)+5(attendance)
• TMS Mid semester exam -15
• TES End semester exam -40
• PCA Practical Continuous assesment-15
• PES Practical End semester exam-15
Compilers and Interpreters
• “Compilation”
• Translation of a program written in a source language into a semantically
equivalent program written in a target language
• simplified view:
Input
Source Target
Compiler
Program Program
Source
Program
Interpreter Output
Input
Error messages 10
Compiler vs. Interpreter
11
Compiler vs. Interpreter
Ideal concept:
Source code
Interpreter Output data
Input data
12
Compiler vs. Interpreter
◼ Most languages are usually thought of as using either one or
the other:
◼ Compilers: FORTRAN, COBOL, C, C++, Pascal, PL/1
◼ Interpreters: Lisp, scheme, BASIC, Perl, Python, Smalltalk
13
Compiler vs. Interpreter
Compiler Interpreter
◼ Pros ◼ Pros
◼ Less space ◼ Easy debugging
◼ Fast execution ◼ Fast Development
◼ Cons ◼ Cons
◼ Slow processing ◼ Not for large projects
◼ Partly Solved ◼ Exceptions: Perl, Python
(Separate compilation) ◼ Requires more space
◼ Debugging ◼ Slower execution
◼ Improved thru IDEs(integrated ◼ Interpreter in memory all the time
development environment)
14
Compiler vs. Interpreter
15
The Analysis-Synthesis Model of Compilation
16
Other Tools that Use the Analysis-Synthesis
Model
• Editors (syntax highlighting)
• Pretty printers (e.g. Doxygen)
• Static checkers (e.g. Lint and Splint)
• Interpreters
• Text formatters (e.g. TeX and LaTeX)
• Silicon compilers (e.g. VHDL)
• Query interpreters/compilers (Databases)
17
Preprocessors, Compilers, Assemblers, and
Linkers
Skeletal Source Program
Preprocessor
Source Program
Compiler
Target Assembly Program
Assembler
Relocatable Object Code
Linker Libraries and
Relocatable Object Files
18
Absolute Machine Code
The Phases of a Compiler
Phase Output Sample
Programmer (source code producer) Source string A=B+C;
Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
And symbol table with names
Parser (performs syntax analysis Parse tree or abstract syntax tree ;
|
based on the grammar of the =
programming language) / \
A +
/ \
B C
23
Compiler-Construction Tools
• Software development tools are available to implement one or more
compiler phases
• Scanner generators
• Parser generators
• Syntax-directed translation engines
• Automatic code generators
• Data-flow engines
24
Outline
• Introduction
• Lexical Analysis and Lex/Flex
• Syntax Analysis and Yacc/Bison
• Syntax-Directed Translation
• Type Checking
• Intermediate Code Generation
• Target Code Generation
• Code Optimization
25
• Symbol Table
• Error Recovery
26
Learning in this course
• High level languages to machine code conversion
• Both theoretical and practical components are covered to implement programming languages
• Algorithms and data structures are studied which are required to understand the process of compilation
• Theory of lexical analysis, parsing, type checking, runtime system, code generation, optimization
• Use of tools and specifications for developing various parts of compilers are also covered in the course
27