Introduction To Compilers - Analysis of The Source Program - Phases of A Compiler Phases of Compiler
Introduction To Compilers - Analysis of The Source Program - Phases of A Compiler Phases of Compiler
Introduction To Compilers - Analysis of The Source Program - Phases of A Compiler Phases of Compiler
Translator:
It is a program that translates one language to another.
Types of Translator:
1.Interpreter
2.Compiler
3.Assembler
INRODUCTION TO COMPILING
Interpreter:
It is one of the translators that translate high level language to
low level language.
Assembler:
It translates assembly level language to machine code.
.
Example: Microprocessor 8085, 8086
INRODUCTION TO COMPILING
Compiler:
It is a program that translates one language(source code) to
another language (target code).
-It executes the whole program and then displays the errors.
Example: C, C++, COBOL, higher version of Pascal.
Difference between compiler and interpreter:
Compiler Interpreter
It is a translator that translates high level to It is a translator that translates high level to low
low level language level language
It displays the errors after the whole program is It checks line by line for errors.
executed.
Examples: Basic, lower version of Pascal. Examples: C, C++, Cobol, higher version of
Pascal.
PARTS OF COMPILATION
• Pretty printers :
– A pretty printer analyzes a program and prints it in such a way
that the structure of the program becomes clearly visible.
– For example, comments may appear in a special font.
Software tools used in Analysis part:
• Static checkers :
– A static checker reads a program, analyzes it, and attempts to
discover potential bugs without running the program.
– For example, a static checker may detect that parts of the source
program can never be executed.
• Interpreters :
– Translates from high level language ( BASIC, FORTRAN, etc..) into
machine language.
– An interpreter might build a syntax tree and then carry out the
operations at the nodes as it walks the tree.
– Interpreters are frequently used to execute command language
since each operator executed in a command language is usually
an invocation of a complex routine such as an editor or complier.
ANALYSIS OF THE SOURCE PROGRAM
Analysis consists of 3 phases:
• Linear/Lexical Analysis :
It is also called scanning.
– It is the process of reading the characters from left to right and grouping
into tokens having a collective meaning.
For example, in the assignment statement a=b+c*2, the characters would be
grouped into the following tokens:
The identifier1 ‘a’
The assignment symbol (=)
The identifier2 ‘b’
The plus sign (+)
The identifier3 ‘c’
The multiplication sign (*)
The constant ‘2’
ANALYSIS OF THE SOURCE PROGRAM
• Syntax Analysis :
– It is called parsing or hierarchical analysis.
– It involves grouping the tokens of the source
program into grammatical phrases that are used
by the compiler to synthesize output.
They are represented using a syntax tree as shown below:
a=b+c*2
LEXICAL ANALYSIS:
• SYNTAX ANALYSIS:
– It is the second phase of the compiler. It is also known as
parser.
– It gets the token stream as input from the lexical analyser
of the compiler and generates syntax tree as the output.
– Syntax tree:
• SEMANTIC ANALYSIS:
– It is the third phase of the compiler.
– It gets input from the syntax analysis as parse
tree and checks whether the given syntax is
correct or not.
– It performs type conversion of all the data types
into real data types.
•
PHASES OF COMPILER
• CODE OPTIMIZATION:
– It is the fifth phase of the compiler.
– It gets the intermediate code as input and produces optimized
intermediate code as output.
– This phase reduces the redundant code and attempts to improve the
intermediate code so that faster-running machine code will result.
– During the code optimization, the result of the program is not
affected.
– To improve the code generation, the optimization involves
• deduction and removal of dead code (unreachable code).
• calculation of constants in expressions and terms.
• collapsing of repeated expression into temporary string.
• loop unrolling.
• moving code outside the loop.
• removal of unwanted temporary variables.
•
PHASES OF COMPILER
• CODE GENERATION:
– It is the final phase of the compiler.
– It gets input from code optimization phase and
produces the target code or object code as result.
– Intermediate instructions are translated into a sequence
of machine instructions that perform the same task.
– The code generation involves
• allocation of register and memory
• generation of correct references
• generation of correct data types
• generation of missing code
PHASES OF COMPILER
• ERROR HANDLING:
– Each phase can encounter errors. After detecting an error, a
phase must handle the error so that compilation can proceed.
– In lexical analysis, errors occur in separation of tokens.
– In syntax analysis, errors occur during construction of syntax
tree.
– In semantic analysis, errors occur when the compiler detects
constructs with right syntactic structure but no meaning and
during type conversion.
– In code optimization, errors occur when the result is affected by
the optimization.
– In code generation, it shows error when code is missing etc.
To illustrate the translation of source code through each phase, consider the
statement a=b+c*2. The figure shows the representation of this statement after each
phase: