Rift Valley University Department of Computer Science
Rift Valley University Department of Computer Science
• The compiler also makes the end code efficient which is optimized for
execution time and memory space.
• In single pass Compiler, source code directly transforms into machine code.
• The Two pass compiler method also simplifies the retargeting process.
• It also allows multiple front ends.
05/25/2020 Fikru Tafesse(MSc. in Computer Science) 6
… Cont’d
3. Multipass Compilers
• The multipass compiler processes the source code or syntax tree of a program
several times.
• It divided a large program into multiple small programs and process them.
• All of these multipass take the output of the previous phase as an input.
• It is05/25/2020
also known as Wide Compiler.
Fikru Tafesse(MSc. in Computer Science) 7
Tasks of Compiler
• Main tasks performed by the Compiler are:
• Breaks up the source program into pieces and impose grammatical structure on
them
• Allows you to construct the desired target program from the intermediate
representation and also create the symbol table
• Compiles source code and detects errors in it
• Manage storage of all variables and codes.
• Support for separate compilation
• Read, analyze the entire program, and translate to semantically equivalent
• 05/25/2020
Translating the source code into object code depending upon the type of machine
Fikru Tafesse(MSc. in Computer Science) 8
History of Compiler
• Important Landmark of Compiler's history are as follows:
The "compiler" word was first used in the early 1950s by Grace Murray Hopper
The first compiler was build by John Backum and his group between 1954 and 1957 at
IBM
COBOL was the first programming language which was compiled on multiple platforms
in 1960
The study of the scanning and parsing issues were pursued in the 1960s and 1970s to
provide a complete solution
• Platform independent
• The techniques used for constructing a compiler can be useful for other
purposes as well
Lexical Analyzer
Syntax Analyzer
Code optimizer
Code Generator
Target Program Fig: Phases of Compiler
15
• A source program may be divided into modules stored in separate
files. Phases …
1. Lexical Analysis:
• Lexical analyzer phase is the first phase of compilation process.
• It takes source code as input.
• It reads the source program one character at a time and converts it into
meaningful lexemes.
• Lexical analyzer represents these lexemes in the form of tokens.
2. Syntax Analysis
• Syntax analysis is the second phase of compilation process.
• It takes tokens as input and generates a parse tree as output.
• In syntax analysis phase, the parser checks that the expression made by the
tokens is syntactically correct or not. 16
3. Semantic Analysis Phases …
• Semantic analysis is the third phase of compilation process.
• It checks whether the parse tree follows the rules of language.
• Semantic analyzer keeps track of identifiers, their types and expressions.
• The output of semantic analysis phase is the annotated tree syntax.
4. Intermediate Code Generation
• In the intermediate code generation, compiler generates the source code into
the intermediate code.
• Intermediate code is generated between the high-level language and the
machine language.
• The intermediate code should be generated in such a way that you can easily
translate it into the target machine code.
17
5. Code Optimization Phases …
• Code optimization is an optional phase.
• It is used to improve the intermediate code so that the output of the
program could run faster and take less space.
• It removes the unnecessary lines of the code and
• arranges the sequence of statements in order to speed up the program
execution.
6. Code Generation
• Code generation is the final stage of the compilation process.
• It takes the optimized intermediate code as input and maps it to the target
machine language.
• Code generator translates the intermediate code into the machine code of
the specified computer. 18
Phases …
7. Symbol Table Manager
• This is the portion to keep the names used by the program and records
essential information about each.
• The data structure used to record this information called a symbol table
8. Error Handler
19
Example: Phases …
20
Cousins of Compiler
• The input to compiler may produced by one or more preprocessors, and
further processing of compiler’s output may be needed before running
machine code obtained.
Preprocessor
Source Program
Compiler
Target Assembly Program
Assembler
Relocatable Machine Code
• In the first part, the source program compiled and translated into the
object program (low level language).
• In the second part, object program translated into the target program
through the assembler.
26
Interpreter
• An interpreter is a program that appears to execute a source program as if it
were machine language.
• Also the programmer would have to retranslate his program with each
execution, thus wasting translation time.
• A loader is a program that places programs into memory and prepares them
for execution.
• It would be more efficient if subroutines could be translated into object form
the loader could relocate directly behind the user’s program.
• The task of adjusting programs they may be placed in arbitrary core locations
is called relocation.
30
• Linking and Loading has four functions: Loader …
1. Allocation:
• It means get the memory portions from operating system and storing the
object data.
2. Relocation:
• It maps the relative address to the physical address and relocating the
object code.
3. Linker:
• It combines all the executable object module to pre single executable file.
4. Loader:
• It loads the executable file into permanent storage.
31
Grouping of Phases
• Front end : machine independent phases
• Lexical analysis
• Syntax analysis
• Semantic analysis
• Intermediate code generation
• Some code optimization
• Back end : machine dependent phases
• Final code generation
• Machine-dependent optimizations 32
Compiler Construction Tools
• These are specialized tools that have been developed for helping implement
various phases of a compiler. The following are the compiler construction tools:
1. Parser Generators: These produce syntax analyzers, normally from input that
is based on a context-free grammar.
• It consumes a large fraction of the running time of a compiler.
• Example: YACC (Yet Another Compiler-Compiler).
34
… Tools
5. Data-Flow Engines: It does code optimization using data-flow analysis,
that is, the gathering of information about how values are transmitted from one
part of a program to each other part.
? ?
?
s , …
End e nt
of C m
hapt o m
er 1 . ,C
. We ns
ll Do t i o
ne es
Qu
35