210 likes | 493 Views
Semantic Analysis. Checking what parsers cannot. Outline. Overview Attribute grammar Attribute Semantic rule Computing attributes Evaluation order Synthesized and Inherited attribute Computation of attributes during parsing Type checking Type declaration Type inference Symbol table.
E N D
Semantic Analysis Checking what parsers cannot Semantic Analysis
Outline • Overview • Attribute grammar • Attribute • Semantic rule • Computing attributes • Evaluation order • Synthesized and Inherited attribute • Computation of attributes during parsing • Type checking • Type declaration • Type inference • Symbol table Semantic Analysis
Overview • Semantics • Static semantics • Data type (in some languages e.g. C, FORTRAN) • Names • Run-time semantics • Data value • Data type (in other languages e.g. Perl) • Semantic analyzer • Determine if the semantic of the program is correct according to the definition of the language • Concern only static semantics Semantic Analysis
Overview (cont’d) • How to describe semantics • Associated to syntax • Syntax-directed semantics • An attribute represents a semantic concept. • A semantic rule (or attribute equation) is associated to a syntactic rule. • A semantic rule describes the relationship between attributes of symbols in a syntactic rule. Semantic Analysis
Attribute Grammar • Language construct • Variables, function declarations, statements • Attribute • Property of language construct • Name, address, type, value, and scope are attributes of variables. • Return type, parameter types, return address, and scope are attributes of functions. • Syntax tree is an attributes of code sections. • Attribute equation (semantic rule) • Associated with grammar production • Specify the relationship between attributes of construct in a syntactic rule. • Attribute grammar • Set of attribute equations Semantic Analysis
Construct: num digit Attribute: val Grammar num -> num digit num -> digit digit -> 0|1|…|9 Example of Attribute Grammar Semantic Analysis
Parse Tree with Attributes num val = 45*10+5 digit val = 5 num val = 4*10+5 5 num val = 4 digit val = 5 5 digit val = 4 4 Semantic Analysis
Grammar Rule dec -> type varList type -> int type -> float varList1 -> id , varList2 varList -> id Semantic Rule varList.dtype = type.dtype type.dtype = int type.dtype =real id.dtype = varList1.dtype varList2.dtype= varList1.dtype id.dtype = varList.dtype Attribute Grammar for Data Type Declaration dec type dtype= real varList dtype=real dtype=real id varList float , dtype=real dtype=real id Semantic Analysis
Bnum num baseC num digit o digit Another Example of Attribute Grammar val=19 base=8 val=19 base=8 base=8 val=2 base=8 val=3 base=8 val=2 Semantic Analysis
X.a X1.a1 X2.a2 Xn.an … X.a X2.a2 X1.a1 … Xn.an Evaluation Order • From semantic rule X.a=f(X1.a1, X2.a2,…, Xn.an) • Value of a in node X depends on the values of a1in X1, a2in X2,…, and anin Xn. • The order of evaluation can be shown in a dependency graph, which is a directed acyclic graph (DAG). X.a X1.a1 X2.a2 Xn.an … X.a X2.a2 X1.a1 … Xn.an Semantic Analysis
Dependency Graph: Example 1 num val = 45*10+5 digit val = 5 num val = 4*10+5 5 num val = 4 digit val = 5 5 digit val = 4 4 Semantic Analysis
Grammar Rule dec -> type varList type -> int type -> float varList1 -> id , varList2 varList -> id Semantic Rule varList.dtype = type.dtype type.dtype = int type.dtype =real id.dtype = varList1.dtype varList2.dtype= varList1.dtype id.dtype = varList.dtype Dependency Graph: Example 2 dec type dtype= real varList dtype=real dtype=real id varList float , dtype=real dtype=real id Semantic Analysis
Dependency Graph:Example 3 Bnum num baseC num digit o digit Semantic Analysis
Rule-based Attribute Evaluation • Order of attribute evaluation can be fixed at compiler construction • Attribute grammar is to be analyzed in order to find the orger of evaluation • Used often in practice • Not general method • Two types of attributes • Synthesized attributes • Inherited attributes Semantic Analysis
Synthesized Attributes • An attribute ais a synthesized attribute if • for a grammar rule A ->X1 X2 … Xn , anattribute equationwitha on the LHS is of the form A.a = f(X1.a1, X2.a2, … Xn.an), or • all dependencies point from child to parent in the parse tree • If all attributes in an attribute grammar are synthesized attributes, the grammar is called an S-attributed grammar. num num digit num digit 5 digit 5 4 Semantic Analysis
Order of Evaluation for Synthesized Attributes • Use postorder (or bottom-up) evaluation Procedure PostEval (T:node) { for each child C of T { PostEval(C); } compute all synthesized attributes of T } 455 num 45 5 num digit 4 5 5 num digit 5 4 digit 5 4 Semantic Analysis
Inherited Attributes dec type varList float id , varList id • An attribute is an inherited attribute if it is not a synthesized attribute. • An attribute grammar is an L-attributed grammar if for each inherited attribute aj at Xi in each grammar rule X -> X1 X2 … Xndepends on the value of attributes of symbols X, X1, X2,…, Xi-1. Semantic Analysis
Order of Evaluation for Inherited Attributes • Use preorder and inorder evaluation together Procedure Eval (T:node) { case nodeType(T) of dec: { Eval(typeChild(T)); varList.dtype=type.dtype; Eval(varChild(T)); } type: { if child(T) is int then T.dtype=int; if child(T) is float then T.dtype=real; } varList: { leftChild(T).dtype=T.dtype; if (rightmostChild(T) is not null) then {rightmostChild(T).dtype=T.dtype; Eval(rightChild(T));} } } dec float float type varList float float float id varList , float id Semantic Analysis
Another Example of Inherited Attributes Proc Eval(T:node) { case Nodetype(T) of Bnum: { Eval(rightChild(T)); (leftChild(T)).base= (rightChild(T)).base; Eval(leftChild(T)); T.val=leftChild(T).val; } baseC: { if child(T)=o then T.base=8; if child(T)=d thenT.base=10;} num: { leftChild(T).base=T.base; Eval(leftChild(T)); if (rightChild(T)!=null) then { rightChild(T).base=T.base; Eval(rightChild(T); T.val=f(T); } else T.val=leftChild(T).val; cal val} digit: { … } } val=44 Bnum val=44 base=8 base=8 num baseC base=8 base=8 val=5 num digit o val=4 val=5 base=8 digit Semantic Analysis
Evaluation Order for Synthesized + Inherited Attributes Procedure CombinedEval(T:node) { for each child C of T { compute all inherited attributes of C; CombinedEval(C); } compute all synthesized attributes of T; } Semantic Analysis
Attribute Computation During Parsing Semantic Analysis