Tải bản đầy đủ (.pdf) (60 trang)

slike bài giảng môn chương trình dịch chương 5 syntax-directed translation

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (329.82 KB, 60 trang )

Syntax-Directed Translation
Dr. Nguyen Hua Phung
Falcuty of CSE – HCMUT
2007
Outline
• Review
• Syntax-directed definition
• Attributes
• Top-down translation
• Bottom-up translation
CSE - HCMUT Syntax-directed Translation 2
Compilation Phases
CSE - HCMUT Syntax-directed Translation 3
Source code
Scanner
Parser
Semantic analyzer
Intermediate code
generation
Code optimization
Code generation
Sequence of tokens
AST or Parse tree
front end
Symbol
table
AST tree
Intermediate code
Optimized code
back end
Machine code


Pass
• A pass consists of reading an input file
and writing an output file.
• A pass may include many phases
• One-pass or multi-pass compilation
– internal form
– information dependence
CSE - HCMUT Syntax-directed Translation 4
One-pass Compiler
CSE - HCMUT Syntax-directed Translation 5
BKIT code
Scanner √
Parser
Semantic analyzer
Code generation
Sequence of tokens
Parse tree
Symbol
table
Parse tree
Jasmin code
Multi-pass Compiler
BKIT code
Scanner √
CSE - HCMUT Syntax-directed Translation 6
Parser
Semantic analyzer
Code generation
Sequence of tokens
AST

Symbol
table
AST
Jasmin code
Semantic Analyzer
• enforce semantic requirements:
– is an identifier a scalar, array, struct or a method ?
– is an identifier declared before used?
– is an expression type-consistent?
– is a method called with right number and type of args?
– which declaration of a reference is?
– does the dimension of an array match with the
declaration?
–etc.
These requirement does not enforced by a CFG
CSE - HCMUT Syntax-directed Translation 7
Outline
• Review √
• Syntax-directed definition
• Attributes
• Top-down translation
• Bottom-up translation
CSE - HCMUT Syntax-directed Translation 8
Syntax-directed Definition
• Each grammar symbol associates with it a
set of attributes
• Each grammar production associates with
it a set of semantics rules which evaluate
and refer only to attributes associated with
the symbols in the production.

CSE - HCMUT Syntax-directed Translation 9
Example 1
Production Semantic Rules
L → E $
print(E.val)
E → E
1
+ T
E.val = E
1
.val add T.val
E → T
E.val = T.val
T → T
1
* F
T.val = T
1
.val mul F.val
T → F
T.val = F.val
F → ( E )
F.val = E.val
F → digit
F.val = IntVal(digit.lexeme)
CSE - HCMUT Syntax-directed Translation 10
Example 1 (cont’d)
8 + 5 * 4 $
L→ E $ {print(E.val)}
L

28
E→ E + T {E.val = E
1
.val + T.val}
E
$
E T
+
T F
*
CSE - HCMUT Syntax-directed Translation 11
F
T
F
8 5
4
F.val = 8
T.val = 8
E.val = 8
F.val = 5
F.val = 4
E.val = 28
T.val = 20
E
E T
+
T F
*
F
T

F
8 5
4
F.val = 5
E
E T
+
T F
*
F
T
F
8 5
4
F.val = 5
T→ F {T.val = F.val}
E→ T {E.val = T.val}
F→digit {F.val=IntVal(digit.lexeme)}
T→T + F {T.val = T
1
.val * F.val}
Example 2
• 8 + 5 * 4
E
E T
+
CSE - HCMUT Syntax-directed Translation 12
T F
*
T

F F
8 5
4
Parse Tree
Example 2
8 + 5 * 4
BinExp
LitExp(8)
+
BinExp
LitExp(5)
LitExp(4)
*
Abstract Syntax Tree (AST)
CSE - HCMUT Syntax-directed Translation 13
Example 2
Production Semantic Rules
E → E
1
+ T
E.node = new BinExp(E
1
.node,+, T.node)
E → T
E.node = T.node
T → T
1
* F
T.node = new BinExp(T
1

.node,*, F.node)
T → F
T.node = F.node
F → ( E )
F.node = E.node
F → digit
F.node = new LitExp(digit.lexeme)
F → id
F.node = new VarExp(id.lexeme)
CSE - HCMUT Syntax-directed Translation 14
Example 2
8 + 5 * 4
E.node
T.nodeE.node
+
T.node F.node
*
F.node 4
5
T.node
F.node
8
BinExp
BinExpLitExp(8)
LitExp(4)LitExp(5)
*
+
parser tree
AST
F.node=new LitExp(num)

T.node=F.node
E.node=T.node
F.node=new LitExp(num)
T.node=new BinExp(T1.node,*,F.node)E.node=new BinExp(E1.node,+,T.node)
CSE - HCMUT Syntax-directed Translation 15
Outline
• Review √
• Syntax-directed definition √
• Attributes
• Top-down translation
• Bottom-up translation
CSE - HCMUT Syntax-directed Translation 16
Attribute
• An attribute can represent
– a string,
– a number,
– a type,
– a memory location,
– a code fragment,
–etc.
• An attribute has its name and value
CSE - HCMUT Syntax-directed Translation 17
Synthesized Attribute
• An attribute of a grammar symbol in LHS is synthesized
if its value is computed from the values of the attributes
of the grammar symbols in RHS
• A syntax-directed definition that consists of only
synthesized attributes is called a S-attributed definition
8 + 5 * 4 $
L

E
$
E T
+
T F
*
F
T
F
8 5
4
F.val = 8
T.val = 8
E.val = 8
F.val = 5
F.val = 4
E.val = 28
T.val = 20
28
CSE - HCMUT Syntax-directed Translation 18
Inherited Attribute
CSE - HCMUT Syntax-directed Translation 19
• An attribute of a grammar symbol in RHS is inherited if its
value is computed from the values of the attributes of the
grammar symbols in LHS and/or RHS
E → E + T | T
T → T * F | F
F → digit | (E)

E → T E’

E’ → + T E’ | ∈
T → F T’
T’ → * F T’ | ∈
F → digit | (E)
E
T
E’
T
+
T’
F
*
F
T’
F
8
5
4
E’
T’
8 + 5 * 4
Example
Production Semantic Rules
L → E $
print(E.val)
E → T E’
E’.in = T.val; E.val = E’.val
E’ → + T E’
1
E’

1
.in = E’.in add T.val; E.val = E’
1
.val
E’ →∈
E’.val = E’.in
T → F T’
T’.in = F.val; T.val = T’.val
T’ → * F T’
1
T’
1
.in = T’.in mul F.val; T’.val = T’
1
.val
T’ →∈
T’.val = T’.in
F → ( E )
F.val = E.val
F → digit
F.val = IntVal(digit.lexeme)
CSE - HCMUT Syntax-directed Translation 20
Example
E.val = E’.val = 17
CSE - HCMUT Syntax-directed Translation 21
E
T
E’
T
+

E’
T
F
T’
F
8
5
E’
T’
8 + 5 + 4
+
F
T’
4
T’.in = 5
T.val = 5
T’.val =T’.in
E’.in =T.val = 8
E
1
’.in =E’.in add T.val = 13
T.val = 4
T’.in = 4
E
1
’.in =E’.in add T.val = 17
T’.in = F.val = 8
T.val =T’.val
T’.val =T’.in
T’.val = 4

E’.val = E
1
’.val = 17
E’.val = E
1
’.val=17
E’.val =E’.in
L-attributed Definitions
• A syntax-directed definition is L-attributed
if each inherited attribute of X
j
, 1≤j≤n, on
RHS of A → X
1
X
2
…X
n
, depends only on
– the attributes of X
1
,X
2
,…X
j-1
– the inherited attributes of A
CSE - HCMUT Syntax-directed Translation 22
Example
Production Semantic Rules
L → E $

print(E.val)
E → T E’
E’.in = T.val; E.val = E’.val
E’ → + T E’
1
E’
1
.in = E’.in add T.val; E.val = E’
1
.val
E’ →∈
E’.val = E’.in
T → F T’
T’.in = F.val; T.val = T’.val
T’ → * F T’
1
T’
1
.in = T’.in mul F.val; T’.val = T’
1
.val
T’ →∈
T’.val = T’.in
F → ( E )
F.val = E.val
F → digit
F.val = IntVal(digit.lexeme)
CSE - HCMUT Syntax-directed Translation 23
Outline
• Review √

• Syntax-directed definition √
• Attributes √
• Top-down translation
• Bottom-up translation
CSE - HCMUT Syntax-directed Translation 24
Implementation
• Graph-based methods:
– create a dependence graph
– topological sort
– calculate all attibutes
– many passes
• Rule-based methods:
– the order in which the attributes are evaluated
is predetermined
– one-pass evaluation
CSE - HCMUT Syntax-directed Translation 25

×