You are on page 1of 16

Compiler Design

Text book: Alfred V. Aho, Ravi Sethi, and Jeffrey D.


Ullman,“Compilers: Principles, Techniques, and Tools”Addison-Wesley,
1986.
COMPILERS
 A compiler is a program takes a program written in a source language
and translates it into an equivalent program in a target language.

source program COMPILER target program


( Normally a program written ( Normally the equivalent
in a high-level programming program in machine code –
language) relocatable object file)
error messages

Other Applications
 In addition to the development of a compiler, the techniques used in compiler design can be applicable to many
problems in computer science.
 Techniques used in a lexical analyzer can be used in text editors, information retrieval system, and pattern
recognition programs.
 Techniques used in a parser can be used in a query processing system such as SQL.
 Many software having a complex front-end may need techniques used in compiler design.
 A symbolic equation solver which takes an equation as input. That program should parse the given input
equation.
 Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems.
 An interpreter reads an executable source program written in a
high-level programming language as well as data for this program,
and it runs the program against the data to produce some results.
 One example is the Unix shell interpreter, which runs operating
system commands interactively
Cousins of compiler
 Preprocessor(Macro processing, file inclusion, Rational preprocessor, Language extensions)
 Assembler
 Linkers-A linker takes several object les and libraries as input and produces one executable object file. It
retrieves from the input files (and puts them together in the executable object file) the code of all the referenced
functions/procedures and it resolves all external references to real addresses. The libraries include the operating
system libraries, the language-specic libraries, and, maybe, user-created libraries.

 Loaders
 Debuggers-used to determine execution errors in a compiled program
 Profiler- Collects Statistics on the behavior of the object program during execution
 Project Managers-used to coordinate and merge the source code produced by different members of the
team
What is the Challenge in compiler Design?
Many variations:
 many programming languages (eg, FORTRAN, C++, Java)
 many programming paradigms (eg, object-oriented, functional, logic)
 many computer architectures (eg, MIPS, SPARC, Intel, alpha)
 many operating systems (eg, Linux, Solaris, Windows)

Qualities of a compiler (in order of importance):


 the compiler itself must be bug-free
 it must generate correct machine code
 the generated machine code must run fast
 the compiler itself must run fast (compilation time must be proportional to program size)
 the compiler must be portable (ie, modular, supporting separate compilation)
 it must print good diagnostics and error messages
 the generated code must work well with existing debuggers
 must have consistent and predictable optimization.

Building a compiler requires knowledge of


 programming languages (parameter passing, variable scoping, memory allocation, etc)
 theory (automata, context-free languages, etc)
 algorithms and data structures (hash tables, graph algorithms, dynamic programming etc)
 computer architecture (assembly programming)
 software engineering.
Front end : machine independent
phases/Language dependent
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate code generation
Some code optimization
Back end : machine dependent
phases/Language Independent
Final code generation
Machine-dependent
optimizations

Analysis:
Lexical analysis
Syntax analysis
Semantic analysis
Synthesis:
Intermediate code generation
code optimization
Final code generation
Storage a
position := initial + rate * 60

intermediate code generator

lexical analyzer

temp1 := inttoreal (60)


id1 := id2 + id3 * 60 temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3

syntax analyzer

The Phases of a Compiler


:= code optimizer
id1
+

id2
* temp1 := id3 * 60.0
id3 id1 := id2 + temp1

60

semantic analyzer
code generator

:= MOVF id3, R2
MULF #60.0, R2
id1 + MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
id2 *

id3 inttoreal
60

You might also like