Unit 1, 4
Unit 1, 4
Compiler Construction
CSE
By Himanshu Swarnkar
Engineering College Banswara
Source Program
Lexical Analyzer
Stream of token
Syntax Analyzer
Parse Tree
Semantic Analyzer
Symbol Table Parse tree
semantically verified Error Handler
Manager
Intermediate
Code Generator
Three address Code
Code
Optimization
Code Generator
Target Program
1 Lexical Analysis
The first phase of compiler is also known as Scanner. The scanner works as a text scanner.
This phase scans the source code as a stream of characters and converts it into meaningful lexemes.
Lexical analyzer represents these lexemes in the form of tokens as: <Token-name, attribute-value>
Example: Position := initial + rate*60 Lexical Analyzer
id1= id2+id3*60
2 Syntax Analysis
The next phase is called the Syntax Analysis or Parser. It takes the token produced by lexical analysis, as input and generates a
parse tree (or syntax tree).
In this phase, token arrangements are checked against the source code grammar, i.e., the parser checks if the expression made
by the tokens is syntactically correct or not.
Syntax Analyzer CFG(A →α )
S→id:=E
E→ E+T/T
:=
T→T*F/F
+ F →60
id 1
id 2 *
id 3 60
Semantic Analyzer
3 Semantic Analysis
✔ Semantic analysis checks whether the parse tree constructed thus Semantic Analyzer
follows the rules of language.
✔ For example, it checks type casting, type conversions issues and so :=
on. Also, the semantic analyzer keeps track of identifiers, their types
and expressions; whether identifiers are declared before use or not, id 1 +
etc.
id 2 *
✔ The semantic analyzer produces an annotated syntax tree as an
output.
id 3 Int to Real
Semantic Analyzer 60
temp1 := id3*60.0
id1:= id2*temp1
6 Code Generation In
✔ this phase, the code generator takes the optimized representation of the intermediate code and maps it to the target machine
language.
✔ The code generator translates the intermediate code into a sequence of re-locatable machine code.
✔ Sequence of instructions of machine code performs the task as the intermediate code would do.
Code Optimization
MOVF id3,R2
MULF #60.0,R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
7 Symbol Table Symbol
✔ Table is also known as Book Keeping. It is a data-structure
maintained throughout all the phases of a compiler.
✔ All the identifiers‟ names along with their information like type, size,
etc., are stored here.
✔ The symbol table makes it easier for the compiler to quickly search
and retrieve the identifier's record.
✔ The symbol table is also used for scope management.
8 Error Hander
✔ A parser should be able to detect and report any error in the program.
✔ It is expected that when an error is encountered, the parser should be
able to handle it and do parsing with the rest of the inputs.
✔ Mostly it is expected from the parser to check for errors.
✔ But errors may be encountered at various stages of the compilation
process.
Bootstrapping
The process by which simple language is use to translate more complicated program, which turn many handle on even more
complicated program and so on, is known as Bootstrapping.
or
Bootstrapping is a process in which simple language is used to translate more complicated program which in turn may handle
for more complicated program.
This complicated program can further handle even more complicated program and so on.
To design a compiler bootstrapping process is use. (Any compiler is represented using tree language, S.L., T.L., I.L.)
SL TL
PL PL ML
C++ C++ M
CL CL ML
AL AL M
M
Cross Compiler: A compiler which runs on one machine but produce object code (Generally Machine code) for another
machine. Py C++ Py C++
C C M M
M
Any Query ?
Thanks