UNIT2-The Phases of a Compiler
-
Upload
anh-trai-em-oc -
Category
Documents
-
view
236 -
download
1
Transcript of UNIT2-The Phases of a Compiler
-
8/13/2019 UNIT2-The Phases of a Compiler
1/23
1
Unit 2.
The phases of a Compiler
-
8/13/2019 UNIT2-The Phases of a Compiler
2/23
2
Main phases of a compiler
-
8/13/2019 UNIT2-The Phases of a Compiler
3/23
3
Phases of a compiler
Scanner (Lexical Analyser)Stream of characters making up the source program
is read from left to right and grouped into tokens
(sequences of characters having a collective
meaning)
Parser (Syntax Analyser)
Group the tokens of the source program into
grammatical phrases that are used by the compiler to
synthesize output
-
8/13/2019 UNIT2-The Phases of a Compiler
4/23
4
Phases of a compiler
Semantic Analysis:Check the sourceprogram for semantic errors and gather
type information for the subsequent code
generation phase.
Intermediate Code Generation: Generate
an intermediate representation as aprogram for an abstract machine.
-
8/13/2019 UNIT2-The Phases of a Compiler
5/23
5
Phases of a compiler
Code optimization : Improve theintermediate code so that faster running
code will result
Code generation: Generation of target
code, consisting normally of relocatable
machine code or assembly code
-
8/13/2019 UNIT2-The Phases of a Compiler
6/23
6
Translation
of astatement
-
8/13/2019 UNIT2-The Phases of a Compiler
7/23
7
Phase 1:Lexical Analysis
Scanner: Converts the stream of inputcharacters into a stream of tokens that becomes
the input to the following phase (parsing)
Tasks of a scannerGroup characters into tokens
Token: the syntax unit
Categorization of tokens.
-
8/13/2019 UNIT2-The Phases of a Compiler
8/23
8
Types of tokens
-
8/13/2019 UNIT2-The Phases of a Compiler
9/23
9
Phase 2: Parsing
The process of determining if a string oftoken can be generate by a grammar
Is executed by a parser
-
8/13/2019 UNIT2-The Phases of a Compiler
10/23
10
Phase 2: Parsing
Output of a parser:Parse tree (if any)
Error Message (otherwise)
If a parse tree is built succesfully, the
program is grammatically correct
-
8/13/2019 UNIT2-The Phases of a Compiler
11/23
11
Example: statement a = b + c
-
8/13/2019 UNIT2-The Phases of a Compiler
12/23
12
Grammars,languages, BNF,syntax diagrams
B phn tch c php cna ra phn tch chomi cu ca ngn ng (chng trnh)
BNF (Backus-Naur Form) is is a meta languageused to express grammars of programminglanguages
Syntax Diagrams : A pictorial diagram showingthe rules for forming an instruction in a
programming language, and how thecomponents of the statement are related. Syntaxdiagrams are like directed graphs.
-
8/13/2019 UNIT2-The Phases of a Compiler
13/23
13
Grammars,languages, BNF,syntax diagrams
BNF and formal grammars use 2 types of
symbol
Terminals :
Tokens of the language
Never appear in the left side of any production
Nonterminals
Intermediate symbol to express structures of alanguage
Must be in a left side of at lease one production
Enclose in
-
8/13/2019 UNIT2-The Phases of a Compiler
14/23
14
Grammars,languages, BNF,syntax diagrams
Start symbol :Nonterminal of the first level
Appear at the root of parse tree
-
8/13/2019 UNIT2-The Phases of a Compiler
15/23
15
Parsing: Concept and techniques
Continuously apply grammatical rules untila string of terminal is generated.
If the parser convert first symbol into the
input string, it is syntactically correct
Otherwise, string is not syntactically
correct
-
8/13/2019 UNIT2-The Phases of a Compiler
16/23
16
Parsing: Concept and techniques
The most important thing of acompiler: grammar
Grammar includes all structures of aprogram
Not includes any other rule
-
8/13/2019 UNIT2-The Phases of a Compiler
17/23
17
Parsing: Concept and techniques
Grammar must be unambiguous
If grammar is ambiguous, more thanone parse tree can be created
-
8/13/2019 UNIT2-The Phases of a Compiler
18/23
18
Phase 3: Semantic Analysis
Certain check are performed toensure that the components of a
program fit together meaningfullyTo generate code, source
program must be syntacticallyand semantically correct
-
8/13/2019 UNIT2-The Phases of a Compiler
19/23
19
Phase 4: Intermediate code generation
Source program is transferred to anequivalent program in intermediate code
by intermediate code generator
Intermediate code is machine independent
-
8/13/2019 UNIT2-The Phases of a Compiler
20/23
20
Advantages of Intermediate Code
1.Easy to translate into object code.2.Code optimizer can be applied before
code generation
3.Decrease time cost
-
8/13/2019 UNIT2-The Phases of a Compiler
21/23
21
Intermediate Code
Alternatives
Parse TreePostfix NotationThree Address Code
-
8/13/2019 UNIT2-The Phases of a Compiler
22/23
22
Phase 5: Code Generator
Input: Intermediate code of sourceprogram
Output: Object programAssembly code
Virtual machine code
-
8/13/2019 UNIT2-The Phases of a Compiler
23/23
23
Problems
Input Output
Set of instruction Register allocation
Object machine