Syntax Directed Translation
Download
Report
Transcript Syntax Directed Translation
Syntax Directed Translation
CSE244
Aggelos Kiayias
Computer Science & Engineering Department
The University of Connecticut
371 Fairfield Road, Unit 1155
Storrs, CT 06269-3155
[email protected]
http://www.cse.uconn.edu/~akiayias
Syntax-Directed Definitions, I
CSE244
It is a CFG grammar augmented with:
“Attributes” (assigned to each grammar
symbol).
Semantic Rules (associated to each production
involving the Attributes of the symbols in the
production).
Attributes can be synthesized or inherited.
Semantic Rules for a production A have the
form:
b = f (c1,…,cn)
where
(b is synthesized) b is an attribute of A and
c1…cn are attributes of symbols in .
(b is inherited) b is an attribute of some symbol
in and c1…cn are attributes of symbols in A,
Syntax-Directed Defitions, II
CSE244
Terminals have only synthesized attributes whose
values are provided by the lexical analyzer.
The start non-terminal typically has no inherited
attributes.
[we may allow function calls as semantic-rules
also; these are “Side-effects”…
Annotated Parse-Trees
CSE244
Parse-tree that also shows the values of the
attributes at each node.
Values of Attributes in nodes of annotated parsetree are either,
initialized to constant values or by the lexical
analyzer.
determined by the semantic-rules.
Evaluating Attributes.
If a syntax-directed definition employs only
Synthesized attributes the evaluation of all
attributes can be done in a bottom-up fashion.
Inherited attributes would require more arbitrary
“traversals” of the annotated parse-tree.
A dependency graph suggests possible evaluation
orders for an annotated parse-tree.
CSE244
Example of a Syntax-Directed Definition
Grammar symbols: L, E, T, F, n , + , * ,( , ) , digit
Non-terminals E, T, F have an attribute called val
CSE244 Terminal digit has an attribute called lexval
The value for lexval is provided by the lexical analyzer.
PRODUCTION
L E n
E E1 + T
ET
T T1 * F
TF
F (E)
F digit
SEMANTIC RULE
print(E.val)
E.val = E1.val + T.val
E.val = T.val
T.val = T1.val * F.val
T.val = F.val
F.val = E.val
F.val = digit .lexval
Draw the Tree
CSE244
Example 3*5+4n
Draw the Tree
Example 3*5+4n
L
CSE244
Print(19)
E val=19
T val=15
T val=4
T val=3
F val=3
digit
lexval =3
F val=5
*
digit
lexval =5
F val=4
+
digit
lexval =4
n
Example with Inherited Attributes
CSE244
Even though inherited can be simulated by
synthesized it is more natural to write SyntaxDirected Definitions using inherited.
…below in is an inherited attribute of L
PRODUCTION
DTL
T int
T real
L L1 , id
L id
SEMANTIC RULE
L.in = T.type
T.type = integer
T.type = real
L1.in = L.in
addtype(id.entry, L.in)
addtype(id.entry, L.in)
Draw the Tree
Example real id1, id2 , id3
CSE244
Draw the Tree
Example real id1, id2 , id3
D
addtype(id3,real)
CSE244
L
in=real
addtype(id2,real)
L
in=real
T
type=real
addtype(id1,real)
L
in=real
real
id
entry=id1
,
id
entry=id2
,
id
entry=id3
Dependency Graph
Directed Graph
Shows interdependencies between attributes.
CSE244
Construction:
Put each semantic rule into the form
b=f(c1,…,ck) by introducing dummy
synthesized attribute b for every semantic rule
that consists of a procedure call.
E.g.,
L E n
Becomes:
Etc.
print(E.val)
dummy = print(E.val)
Dependency Graph Construction
CSE244
for each node n in the parse tree do
for each attribute a of the grammar symbol at n do
construct a node in the dependency graph for a
for each node n in the parse tree do
for each semantic rule b = f(c1,…,cn)
associated with the production used at n do
for i= 1 to n do
construct an edge from
the node for ci to the node for b
Example I
L
CSE244
Print(19)
E val=19
T val=15
T val=4
T val=3
F val=3
digit
lexval =3
F val=5
*
digit
lexval =5
F val=4
+
digit
lexval =4
n
Example I
dummy
CSE244
val=19
val=15
val=4
val=3
val=3
val=5
val=4
digit
lexval =3
digit
lexval =5
digit
lexval =4
Example II
D
addtype(id3,real)
CSE244
L
in=real
addtype(id2,real)
L
in=real
T
type=real
addtype(id1,real)
L
in=real
real
id
entry=id1
,
id
entry=id2
,
id
entry=id3
Example II
CSE244
in=real
dummy
in=real
dummy
T
type=real
in=real
dummy
id
entry=id1
id
entry=id2
id
entry=id3
Evaluating Attributes
CSE244
Notion: Directed Acyclic Graph
Dependency graph should be a DAG (why?)
Any topological sort of the directed acyclic graph
can be used as a “guide” for attribute evaluation.
Example I
dummy
CSE244
val=19
val=15
val=4
val=3
val=3
val=5
val=4
digit
lexval =3
digit
lexval =5
digit
lexval =4
Example I as a DAG
A topological sort
6,8,7,5,4,3,11,10,9,2,1
1
CSE244
2
3
9
4
5
7
6
10
8
11
Example II
CSE244
in=real
dummy
in=real
dummy
T
type=real
in=real
dummy
id
entry=id1
id
entry=id2
id
entry=id3
Example II as a DAG
CSE244
A topological sort:
8,10,3,1,2,4,9,5,6,7
1
2
4
5
3
6
7
8
9
10
Syntax Trees
CSE244
Decoupling Translation from Parsing-Trees.
Syntax-Tree: an intermediate representation of the
compiler’s input.
Example Procedures:
mknode, mkleaf (create a labeled node – return pointer)
Employment of the synthesized attribute nptr (type:pointer)
PRODUCTION
E E1 + T
E E1 - T
ET
T (E)
T id
T num
SEMANTIC RULE
E.nptr = mknode(“+”,E1.nptr ,T.nptr)
E.nptr = mknode(“-”,E1.nptr ,T.nptr)
E.nptr = T.nptr
T.nptr = E.nptr
T.nptr = mkleaf(id, id.lexval)
T.nptr = mkleaf(num, num.val)
Draw the Tree
CSE244
a-4+c
Draw The Tree
a-4+c
+
CSE244
-
E nptr
c
a
E nptr
4
T nptr
E nptr
T nptr
id
lexval =a
T nptr
-
num
val =4
+
id
lexval =c
Using DAGs for expressions
CSE244
Consider the expression:
a+a*(b-c)+(b-c)*d
Using DAGs for expressions
Consider the expression:
a+a*(b-c)+(b-c)*d
CSE244
+
+
+
+
*
*
a
a
b
c
*
-
b
*
c
d
a
b
c
d
Question
CSE244
How should we modify mkleaf and mknode so that
they produce the DAG instead?
Question
CSE244
How should we modify mkleaf and mknode so that
they produce the DAG instead?
mknode and mkleaf create new nodes only when
necessary.
First they search whether one node with the same
exact properties (label + pointers to children if any)
has already been defined.
If yes node is reused
If no, new node is created.
DAG representation
input
i := i + 10
:=
DAG form
+
CSE244
i
10
Array Representation:
1
id
pointer to i
2
num
10
3
+
1,2
4
:=
1,3
…
…
Implementing the DAG representation
CSE244
For each node with a signature <op,l,r>
Search whether it exists in the list of nodes and
if yes, return its value number (e.g., row in the
array).
most basic implementation: linear search.
More sophisticated implementations:
arrange nodes in a “array of lists” according to
HASH(op || l || r).
HASH(op || l || r) = bucket the node with
signature <op, l, r> must be placed.
Search time drops to
O(bucket-length_search + hash-computation)
Bottom Up Evaluation of S-Attributed
Definitions
CSE244
A syntax-directed definition is S-Attributed if all
attributes are synthesized.
Dependency graph is a (directed) tree “pointing
upwards”
Can be evaluated in bottom up fashion.
=> Consistent with (bottom-up) Shift/Reduce
parsing.
Possible to do the evaluation of the attributes using
the stack at the time of “reduce” operations!
Using the Stack to compute the attributes
CSE244
Suppose the stack of a general Shift/Reduce parser
is equal to
$...XYZ
Suppose the grammar symbols A, X, Y, Z have the
attributes a,x,y,z respectively.
The augmented stack with the attributes is as
follows:
$...[X,X.x=v1][Y,Y.y=v2][Z, Z.z=v3]
If we reduce by the production AXYZ that has
the semantic action A.a = f(X.x, Y.y, Z.z) we will
modify the stack as follows:
$...[A,A.a=f (v1,v2,v3)]
Recall the S-Attributed Definition
CSE244
PRODUCTION
LEn
E E1 + T
ET
T T1 * F
TF
F (E)
F digit
SEMANTIC RULE
print(E.val)
E.val = E1.val + T.val
E.val = T.val
T.val = T1.val * F.val
T.val = F.val
F.val = E.val
F.val = digit .lexval
Semantic Rules Using the Stack of the S/R
parser
CSE244
PRODUCTION
LEn
E E1 + T
ET
T T1 * F
TF
F (E)
F digit
SEMANTIC RULE
print(val[top])
val[ntop]=val[top-2]+val[top]
val[ntop]=val[top]
val[ntop]=val[top-2]*val[top]
val[ntop]=val[top]
val[ntop]=val[top-1]
val[ntop]=val[top]
top = top of the stack
ntop = top of the stack after popping right hand side of
production.
val[…] = attribute value of stack contents
A trace of a Shift Reduce Parser with
Attribute Evaluation
STACK
CSE244
INPUT ACTION
A trace of a Shift Reduce Parser with
Attribute Evaluation
CSE244
$
$[digit,3]
$[F,3]
$[T,3]
$[T,3][*,.]
$[T,3][*,.][digit,5]
$[T,3][*,.][F,5]
$[T,15]
$[E,15]
$[E,15][+,.]
$[E,15][+,.][digit,4]
$[E,15][+,.][F,4]
$[E,15][+,.][T,4]
$[E,19]
$[E,19][n,.]
$[L,.]
3*5+4n$
*5+4n$
*5+4n$
*5+4n$
5+4n$
+4n$
+4n$
+4n$
+4n$
4n$
n$
n$
n$
n$
$
$
shift
reduce Fdigit
reduce TF
shift
shift
reduce Fdigit
reduce TT*F
reduce ET
shift
shift
reduce Fdigit
reduce TF
reduce EE*T
shift
reduce LEn ; print(19)
ACCEPT