Download Syntax-Directed Translation: SDD and AST - Prof. Zijiang Yang and more Study notes Computer Science in PDF only on Docsity!
Syntax
‐Directed
Translation
Syntax
‐Directed
Translation Chapter^5
Chapter^ 5:^ Syntax‐directed Translation^
CS5810^ Spring^2009
Bad TranslationBad^ Translation
•^ CS581 is a very boring classCS
is^ a^ very
boring^
class
–^ Use^ babelfish translatorEnglish to Chinese then Chinese t o English–^ English
to^ Chinese
,^ then^ Chinese
t^ o^ English
•^ CS
is^ the^ extremely
tasteless
kind
Chapter^ 5:^ Syntax‐directed Translation^
CS5810^ Spring^2009
Syntax
‐Directed Definitions
Syntax
Directed
Definitions
•^ A^ syntax
‐directed
definition
(SDD)^ is
a^ context
sy ta^ d ected de
t o^ (S
)^ s a co te t
free^ grammar
together
with^ attributes
and^ rules
-^ Attributes
are^ associated
with^ grammar
symbols
-^ Rules^ are
associated
with^ productions
•^ A^ synthesized
attribute
for^ A^ at
a^ parse
tree^ node
N i^ d fi
d^ l^
i^
f^ ib^
h
N^ is^ defined
only^ in^ terms
of^ attributes
at^ the
children
of^ N^ and
at^ N^ itself
•^ An inherited attribute for B at a parse tree node•^ An^ inherited
attribute
for^ B^ at
a^ parse
tree^ node
N^ is^ defined
only^ in^ terms
of^ attributes
at^ the^ N’s
parent, N itself, and N
’s siblings
parent,^
N^ itself,^
and^ N s^
siblings
Chapter^ 5:^ Syntax‐directed Translation^
CS5810^ Spring^2009
SDD Example 1SDD^ Example
Production
Semantic Rules Production
Semantic
Rules E^ Æ^ E + T^
E.val = E^12
.val + T.val
E^ Æ^ T^
E.val = T.val T^ Æ^ T * F^
T.val = T^ .val * F.val^12 T^ Æ^ F^
T^ l^ F^
l
T^ Æ^ F^
T.val = F.val F^ Æ^ int^
F.val = int.lexval F^ Æ^ ( E )^
F val = E val F^ Æ^ ( E )^
F.val^ E.val
-^ An SDD that involves only synthesized attributes is called S-attributed
CS5810^ Spring^2009 Chapter^ 5:^ Syntax‐directed Translation
TEST YOURSELF #1TEST^ YOURSELF
•^ A^ CFG
for^ the
language
of^ binary
numbers:
g^ g^
y
B^ Æ^0 Æ^1 Æ^ B 0^ Æ^ B 1 D fi^
t^ di^
t d t^
l ti^
th t
•^ Define
a^ syntax
‐directed
translation
so^ that
the^ translation
of^ a^ binary
number
is^ its^ base
10 value. 10 value. • Draw^ the
parse^ tree
for^1001
and^ annotate
each^ nonterminal with
its^ translation. CS5810 Spring^2009
Chapter^ 5:^ Syntax‐directed Translation
Evaluation Orders for SDDsEvaluation
Orders
for^ SDDs
•^ S‐Attributed DefinitionsS^ Attributed
Definitions
postorder(N) {postorder(N)
{ for (each child C of N, from the left)postorder (C);evaluate the attributes associated with N;evaluate the attributes associated with N;
} Chapter 5: Syntax‐directed Translation
CS5810^ Spring^2009
AST vs Parse TreeAST^ vs
Parse
Tree
•^ AST^ is
a^ better
structure
for^ later
compiler
stages
S^ s a bette st uctu e o
ate co
p e stages
-^ omits^ details
having^ to
do^ with^ the
source^ language,
-^ only^ contains
information
about^ the
essential
structure
of^ the^ program. • e.g., parentheses,^
commas,^ semi
‐colons
•^ AST is condensed form of a parse tree•^ AST^ is
condensed
form^ of
a^ parse
tree
-^ operators
appear^ at
internal^ nodes,
not^ at^ leaves.
-^ "Chains" of single productions are collapsedChains
of^ single
productions
are^ collapsed
-^ Lists^ are
"flattened".
-^ Syntactic
details^ are
ommitted
Chapter^ 5:^ Syntax‐directed Translation^
CS5810^ Spring^2009
Example: 2
* (4 + 5): parse tree
vs^ AST
Example:
+^ 5):^ parse
tree^ vs
AST
E TT
F T^ *
F^
E^ )(
2 4 T E^ + int (2)^
(^54)
F T F^
int (5) Chapter^ 5:^ Syntax‐directed Translation^
CS5810^ Spring^2009 F^
int^ (5) int (4)
AST ConstructionAST^ Construction
•^ We want to explicitly construct the AST duringWe^ want
to^ explicitly
construct
the^ AST
during
the^ parsing
phase
Chapter^ 5:^ Syntax‐directed Translation^
CS5810^ Spring^2009
Constructing Syntax TreeConstructing
Syntax
Tree
Production
Semantic Rules Production
Semantic
Rules E^ Æ^ E + T^
E.node = new Node(‘+’,E^1
.node,T.node) 2
E^ Æ^ T^
E.node = T.node T^ Æ^ T * F^
T.node= new Node(‘*’, T^1
.node, F.node) 2
T^ Æ^ F^
T^ d^ F
d
T^ Æ^ F^
T.node = F.node F^ Æ^ int^
F.node = new leaf(int, int.val) F^ Æ^ ( E )^
F node = E node F^ Æ^ ( E )^
F.node^ E.node^ CS5810^ Spring
2009
Chapter^ 5:^ Syntax‐directed Translation
Constructing Syntax TreeConstructing
Syntax
Tree
Production
Semantic Rules Production
Semantic
Rules T^ Æ^ F T’^
T.node = T’.synT’.inh = F.node T’^ Æ^ *FT’^1
T’^ .inh = new node(‘*’, T’.inh, F.node)^1 T’.syn = T’^1
.syn
T’^ Æ^ Є^
T’.syn = T’.inh F^ Æ^ i t^
F^ d^
l^ f(i t i t^
l)
F^ Æ^ int^
F.node = new leaf(int, int.val)
-^ Annotated parse tree for 3*
CS5810^ Spring^2009 Chapter^ 5:^ Syntax‐directed Translation
Example
2:^ Compute
the^ type
of^ an
expression
E -> E + E^
if ((E.trans == INT) and (E^2
.trans == INT) 3 h^ E^
INT
then E.trans = INT^1 else E^ .trans = ERROR^1 E -> E and E
if ((E^ .trans == BOOL) and (E^2
.trans == BOOL) 3 th^ E^ t^
BOOL
then E.trans = BOOL^1 else E^ .trans = ERROR^1 E -> E == E
if ((E.trans == E^2
.trans) and (E 3
.trans != ERROR)) 2 th^ E^ t^
BOOL
then E.trans = BOOL^1 else E^ .trans = ERROR^1 E -> true^
E.trans = BOOL E -> false^
E.trans = BOOL E -> int^
E.trans = INT E -> ( E )^
E.trans = E^1
.trans 2 CS5810^ Spring^2009 Chapter^ 5:^ Syntax‐directed Translation
AST Construction
‐^ LL
AST^ Construction
LL
LL^ parsing:^
extend^ procedures
f^ t^
i^ l
S^ Æ^ ES’S’^ Æ^ ε^ | +S
void^ parse_S()
{ h ( k ) {
Expr parse_S()
switch (token) {
for^ non‐terminals
S^ Æ^ ε^ |^ +S E^ Æ^ num^ |
(S)
switch^ (token)
case^ num:^
case^ ‘(‘:parse_E();
switch^ (token)
case^ num:^
case^ ‘(‘:Expr left =^ parse_E();Expr right = parse S’();
parse_S’();return;default:
Expr right^ =
parse_S ();
if^ (right^ ==^
NULL)^ return
left
else^ return
new^ Node(left,right);
default:
ParseError();} }
default:ParseError();} }
Chapter^ 5:^ Syntax‐directed Translation^
CS5810^ Spring^2009
AST Construction
‐^ LR
AST^ Construction
LR
•^ We again need to add code for explicit ASTWe^ again
need^ to
add^ code
for^ explicit
AST
construction • AST construction mechanism• AST^ construction
mechanism
–^ Store^ parts
of^ the^ tree
on^ the^ stack
F^ h
i^ l^ b l X
k
l
–^ For^ each
nonterminal symbol
X^ on^ stack,
also
store^ the
sub‐tree
rooted^ at
X^ on^ stack
Wh^
th^
f^
d
–^ Whenever
the^ parser
performs
a^ reduce
operation
for^ a^ production
X^ Æ^ γ,^ create
an^ AST
node for X Chapter 5: Syntax‐directed Translation
CS5810^ Spring^2009
node^ for
X