Semantic Analysis
Checking what parsers cannot
2301373
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
2301373 Semantic Analysis 2
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
2301373 Semantic Analysis 3
Overview (contd)
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.
2301373
Semantic Analysis
Attribute Grammar
Language construct
Attribute
Variables, function declarations, statements 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. Associated with grammar production Specify the relationship between attributes of construct in a syntactic rule. Set of attribute equations
Semantic Analysis 5
Attribute equation (semantic rule)
Attribute grammar
2301373
Example of Attribute Grammar
Construct: num digit Attribute: val Grammar num -> num digit num -> digit digit -> 0|1||9
Grammar rule num1 -> num2 digit num -> digit digit -> 0 digit -> 1 digit -> 2 digit -> 3 digit -> 4 digit -> 5 digit -> 6 digit -> 7 digit -> 8 digit -> 9
2301373 Semantic Analysis
Semantic rule [Link]= [Link]*10+ [Link] [Link] = [Link] [Link] = 0 [Link] = 1 [Link] = 2 [Link] = 3 [Link] = 4 [Link] = 5 [Link] = 6 [Link] = 7 [Link] = 8 [Link] = 9
6
Parse Tree with Attributes
Grammar rule Semantic rule num1 -> num2 digit [Link]= [Link]*10+ [Link] num -> digit [Link] = [Link] digit -> 0 [Link] = 0 digit -> 1 [Link] = 1 digit -> 2 digit -> 3 digit -> 4 [Link] = 2 [Link] = 3 [Link] = 4
num val = 45*10+5
num val = 4*10+5 digit val = 5 5
num val = 4
digit val = 4 4
digit val = 5
5
digit -> 5
digit -> 6 digit -> 7 digit -> 8 digit -> 9
2301373
[Link] = 5
[Link] = 6 [Link] = 7 [Link] = 8 [Link] = 9
Semantic Analysis 7
Attribute Grammar for Data Type Declaration
Grammar Rule dec -> type varList type -> int type -> float varList1 -> id , varList2 varList -> id
dec type dtype= real varList dtype=real dtype=real dtype=real
8
Semantic Rule [Link] = [Link] [Link] = int [Link] = real [Link] = [Link] [Link]= [Link] [Link] = [Link]
float
id dtype=real
varList id
2301373
Semantic Analysis
Another Example of Attribute Grammar
Grammar Rule Bnum -> num baseC baseC ->o baseC -> d num1 -> num2 digit Semantic Rule [Link]=[Link] [Link]=8 [Link]=10 [Link]=[Link]
[Link]=[Link] [Link]=[Link] [Link]= if [Link]=error then error else [Link]*[Link] + [Link]
[Link]=[Link] [Link]=[Link] [Link] = numval(D), where D is 0, 1,,7 [Link] = if [Link]=8 then error else numval(D), where D is 8,9 Bnum val=19
num -> digit
digit -> 0|1||7
digit -> 8 | 9
base=8 val=19
num
baseC
base=8
base=8 num val=2 digit
2301373
digit
base=8 val=2
base=8 val=3
o
9
Semantic Analysis
Evaluation Order
From semantic rule X.a=f(X1.a1, X2.a2,, [Link])
Value of a in node X depends on the values of a1 in X1, a2 in X2,, and an in Xn.
X.a X1.a1 X2.a2 X.a X1.a1 X2.a2 X.a [Link] [Link]
The order of evaluation can be shown in a dependency graph, which is a directed acyclic graph (DAG).
X1.a1
X2.a2 X.a
[Link]
X1.a1
2301373 Semantic Analysis
X2.a2
[Link]
10
Dependency Graph: Example 1
Grammar rule Semantic rule num1 -> num2 digit [Link]= [Link]*10+ [Link] num -> digit [Link] = [Link] digit -> 0 [Link] = 0 digit -> 1 [Link] = 1 digit -> 2 digit -> 3 digit -> 4 [Link] = 2 [Link] = 3 [Link] = 4
num val = 45*10+5
num val = 4*10+5 digit val = 5 5
num val = 4
digit val = 4 4
digit val = 5
5
digit -> 5
digit -> 6 digit -> 7 digit -> 8 digit -> 9
2301373
[Link] = 5
[Link] = 6 [Link] = 7 [Link] = 8 [Link] = 9
Semantic Analysis 11
Dependency Graph: Example 2
Grammar Rule dec -> type varList type -> int type -> float varList1 -> id , varList2 varList -> id
dec type dtype= real varList dtype=real dtype=real dtype=real
12
Semantic Rule [Link] = [Link] [Link] = int [Link] = real [Link] = [Link] [Link]= [Link] [Link] = [Link]
float
id dtype=real
varList id
2301373
Semantic Analysis
Dependency Graph:Example 3
Grammar Rule Bnum -> num baseC baseC ->o baseC -> d num1 -> num2 digit Semantic Rule [Link]=[Link] [Link]=8 [Link]=10 [Link]=[Link] [Link]=[Link] [Link]= if [Link]=error then error else [Link]*[Link] + [Link] [Link]=[Link] [Link]=[Link] [Link] = numval(D), where D is 0, 1,,7 [Link] = if [Link]=8 then error else numval(D), where D is 8,9 Bnum num num digit
2301373 Semantic Analysis 13
[Link]=[Link]
num -> digit digit -> 0|1||7 digit -> 8 | 9
baseC digit
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
2301373
Semantic Analysis
14
Synthesized Attributes
An attribute a is a synthesized attribute if
Grammar rule Semantic rule
If all attributes in an attribute grammar are synthesized attributes, the grammar is called an Sattributed grammar.
for a grammar rule A ->X1 X2 Xn , an attribute equation with a on the LHS is of the form A.a = f(X1.a1, X2.a2, [Link]), or all dependencies point from child to parent in the parse tree
num1 -> num2 digit [Link]= [Link]*10+ [Link] num -> digit [Link] = [Link] digit -> 0|1||9 [Link] = val(D) num
num
num digit 4 digit
digit
5
2301373
Semantic Analysis
15
Order of Evaluation for Synthesized Attributes
Use postorder (or bottomup) evaluation
num 455 num 45 digit 5
Procedure PostEval (T:node) { for each child C of T { PostEval(C); } compute all synthesized attributes of T }
num digit 4
4 4
digit 5
2301373
Semantic Analysis
16
Inherited Attributes
Grammar Rule dec -> type varList type -> int Semantic Rule [Link] = [Link] [Link] = int
type float dec varList id , varList id
type -> float [Link] = real varList1 -> id , varList2 [Link] = [Link] varList -> id [Link] = [Link]
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 Xn depends on the value of attributes of symbols X, X1, X2,, Xi-1.
2301373 Semantic Analysis 17
Order of Evaluation for Inherited Attributes
Use preorder and inorder evaluation together
Procedure Eval (T:node) { case nodeType(T) of dec: { Eval(typeChild(T)); [Link]=[Link]; Eval(varChild(T)); } type: { if child(T) is int then [Link]=int; if child(T) is float then [Link]=real; } varList: { leftChild(T).dtype=[Link]; if (rightmostChild(T) is not null) then {rightmostChild(T).dtype=[Link]; Eval(rightChild(T));}
Semantic Analysis 18
dec type
float
varList id
float
float float
float
, varList
id
float
2301373
Another Example of Inherited Attributes
Proc Eval(T:node) Grammar Rule Semantic Rule { case Nodetype(T) of Bnum -> num baseC [Link]=[Link]; [Link]=[Link] Bnum: baseC ->o [Link]=8 { Eval(rightChild(T)); (leftChild(T)).base= baseC -> d [Link]=10 (rightChild(T)).base; num1 -> num2 digit [Link]=[Link]; [Link]=[Link]; Eval(leftChild(T)); [Link]= if [Link]=error then error else [Link]=leftChild(T).val; } [Link]*[Link] + [Link] baseC: { if child(T)=o then [Link]=8; num -> digit [Link]=[Link]; [Link]=[Link] if child(T)=d [Link]=10;} digit -> 0|1||7 [Link] = numval(D) num: digit -> 8 | 9 [Link] = if [Link]=8 then error else { leftChild(T).base=[Link]; numval(D) Eval(leftChild(T)); Bnum val=44 if (rightChild(T)!=null) then val=44 { rightChild(T).base=[Link]; base=8 base=8 num baseC Eval(rightChild(T); [Link]=f(T); } base=8 val=5 base=8 else [Link]=leftChild(T).val; cal num digit o val=4 val} val=5 digit: base=8 digit { } 2301373 Semantic Analysis 19 }
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; }
2301373
Semantic Analysis
20
Attribute Computation During Parsing
2301373
Semantic Analysis
21