Docsity

Context Sensitive Analysis: Attribute Grammar and Type Checking | CS 5363, Study notes of Computer Science

Partial preview of the text

Download Context Sensitive Analysis: Attribute Grammar and Type Checking | CS 5363 and more Study notes Computer Science in PDF only on Docsity!

Context-sensitive Analysis

Attribute grammar and type

checking

Context-sensitive analysis

 To understand the input computation, a

compiler/interpreter must discover

 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

 Static definition of variables: variable declarations

 Compilers need properties of variables before translation  Use symbol tables to keep track of variable information

 Context-sensitive analysis

 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 ::= n | e+e | e−e | e * e | e / e

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

Synthesized attribute definition

 An attribute is synthesized if

 The attribute value of parent is determined from attribute values of children in the parse tree

e ::= n | e+e | e−e | e * e | e / e

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

Dependences in attribute evaluation

 If value of attribute b depends on attribute c,

 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

D

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

Evaluation order of attributes

 Topological order of the dependence graph

 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

L-attributed definitions

 A syntax-directed definition is L-attributed if each inherited

attribute of Xj, 1<=j<=n, on the right side of A::=X 1 X 2 …Xn,

depends only on

 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

A ::= Q R

L.i = A.i M.i = L.s A.s = M.s

A::=L M

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 ::= n e’

e’ ::= +ee’ | *ee’ | ε

e’ ::= ε e’.syn = e’.inh

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)

Translation schemes

 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.

Designing translation schemes

 Every attribute value must be available when referenced

 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

 How to compute attribute values at each production?

Top-down translation

 For each non-terminal A, construct a function that

 Has a formal parameter for each inherited attribute of A  Returns the values of the synthesized attributes of A

 The code associated with each production does the

following

 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

Top-down translation Example

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) } }

Bottom-up translation in Yacc

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?

Types in Programming

 A type is a collection of computable values

 Represent concepts from problem domain

 Accounts, banks, employees, students

 Represent different implementation of values

 Integers, strings, floating points, lists, records, tuples …  Languages use types to

 Support organization of concepts

 Support consistent interpretation of values

 Compile-time and run-time type checking  Prevent meaningless computation 3 + true - “Bill”

 Support efficient translation (by compilers)

 Short integers require fewer bits  Access record component by a known offset  Use integer units for integer operations