

























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!


























The type of value stored in each variable The type of argument and return values for each function The representation/interpretation of each value The memory space allocated for each variable The scope and live range of each variable
Compilers need properties of variables before translation Use symbol tables to keep track of variable information
Determine properties of program constructs Cannot be handled by context-free grammar E.g., CFG cannot enforce all variables are declared before used
Attribute grammar (Syntax directed definition) Associate a set of attributes with each grammar symbol Associate a set of semantic rules with each production Specify how to compute attribute values of symbols Systematic evaluation of context information through traversal of parse tree (or abstract syntax tree)
e.val= e.val= e.val= e.val= 5
e.val= 15 20 Annotated parse tree for 5+1520: e ::= e1 / e2 e.val = e1.val [/] e2.val e ::= e1 * e2 e.val = e1.val [] e2.val e ::= e1 - e2 e.val = e1.val [-] e2.val e ::= e1 + e2 e.val = e1.val [+] e2.val e ::= n e.val = n.val production Semantic rules
The attribute value of parent is determined from attribute values of children in the parse tree
e ::= e1 / e2 e.val = e1.val [/] e2.val e ::= e1 * e2 e.val = e1.val [*] e2.val e ::= e1 - e2 e.val = e1.val [-] e2.val e ::= e1 + e2 e.val = e1.val [+] e2.val e ::= n e.val = n.val production Semantic rules e.val= e.val= e.val= e.val= 5
e.val= 15 20
Value of b must be evaluated after evaluating value of c There is a dependence from c to b real T.type L3.in L2.in L1.in id1.entry id3.entry id2.entry
T.type=real L3.in=real real L2.in=real L1.in=real id
id , id Annotated parse tree: Dependency graph: L3.addentry L2.addentry L1.addentry
Edges go from nodes earlier in the ordering to later nodes No cycles are allowed in dependence graph Input string Parse tree Dependency graph Evaluation order for Semantic rules real T.type L3.in L2.in L1.in id1.entry id3.entry id2.entry L3.addentry L2.addentry L1.addentry
the attributes of X 1 ,X 2 ,…,Xj-1 to the left of Xj in the production the inherited attributes of A R.i = A.i Q.i = R.s A.s = Q.s
L.i = A.i M.i = L.s A.s = M.s
Production Semantic rules L-attributed definition L::= id Addtype(id.entry,L.in) L1.in := L.in Addtype(id.entry,L.in) L::=L1 ,id T::= real T.type:=real T::= int T.Type:=integer D::=T L L.in:=T.type Production Semantic rules Non L-attributed definition
Synthesized and inherited attributes L-attributes may include both synthesized and inherited attributes
e’1.inh = e’.inh [*] e.val e’.syn = e’1.syn e’ ::= * e e’ e’1.inh = e’.inh [+] e.val e’.syn = e’1.syn e’ ::= + e e’ e’.inh=n.val; e.val = e’.syn e ::= n e’ production Semantic rules e (val=305) n(val=5) e’(inh=5;syn=305) n.val=
e.val=
e(val=305) e’(inh=s yn=305) e’(inh=15, syn=300)
n.val=20 e’(inh= syn=20)
e’(inh= syn=300)
A translation scheme is a CFG where Attributes are associated with grammar symbols and Semantic actions are inserted within right sides of productions Notation for specifying translation during parsing E ::= T R R ::= ‘+’ T {print(‘+’)} R | ε T ::= num {print(num.val)} Translation scheme: E T (^) R 9 print(‘9’)^ +^ T^ print(‘+’)^ R 5 print(‘5’)^ ε Parse tree for 9+5 with actions Treat actions as though they are terminal symbols.
S-attribute of left-hand symbol computed at end of production I-attribute of right-hand symbol computed before the symbol S-attribute of right-hand symbol referenced after the symbol D::=T { L.in:=T.type } L T::= int { T.Type:=integer } T::= real { T.type:=real } L::= { L1.in := L.in } L1 ,id { Addtype(id.entry,L.in) } L::= id { Addtype(id.entry,L.in) } L::= id Addtype(id.entry,L.in) L::=L1 ,id L1.in := L.in; Addtype(id.entry,L.in) T::= real T.type:=real T::= int T.Type:=integer D::=T L L.in:=T.type
Has a formal parameter for each inherited attribute of A Returns the values of the synthesized attributes of A
Save the s-attribute of each token X into a variable X.x Generate an assignment B.s=parseB(B.i1,B.i2,…,B.ik) for each non-terminal B, where B.i1,…,B.ik are values for the L- attributes of B and B.s is a variable to store s-attributes of B. Copy the code for each action, replacing references to attributes by the corresponding variables
void parseD() { Type t = parseT(); } parseL(t); } Type parseT { switch (currentToken()) { case INT: return TYPE_INT; case REAL: return TYPE_REAL; } } void parseL(Type in) { SymEntry e = parseID(); AddType(e, in); if (currentToken() == COMMA) { parseTerminal(COMMA); parseL(in) } }
D::=T { L.in:=T.type } L T::= int { T.Type:=integer } T::= real { T.type:=real } L::= { L1.in := L.in } L1 ,id { Addtype(id.entry,L.in) } L::= id { Addtype(id.entry,L.in) } D : T {$$ = $1; } L T : INT { $$ = integer; } | REAL { $$ = real; } L : L COMMA ID { Addtype($3, $0); } | ID { Addtype($1,$0); } Exercise: how to add C array and function declarations?
A type is a collection of computable values
Accounts, banks, employees, students
Integers, strings, floating points, lists, records, tuples … Languages use types to
Compile-time and run-time type checking Prevent meaningless computation 3 + true - “Bill”
Short integers require fewer bits Access record component by a known offset Use integer units for integer operations