Professional Documents
Culture Documents
CH1 - Introduction
Outline
• Introduction – Definition
• Why you study about compilers?
• Classification of Compilers
• Cousins of Compilers
– Language Processing System
– Compilers vs Interpreters
• Basic Compiler Design
– The Analysis – Synthesis Model of Compilation
• Phases of a Compiler
• Compiler Construction Tools
Introduction - Definition
• Compiler is an executable program that can read a program in one high-
level language and translate it into an equivalent executable program in
machine language.
• A compiler is a computer program that translates an executable program
in a source language into an equivalent program in a target language.
• 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
Basic Compiler Design
• Involves writing a huge program that takes as input another
program in the source language for the compiler, and gives
as output an executable that we can run.
– For modifying code easily, usually, we use modular design
(decomposition) methodology to design a compiler.
• There are two design strategies:
1. Write a “front end” of the compiler (i.e. the lexer, parser,
semantic analyzer, and assembly tree generator), and write a
separate back end for each platform that you want to support
2. Write an efficient highly optimized “back end”, and write a
different front end for several languages, such as Fortran, C, C+
+, and Java.
Basic Compiler Design
The Analysis-Synthesis Model of Compilation
• There are two parts to compilation: analysis & synthesis.
– During analysis, the operations implied by the source program are
determined and recorded in a hierarchical structure called a tree.
– During synthesis, the operations involved in producing translated
code. Breaks up source program into constituent
pieces
Creates intermediate representation of
source program
• Compiler makes a second pass over the parse tree to produce the
translated code
– If there are no compile-time errors, the semantic analyzer translates the
abstract syntax tree into the abstract assembly tree
– The abstract assembly tree will be passed to the code optimization and
assembly code generation phase
Phases of a Compiler
Phase IV: Intermediate Code Generation
• Using intermediate code is beneficial when compilers
which translates a single source language to many
target languages are required.
– The front-end of a compiler:- scanner to intermediate code
generator can be used for every compilers.
– Different back-ends:- code optimizer and code generator is
required for each target language.
• One of the popular intermediate code is three-address
code.
• A three-address code instruction is in the form of
x = y op z
Phases of a Compiler
Phase V: Assembly Code Generation
• Code generator coverts the abstract assembly tree into the
actual assembly code
• To do code generation
– The generator covers the abstract assembly tree with tiles (each
tile represents a small portion of an abstract assembly tree) and
• Output the actual assembly code associated with the tiles
that we used to cover the tree
Phase VI: Machine Code Generation and Linking
• The final phase of compilation coverts the assembly
code into machine code and links (by a linker) in
appropriate language libraries
Code Optimization
• Is a process of replacing an inefficient sequence of instructions with a
better sequence of instructions.
• Sometimes called code improvement.
• Code optimization can be done:
– after semantic analyzing
performed on a parse tree
– after intermediate code generation
performed on a intermediate code
– after code generation
performed on a target code
• There are two types of code optimizations
1. Local Optimization
2. Global Optimization
Code Optimization
1. Local Optimization
– The compiler looks at a very small block of instructions and tries to determine
how it can improve the efficiency of this local code block.
– Relatively easy; included as part of most compilers
– Examples of possible local optimizations
1. Constant evaluation
2. Strength reduction
3. Eliminating unnecessary operations
2. Global Optimization
– The compiler looks at large segments of the program to decide how to
improve performance
– Much more difficult; usually omitted from all but the most sophisticated and
expensive production-level “optimizing compilers”
– Optimization cannot make an inefficient algorithm efficient
Summary of Phases of a Compiler
Phase Output Sample
Programmer (source code Source string A=B+C;
producer)
Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
And symbol table with names
;
Parser (performs syntax analysis Parse tree or abstract |
based on the grammar of the syntax tree =
/\
programming language) A +
/\
B C