Abstract Syntax Tree (AST). The parse tree contains too much detail e.g. unnecessary terminals such as parentheses depends heavily on the structure of the grammar e.g. intermediate non-terminals Idea: strip the unnecessary parts of the tree, simplify it.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
if-statement
if-statement
can become
IF
cond
THEN statement
cond
statement
E
add_expr
can become
E
+
E
mul_expr
id
id
E
*
E
id
num
id
num
int func (int x, int y);
int main () {
int list[5], i, j;
char *str;
j = 10 + 'b';
str = 8;
m = func("aa", j, list[12]);
return 0;
}
Example 1: Grammar of declarations
ProductionSemantic rule
D T LL.in = T.type
T int T.type = integer
T charT.type = character
L L1, idL1.in = L.in
addtype (id.index, L.in)
L idaddtype (id.index, L.in)
Example 2: Grammar of signed binary numbers
ProductionSemantic rule
N S Lif (S.neg) print('-'); else print('+');
print(L.val);
S +S.neg = 0
S –S.neg = 1
L L1, BL.val = 2*L1.val+B.val
L BL.val = B.val
B 0B.val = 0*20
B 1B.val = 1*20
ProductionSemantic rule
E E1+E2 E.val = E1.val+E2.val
E numE.val = num.yylval
E (E1)E.val = E1.val
attribute-dependence graph
12
E
E
E
+
E
num
2
10
)
(
+
E
E
num
7
num
3
ProductionSemantic rule
E E1+E2 E.node = new PlusNode(E1.node,E2.node)
E numE.node = num.yylval
E (E1)E.node = E1.node
L.in is inherited and evaluated
after parsing T but before L
T.type is synthesized and
evaluated after parsing int
D T{L.in = T.type}L
T int{T.type = integer}
Tchar{T.type = character}
L {L1.in = L.in}L1, id{L.action = addtype (id.index, L.in)}
L id{L.action = addtype (id.index, L.in)}
ProductionSemantic rule
L E \nprint(E.val)
E E1 + TE.val = E1.val+T.val
E TE.val = T.val
T T1* FT.val = T1.val*F.val
T FT.val = F.val
F (E)F.val = E.val
F digitF.val = yylval
ProductionSemantic rule Stack operation
L E \nprint(E.val)
E E1 + TE.val = E1.val+T.val val[newtop]=val[top-2]+val[top]
E TE.val = T.val
T T1* FT.val = T1.val+F.val val[newtop]=val[top-2]*val[top]
T FT.val = F.val
F (E)F.val = E.val val[ntop]=val[top-1]
F digitF.val = yylval
ProductionSemantic rule Stack operation
D T LL.in = T.type
T int T.type = integer val[ntop]=integer
T charT.type = character val[ntop]=character
L L1, idL1.in = L.in
addtype (id.index, L.in) addtype(val[top], val[top-3])
L idaddtype (id.index, L.in) addtype(val[top], val[top-1])
case2: SaABC
ProductionSemantic rule
S aACC.i = A.s
S bABC C.i = A.s
C cC.s = f(C.i)
ProductionSemantic rule
S aACC.i = A.s
S bABMC M.i=A.s, C.i = M.s
C cC.s = f(C.i)
M M.s = M.i