Chapter 1 - Intro
Chapter 1 - Intro
Course Introduction
Course Materials
◼ Dragon Book
◼ Aho, Lam, Sethi, Ullman, “Compilers: Principles,
Techniques, and Tools”, 2nd ed, Addison 2007
200px-Purple_dragon_book_b
Compiler Review
What is a Compiler?
◼ A program that translates a program in one
language to another language
◼ The essential interface between applications &
architectures
◼ Typically lowers the level of abstraction
◼ analyzes and reasons about the program & architecture
◼ We expect the program to be optimized, i.e., better
than the original
◼ ideally exploiting architectural strengths and hiding
weaknesses
Compiler vs. Interpreter (1/5)
◼ Compilers: Translate a source (human-
writable) program to an executable (machine-
readable) program
Ideal concept:
Source code
Interpreter Output data
Input data
Compiler vs. Interpreter (3/5)
◼ 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, APL, Perl,
Python, Smalltalk
◼ BUT: not always implemented this way
◼ Virtual Machines (e.g., Java)
◼ Linking of executables at runtime
◼ JIT (Just-in-time) compiling
Compiler vs. Interpreter (4/5)
◼ Actually, no sharp boundary between them.
General situation is a combo:
Intermed. code
Virtual machine Output
Input Data
Compiler vs. Interpreter (5/5)
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 ◼ Interpreter in memory all
the time
Phase of compilations
1
1
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
Compiler-Construction Tools
◼ Software development tools are available to
implement one or more compiler phases
◼ Scanner generators
◼ Parser generators
◼ Data-flow engines
Scanning/Lexical analysis
◼ Break program down into its smallest
meaningful symbols (tokens, atoms)
◼ Tools for this include lex, flex
◼ Tokens include e.g.:
◼ “Reserved words”: do if float while
◼ Special characters: ( { , + - = ! /