Compiler Notes Introduction
Compiler Notes Introduction
Error report
Fig.1.1. A Compiler
Major functions done by compiler:
Compiler is used to convert one form of program to another.
A compiler should convert the source program to a target machine code in such a way
that the generated target code should be easy to understand.
Compiler should preserve
preserv the meaning of source code.
Compiler should report errors that occur during compilation process.
The compilation must be done efficiently.
+
a
a *
b *
c 2
Semantic analysis:
Semantic analyzer determines the meaning of a source string.
For example matching of parenthesis in the expression, or matching of if..else
statement or performing arithmetic operation that are type compatible, or checking the
scope of operation.
=
+
a
a *
b *
c 2
Int to float
Synthesis phase:: synthesis part is divided into three sub parts,
I. Intermediate code generation
II. Code optimization
III. Code generation
Intermediate code generation:
The intermediate representation should have two important properties, it should be
Dixita Kagathara, CE Department | 170701 – Compiler Design 3
Unit 1 - Introduction
easy to produce and easy to translate into target program.
We consider intermediate form called “three address code”.
Three address code consist of a sequence of instruction, each of which has at most
three operands.
The source program might appear in three address code as,
t1= int to real(2)
t2= id3 * t1
t3= t2 * id2
t4= t3 + id1
id1= t4
Code optimization:
The code optimization phase attempt to improve the intermediate code.
This is necessary to have a faster executing code or less consumption of memory.
Thus by optimizing the code the overall running running time of a target program can be
improved.
t1= id3 * 2.0
t2= id2 * t1
id1 = id1 + t2
Code generation:
In code generation phase the target code gets generated. The intermediate code
instructions are translated into sequence of machine instruction.
instructio
MOV id3, R1
MUL #2.0, R1
MOV id2, R2
MUL R2, R1
MOV id1, R2
ADD R2, R1
MOV R1, id1
Symbol Table
A symbol table is a data structure used by a language translator such as a compiler or
interpreter.
It is used to store names encountered in the source program, along with the relevant
attributes for those names.
Information
tion about following entities is stored in the symbol table.
Variable/Identifier
Procedure/function
Keyword
Constant
Class name
Label name
Source program
Lexical Analysis
Syntax Analysis
Semantic Analysis
Symbol Table Error detection
and recovery
Intermediate Code
Code Optimization
Code Generation
Target Program
Skeletal source
Preprocessor
Source program
Compiler
Target assembly
Assembler
Linker / Loader
7. What is the pass of compiler? Explain how the single and multi
multi-
pass compilers work? What is the effect of reducing the
number of passes?
One complete scan of a source program is called pass.
Pass include reading an input file and writing to the output file.
In a single pass compiler analysis of source statement is immediately followed by
synthesis of equivalent target statement.
It is difficult to compile the source program into single pass due to:
Forward reference: a forward reference of a program entity is a reference to the entity
which precedes its definition in the program.
This problem can be solved by postponing the generation of target code until more
information concerning the entity becomes available.
It leads to multi pass model of compilation.
In Pass I: Perform analysis
analysis of the source program and note relevant information.
In Pass II: Generate target code using information noted in pass I.
Effect of reducing the number of passes
It is desirable to have a few passes, because it takes time to read and write
intermediate file.
On the other hand if we group several phases into one pass we may be forced to keep
the entire program in the memory. Therefore memory requirement may be large.