











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Y
Derivation = sequence ofapplied productions »
S
Æ
E+S
Æ
1+S
Æ
1+E
Æ
1+
Y
Parse tree = graphrepresentation of aderivation »
Doesn’t capture the orderof applying theproductions
Y
AST discardsunnecessary informationfrom the parse tree
void parse_S() {
switch (token) {
case num: case ‘(‘:
parse_E(); parse_S’(); return; default:
ParseError();
Expr parse_S() {
switch (token) {
case num: case ‘(‘:
Expr left = parse_E(); Expr right = parse_S’(); if (right == NULL) return left else return new Add(left,right); default:
ParseError();
ε
num | (S)
LL parsing: extend procedures for non-terminals
Y
Y
» The generated parser needs to contain AST
construction code » How to construct a customized AST data structure
using an automatic parser generator?
Y
» E.g., semantic checks » This can reduce the number of compiler passes
y
E + S {action}
Y
Y
expr ::= NUM
{$$ = new Num($1.val); }
expr ::= expr PLUS expr
{$$ = new Add($1, $3); }
expr ::= expr MULT expr
{$$ = new Mul($1, $3); }
expr ::= LPAR expr RPAR
num | (E) | E + E | E * E
Perform a LR derivation of the string: “(1+2)*3” Show where each part of the AST is constructed
Propagate type attributes while building AST from the bottom to the top
T id
D1, id
int
float
int a, b
id
T.type
D.type
D.type
AddType(id, D.type)
id
AddType(id, T.type)
intType
int
Propagate values both bottom-up and top-down
AddType(id, L.type)
D
id
int
intType
T.type
L.type
D.type
id
L.type
int a, b
int
float
L1, id
id
AddType(id, L.type)
Y
» Values from parents and siblings = inherited » Values from children = synthesized
Y
Y
» May compute inherited attrs from its children and pass
these values down the parse tree » May compute synthesized attribute and pass these
values up the parse tree
Y
Y
» Construct AST, use that to establish the dependence
relationships to guide attribute evaluation » Most flexible, but may fail if get cycle » Build dep graph, topo sort determines order
Y
» Order of evaluation of attributes established when the
compiler is constructed
Y
» Order determined by order nodes are visited (e.g.,
parsing method, top-down or bottom-up)
Y
Y
Y
» It is better when efficiency is not a critical issue
Y