Syntax-Directed Tran
s
lation
Gist:
Semantic actions
are attached to gramatical
rules. Most importantly, these actions make
intermediate code generation and type checking.
Rule:
Semantic Action:
E
i
®
E
j
+
E
k
{
E
i
.
a
:=
E
j
.
a
+
E
k
.
a
}
E
i
®
E
j
*
E
k
{
E
i
.
a
:=
E
j
.
a
*
E
k
.
a
}
E
i
®
(
E
j
)
{
E
i
.
a
:=
E
j
.
a
}
E
i
®
i
{
E
i
.
a
:=
i.v
al
}
+
*
i
i
i
Example:
1
+
2
*
3
Rule:
Action:
E
1
E
1
®
i
E
2
E
2
®
i
E
3
E
3
®
i
E
4
E
4
®
E
2
*
E
3
E
5
E
5
®
E
1
+
E
4
E
1
.
a
:=
i.v
al
E
1
.a = 1
E
2
.
a
:=
i.v
al
E
2
.a = 2
E
3
.
a
:=
i.v
al
E
3
.a = 3
E
4
.
a
:=
E
2
.
a
*
E
3
.
a
E
4
.a = 6
E
5
.
a
:=
E
1
.
a
+
E
4
.
a
E
5
.a = 7
2
/69