

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Paper; Class: Compiler Constr; Subject: Electrical Engineering And Computer Science; University: University of Michigan - Ann Arbor; Term: Fall 2003;
Typology: Papers
1 / 25
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
LL/LR parsing techniques implicitly buildAST
Y
The parse tree is captured in the derivation
We want to explicitly construct the ASTduring the parsing phase
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
num | (S)
Num(1)
Num(2)
input string: “1 + 2 + 3” Add
Add
Num(1)
Num(2)
Num(3)
Add
stack
Num(3)
Before reduction: S
After reduction: S
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
Y
» Parser generators extend the stack of symbols with
entries for user-defined structures (e.g., parse trees)
Y
» Need to refer to multiple occurences of the same non-
terminal symbol, distinguish RHS vs LHS occurrence y
E
Æ
E + E
» Use dollar variables in yacc/bison ($$, $1, $2, etc.)
y
expr ::= expr PLUS expr
{$$ = $1 + $3;}
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
Can use syntax-directed definitions toperform semantic checks during parsing
Benefit = efficiency
Disadvantage = unstructured code
T id
{AddType(id, T.type);
D.type = T.type; }
D1, id
{AddType(id, D1.type);
D.type = D1.type; }
int
{T.type = intType; }
float
{T.type = floatType; }
{AddType($2, $1.type);$$.type = $1.type; }
this really looks like
{AddType(id, T.type);
D.type = T.type; L.type = D.type; }
int
{T.type = intType; }
float
{T.type = floatType; }
L1, id
{AddType(id, L1.type);
id
{AddType(id, ???); }
Propagate values both bottom-up and top-down
int a, b
id
int
intType
T.type
L.type
D.type
id
L.type
AddType(id, L.type)
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)