Professional Documents
Culture Documents
7. Compilers
2/17/2018
John Roberts
2
Overview
• Compilation Process
3
Compilation Process
Source
Program Parsing (Grammars)
Lexical Analysis recursive descent
(stream of Tokens processing
characters) (stream of lexical units)
Abstract Syntax Tree (AST)
Constraining
Code Generation Interpreter
type check, decorate AST,
bytecodes, runtime stack (Virtual Machine)
symbol table Bytecode Program
Decorated AST
4
Extended Example
5
Source Program
Source
Program
Lexical Analysis
(stream of
characters)
6
Lexical Analysis
Lexical Analysis
Tokens
(stream of lexical units)
• Removes whitespace
• Removes comments
7
Tokens
Parsing (Grammars)
recursive descent
Tokens processing
(stream of lexical units)
rightBrace
{ left: 8, right: 8
READLINE i=i+j+7
i left: 3, right: 3
= left: 5, right: 5
i left: 7, right: 7
+ left: 9, right: 9
READLINE j = write( i )
j left: 3, right: 3
= left: 5, right: 5
READLINE }
} left: 0, right: 0
12
Parsing (Syntax Analysis)
Parsing (Grammars)
recursive descent
processing
Abstract Syntax Tree (AST)
Constraining
type check, decorate AST,
symbol table
Abstract Syntax Tree (AST)
block
+ 7 write i
i j
1: Program
2: Block
3: IntType 4: Id: i 6: IntType 7: Id: j 9: Id: i 14: AddOp: + 16: Id: j 19: Call
Constraining
type check, decorate AST,
symbol table
Decorated AST
17 More on symbol tables and intrinsic trees later (write tree not shown)
Decorated AST
program
Symbol Table
Intrinsic Tree
Entry
block
Put 1 Put 2
decl decl assign assign
+ 7 write i
Int Int Int Write Int
Tree Tree Tree Tree Get 1
Tree
i j
decl
Get 1 Get 2
int <<int>>
2: Block
3: IntType 4: Id: i 6: IntType 7: Id: j 9: Id: i 14: AddOp: + 16: Id: j 19: Call
0 28 1 28 5 28 8 28
28 28 35 5
28: Decl
28
37: Decl
39: Id:
38: IntType
dummyFormal
28
19
Code Generation
Code Generation
bytecodes, runtime stack
Bytecode Program
• Generates bytecodes
block
addr = 0 addr = 1
+ 7 write i
Int
Tree
i j
decl
int <<int>>
Write
Tree
label = write
functionDecl
decl
int dummyFormal
addr = 0
i is first, j is second
000 load i
0000 load j
0007 load 7
007 add
70 store into i
1 program { int i int j
2 i = i + j + 7
22 Do we need to refresh on stack frames? Covered in 415…
Code Generation New stack frame on fn invocation, arg copies pushed to stack (by value!
3 j = write(i)
4 }
Call write
77 store into j
GOTO start<<1>>
LABEL read
RETURN
LABEL write
WRITE
RETURN
1 program { int i int j 25
2 i = i + j + 7
Byte Codes 3
4 }
j = write(i)
LOAD 1 j
BOP +
LOAD 0 i j = write( i )
ARGS 1
CALL write
STORE 1 j
HALT