Professional Documents
Culture Documents
4 Semanticanalysis
4 Semanticanalysis
4. Semantic Analysis
ExprStmt
while Condition do
MethodCall
MulExpr
print
iij 10
id mul PostIncExpr
iij id postinc
iij
6 Semantic Analysis CZ3007
Example of an AST
WhileStmt
cond body
back
LtExpr ExprStmt
left right expr
MethodCall
Name Literal callee args
Name MulExpr
iij 10 left right
iij
7 Semantic Analysis CZ3007
Design of the AST
We devise the AST for the grammar to retain the essential
information needed for name and type analysis and
code generation.
Examples
Assignment statement: the target of the assignment and the
value to be stored at that target.
AST subtree:
IfStmt
Condition Else
Then
WhileStmt
Condition Body
JastAdd back
Abstract grammar Java code
specified in .ast file implementing
the AST
Back to slide 76
AddExpr
left right
Literal MulExpr
left right
Literal Literal
5
4 3
For example:
BlockStmt
Running Example
An abstract grammar for describing box scenarios like the one in next
slide:
Frame ::= Box;
abstract Box;
HBox : Box ::= Left:Box Right:Box; back
VBox : Box ::= Top:Box Bottom:Box;
ABox : Box ::= <Width:Integer> <Height:Integer>;
34 Semantic Analysis CZ3007
An example of a box scenario
back
back
back
6
Box c 5
Box b
3 Box d 3
2
4 3 2 1
}
public class VBox extends Box {
...
int width() {
return Math.max(getTop().width(),
getBottom().width());
}
}
40 Semantic Analysis CZ3007
Java methods generated for the synthesised
attribute width
public class HBox extends Box {
...
int width() {
return getLeft().width() + getRight().width();
}
}
public class ABox extends Box {
...
int width() {
return getWidth();
}
}
41 Semantic Analysis CZ3007
Inherited attributes
Suppose we additionally want to find out the coordinates
of every box’s lower left corner.
6
5 Box b Box c 5
Box a
3 Box d 3
2
4 10
3 2 1
0 x
What are the computations of the coordinates of the
lower left corner of a box based on?
44 Semantic Analysis CZ3007
Defining Inherited attributes (x coordinates)
xpos is an inherited attribute of AST node Box Provide one equation for
every occurrence of a
inh int Box.xpos(); relevant node type on the
right side of a grammar rule
eq Frame.getBox().xpos() = 0;
for HBox
// Equation 1
eq HBox.getLeft().xpos() = xpos(); Parent’s attribute value
// Equation 2
eq HBox.getRight().xpos() = xpos() + getLeft().width();
// Equation 3
sibling’s attribute value
eq VBox.getTop().xpos() = xpos();
// Equation 4 The Java code on the RHS
eq VBox.getBottom().xpos() = xpos(); executes in the context of
the parent node
// Equation 5
45 Slide 33 Semantic Analysis CZ3007
Defining inherited attributes
JastAdd supports several abbreviations to make the
definition of inherited attributes less verbose. back
Abstract grammar
specified in .ast file JastAdd
Java code
Attribute grammar implementing the
specified in .jrag file AST
back
Boolean : TypeName;
ArrayTypeName : TypeName ::= ElementType:TypeName;
Note ArrayLiteral represents array literals like
[[1, 2], [3, 4], [5]]
Since the elements of an array literal may be arbitrary
expressions, they can in particular themselves be array
literals, and do not all have to be of the same length.
“x” true
back
59 Semantic Analysis CZ3007
Implement the decl attribute in JastAdd
To compute the attribute decl for a VarName, we need
to look up the place where it is defined. That is, look up
the parent nodes through the AST tree.
We define another attribute lookupVar for all Expr and
Stmt node types of the AST tree. This attribute is an
inherited attribute. Why?
So to compute attribute decl of a VarName, we compute
the lookupVar attribute of the Varname. The decl
attribute is a synthesised attribute.
BlockStmt
Stmt
BlockStmt.getStmt(2).lookupVar(String name):
Check this.getStmt(0)
Check this.getStmt(1)
IntType : TypeDescriptor;
BooleanType : TypeDescriptor; back
Program
{ int x;
Attribute value
{ int y;
BlockStmt y = x;
}
VarDecl BlockStmt
}
decl Int “x” VarDecl ExprStmt
decl
Int “y” AssignExpr
VarName VarName
“y” “x”
65 Semantic Analysis CZ3007
The type descriptor of a type name
Each type name has a type descriptor. Slide 54
IntType
68 Semantic Analysis CZ3007
The type descriptor of a type name ArrayTypeName
eq Int.getDescriptor() = TypeDescriptor.INT;
VarDecl eq ArrayTypeName.getDescriptor() =
getElementType().getDescriptor().arrayType();
TypeName Name
Slide 54
ArrayTypeName “count”
69
Slide 66 Semantic Analysis CZ3007
The type of an expression
Every expression node needs to compute its type. Slide 53
VarDecl
TypeName Name
ArrayTypeName “count”
decl
Slide 68 ElementType
Int
AssignExpr
getDescriptor ArrayType Right
Left
Program
BlockStmt