The Model of Compilation - PowerPoint PPT Presentation

About This Presentation
Title:

The Model of Compilation

Description:

The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 20
Provided by: NNP50
Category:

less

Transcript and Presenter's Notes

Title: The Model of Compilation


1
The Model of Compilation
  • Natawut Nupairoj, Ph.D.
  • Department of Computer Engineering
  • Chulalongkorn University

2
Outline
  • Overview.
  • Front-End
  • Lexical Analysis.
  • Syntactic Analysis.
  • Semantic Analysis.
  • Back-End
  • Code Generation.
  • Code Optimization.

3
Overview
  • Translate a source program (in language S) into
    an equivalent object program (in language O).

4
The Model of Compilation
Analysis (Front-End)
Synthesis (Back-End)
IntermediateRepresentation
source
object
  • Reduce Complexity
  • Source/Target Independent
  • Plug-able Compiler
  • IR contain sufficient information
  • tree-like structure the syntax tree or
  • Assembly-like format three-address code.

5
Front-End
  • Lexical Analysis
  • group the input stream into tokens
  • Syntactic Analysis
  • see if the source is valid or correct
  • Contextual/Semantic Analysis
  • make sure the program is meaningful or
    semantically correct.

6
Front-End Components
Group token.
Scanner
Source program (text stream)
identifier main
symbol (
m
a
i
n
(
)

token
Construct parse tree.
next-token
Symbol Table
Parser
parse-tree
Check semantic/contextual.
Intermediate Representation (file or in memory)
Semantic Analyzer
7
Lexical Analysis
  • Scanner.
  • Group the input stream into tokens
  • identifiers.
  • numbers.
  • keywords.
  • symbols signs.
  • Lexeme Character sequence forming a token.
  • Eliminate all blanks and comments.

8
Example Tokens
position initial rate 60
1. identifier position 2. assignment symbol 3.
identifier initial 4. plus symbol
5. identifier rate 6. muliplication symbol 7.
integer-literal 60
9
Syntax Analysis
  • Parser.
  • Check if the source is grammatically correct.
  • Construct a parse tree.

10
Mini-Triangle Syntax
  • single-Command V-name Expression
  • Identifier ( Expression )
  • if Expression then single-Command
  • else single-Command
  • while Expression do single-Command
  • let Declaration in single-Command
  • begin Command end

11
Mini-Triangle Syntax
  • Expression primary-Expression
  • Expression Operator primary-Expression
  • primary-Expression Integer-Literal
  • V-name
  • Operator primary-Expression
  • ( Expression )
  • V-name Identifier
  • ...
  • Operator - / lt gt \

12
Example Parse Tree
13
Semantic Analysis
  • Make sure that the program is meaningful.
  • Walk the parse tree to check
  • Type checking.
  • Type conversion.
  • Example
  • rate 60
  • rate is a real variable
  • rate inttoreal(60)
  • Generate IR (can also done by parser).

14
Example of IRAbstract Syntax Tree (AST)
  • position initial rate 60

position initial rate 60
interior node operation children
arguments leaves identifiers or constants
15
Example of IRThree-Address Code
position initial rate 60
  • tmp rate 60
  • tmp initial tmp
  • position tmp

16
Back-End
  • Code Optimization
  • improve IR machine-independent.
  • improve object code machine-depedent.
  • optimizing compiler.
  • widely-used.
  • Code Generation
  • generate object code.
  • assign memory/register locations.
  • instruction selection.

17
Front-End Components
Machine-independent optimization.
IR Optimizer
Intermediate Representation (file or in memory)
IR
Generate object code.
Symbol Table
Code Generator
Machine-dependent optimization
Object code
Peephole Optimizer
Object code (assembly or binary)
18
Other Phases
  • Symbol-Table Management
  • information about identifier being-used.
  • name
  • type
  • scope
  • Scanner creates an entry into the table.
  • Error Handler
  • what to do when found errors in the source.

19
Compiler-Construction Tools
  • Parser generators.
  • Generate a parser from a CFG.
  • Yacc, Bison.
  • Scanner generators.
  • Generate a scanner from regular expressions.
  • Lex, Flex.
Write a Comment
User Comments (0)
About PowerShow.com