Beruflich Dokumente
Kultur Dokumente
Complier
Source code Machine code
errors
Implications
Must recognize legal (and illegal) programs
Must generate correct code
Must manage storage of all variables (and code)
Must agree with OS & linker on format for object code
Sometimes we call the analysis part the FRONT END and the synthesis part
the BACK END of the compiler. They can be written independently.
lexical
analyzer Phases of A Compiler
syntax
analyzer
semantic
analyzer
symbol-table error
manager intermediate handler
code generator
code
optimizer
code
generator
target program
14 Wednesday, December S.R. Dash KIIT University
08, 2021
Phases of A Compiler
Error Handler
scanner
Stream of tokens
parser
Parse/syntax tree
Semantic analyzer
Annotated tree
Intermediate code generator
Intermediate code
Code optimization
Intermediate code
Code generator
Target code
Code optimization
Parser
Syntax Tree
Semantic Analyzer Symbol
Annotated Tree Table
Source code
Optimizer
Intermediate Code Error
Code Generator
Handler
Target Code
Target Code
Optimizer
Target Code
23 Wednesday, December S.R. Dash KIIT University
08, 2021
Scanner
The scanner performs what is called lexical Analysis. it collects sequences of
characters into meaningful units called tokens.
a[index] = 4 + 2
a identifier
[ left bracket
index identifier
] left bracket
= assignment
4 number
+ plus sign
2 number
lexical analyzer
Internal
id1 = id2 + id3 * 60
Representations
syntax analyzer
Each stage of
symbol table = processing
id1 +
1 Position …
id2 * transforms a
2 initial … id3 60 representation of
3 rate …
the source code
4
semantic analyzer program into a new
representation.
=
id1 +
id2 *
id3 inttoreal
60
26 Wednesday, December S.R. Dash KIIT University
08, 2021
The Structure of a Compiler
Source
Program
Tokens Syntactic Semantic
Scanner Parser
Structure Routines
Intermediate
Representation
Code
Generator
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Scanner Representation
The scanner begins the analysis of the source program by
reading the input, character by character, and grouping
characters into individualSymbol and
words and symbols (tokens) Optimizer
Attribute
RE ( Regular expression ) Tables
NFA ( Non-deterministic Finite Automata )
DFA ( Deterministic Finite Automata )
(Used by all
LEX
Phases of
The Compiler) Code
Generator
28 Wednesday, December
S.R. Dash KIIT University Target machine code
08, 2021
The Structure of a Compiler
Source
Program Tokens Syntactic Semantic
Scanner Parser Structure
(Character Stream) Routines
Intermediate
Parser Representation
Intermediate
(Character Stream)
Representation
Semantic Routines
Perform two functionsSymbol and Optimizer
Check the static semantics of each construct
Attribute
Do the actual translation Tables
The heart of a compiler
(Used by all
Syntax Directed Translation
Phases of
Semantic Processing Techniques
The Compiler)
IR (Intermediate Representation)
Code
Generator
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Optimizer Representation
The IR code generated by the semantic routines is analyzed and
transformed into functionally equivalent but improved IR code
This phase can be verySymbol
complex and
and slow Optimizer
Peephole optimization Attribute
Tables
loop optimization, register allocation, code scheduling
Register and Temporary Management
Peephole Optimization
(Used by all
Phases of
The Compiler) Code
Generator
31 Wednesday, December
S.R. Dash KIIT University Target machine code
08, 2021
The Structure of a Compiler
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Code Generator Representation
Interpretive Code Generation
Generating Code from Tree/Dag
Grammar-Based Code Generator
Optimizer
Code
Generator
32 Wednesday, December Target machine code
S.R. Dash KIIT University
08, 2021
The Structure of a Compiler
Code Generator
[Intermediate Code Generator]
Tokens
Code Optimizer
Parser
[Syntax Analyzer]
Optimized Intermediate Code
Parse tree
Code Generator
Semantic Process
[Semantic analyzer] Target machine code
identifier := expression
• In a parse tree, all terminals are at leaves.
Old value
12
44 Wednesday, December S.R. Dash KIIT University
08, 2021
Syntax Analyzer (CFG)
The syntax of a language is specified by a context free grammar
(CFG).
The rules in a CFG are mostly recursive.
A syntax analyzer checks whether a given program satisfies the
rules implied by a CFG or not.
– If it satisfies, the syntax analyzer creates a parse tree for the given program.
Ex:
assgstmt -> identifier := expression
expression -> identifier
expression -> number
expression -> expression + expression
The type of the identifier newval must match with type of the expression (oldval+12)
Temp1= inttoreal(60)
Temp2 = id3 * temp1
Temp3= id2 + temp2
Id1= temp3
Ex:
Temp1= id3* 60.0
Id1=id2*temp1
MOVE id3,R2
MULF #60.0, R2
MOVF ID2,R2
ADDF R2,R1
MOVE R1,id1