Title: Compiler Construction Semantic Analysis II
1Compiler ConstructionSemantic Analysis II
- Ran Shaham and Ohad Shacham
- School of Computer Science
- Tel-Aviv University
2PAs
- PA2
- Deadline extension Dec 29
- Fix PA1 graders notes in your IC.lex
- PA1
- Escape characters should stay as they are
- ltSTRINGgt \\n str.append(\n)
3IC compiler
Compiler
LexicalAnalysis
Syntax Analysis Parsing
AST
SymbolTableetc.
Inter.Rep.(IR)
CodeGeneration
- We saw
- Scope
- Symbol tables
- Today
- Type checking
- Recap
4Examples of type errors
1 lt true
int a a true
class A class B extends A void foo()
A a B b b a
void foo(int x) int x foo(5,7)
5Types
- Type
- Set of values computed during program execution
- boolean true,false
- int -232,232
- void
- Type safety
- Types usage adheres formally defined typing rules
6Type judgments
- e T
- Formal notation for type judgments
- e is a well-typed expression of type T
- 2 int
- 2 (3 4) int
- true bool
- Hello string
7Type judgments
- E ? e T
- Formal notation for type judgments
- In the context E, e is a well-typed expression of
T - bbool, xint ? bbool
- xint ? 1 x lt 4bool
- fooint-gtstring, xint ? foo(x) string
- Type context
- set of type bindings id T (symbol table)
8Typing rules
Premise
Name
Conclusion
Axioms
Name
Conclusion
9Typing rules for expressions
E ? e1 int
E ? e2 int
E ? e1e2 int
AST leaves
E ? true bool
E ? false bool
E ? int-literal int
E ? string-literal string
E ? null null
E ? new T() T
10Some IC expression rules 1
E ? true bool
E ? false bool
E ? int-literal int
E ? string-literal string
E ? e1 int
E ? e2 int
op ? , -, /, ,
E ? e1 op e2 int
E ? e1 int
E ? e2 int
rop ? lt,lt, gt, gt
E ? e1 rop e2 bool
E ? e1 T
E ? e2 T
rop ? ,!
E ? e1 rop e2 bool
11Some IC expression rules 2
E ? e1 bool
E ? e2 bool
lop ? ,
E ? e1 lop e2 bool
E ? e1 int
E ? e1 bool
E ? - e1 int
E ? ! e1 bool
E ? e1 T
E ? e1 T
E ? e2 int
E ? e1 int
E ? e1e2 T
E ? e1.length int
E ? new Te1 T
id T ? E
E ? id T
E ? new T() T
12Type-checking algorithm
- Construct types
- Add basic types to type table
- Traverse AST looking for user-defined types
(classes,methods,arrays) and store in table - Bind all symbols to types
- Traverse AST bottom-up (using visitor)
- For each AST node find corresponding rule(there
is only one for each kind of node) - Check if rule holds
- Yes assign type to node according to consequent
- No report error
13Algorithm example
BinopExpr
bool
opAND
bool
bool
BinopExpr
UnopExpr
opNEG
opGT
int
int
bool
45 gt 32 !false
14Type-checking visitor
class TypeChecker implements PropagatingVisitorltTy
pe,SymbolTablegt public Type
visit(ArithBinopExp e, SymbolTable symtab) throws
Exception Type lType
e.left.accept(this, symtab) Type rType
e.right.accept(this, symtab) if (lType
! TypeTable.intType()) throw new
TypeError(Expecting int type, found
lType.toString(),
e.getLine) if (rType !
TypeTable.intType) throw new
TypeError(Expecting int type, found
rType.toString(),
e.getLine) // we only get here if no
exceptions were thrown e.type
TypeTable.intType ...
15Statement rules
- Statements have type void
- Judgments of the form E ? S
- In environment E, S is well-typed
16Checking return statements
- Special entry retTr represents return value
- Add to symbol table when entering method
- Lookup entry when hit return statement
retvoid ? E
retT?E TT
E ? eT
E ? return
E ? return e
17Subtyping
- Inheritance induces subtyping relation
- Type hierarchy is a tree
- Subtyping rules
A extends B
A B
A A
A B B C
A C
null A
- Subtyping does not extend to array types
- A subtype of B then A is not a subtype of B
18Type checking with subtyping
- S T
- S may be used whenever T is expected
- An Expression E from type S also has type T
19IC rules with subtyping
E ? e1 T1 E ? e2 T2 T1 T2 or T2
T1op ,!
E ? e1 op e2 bool
20Semantic analysis flow
- Parsing and AST construction
- Combine library AST with IC program AST
- Construct and initialize global type table
- Phase 1 Symbol table construction
- Construct class hierarchy and check that
hierarchy is a tree - Construct remaining symbol table hierarchy
- Assign enclosing-scope for each AST node
- Phase 2 Scope checking
- Resolve names
- Check scope rules using symbol table
- Phase 3 Type checking
- Assign type for each AST node
- Phase 4 Remaining semantic checks
21Class hierarchy for types
abstract class Type ...class IntType extends
Type ...class BoolType extends Type
...class ArrayType extends Type Type
elemTypeclass MethodType extends Type
Type paramTypes Type returnType ...
class ClassType extends Type ICClass
classAST ......
22Type comparison
- Option 1 use a unique object for each distinct
type - Resolve each type expression to same object
- Use reference equality for comparison ()
- Option 2 implement a method t1.equals(t2)
- Perform deep (structural) test
- For object-oriented languages also need
sub-typing t1.subtypeof(t2)
23Type table implementation
class TypeTable // Maps element types to
array types private MapltType,ArrayTypegt
uniqueArrayTypes private MapltString,ClassType
gt uniqueClassTypes public static Type
boolType new BoolType() public static Type
intType new IntType() ... // Returns
unique array type object public static
ArrayType arrayType(Type elemType) if
(uniqueArrayTypes.containsKey(elemType))
// array type object already created return
it return uniqueArrayTypes.get(elemType)
else // object
doesnt exist create and return it
ArrayType arrt new ArrayType(elemType)
uniqueArrayTypes.put(elemType,ArrayType)
return arrt ...
24Recap
25Semantic analysis flow example
class A int x int f(int x)
boolean y ... class B extends A
boolean y int tclass C A o int
z
26Parsing and AST construction
AST
Programfile
class A int x int f(int x)
boolean y ... class B extends A
boolean y int tclass C A o int
z
parser.parse()
classes2
classes0
classes1
ICClassname A
ICClassname Bsuper A
ICClassname C
fields0
methods0
Fieldname xtype IntType
Methodname f
body
parameters0
DeclarationvarName yinitExpr nulltype
BoolType
Paramname xtype IntType
TypeTable
IntTypeBoolTypeABCf int-gtint
Table populated with user-defined types during
parsing(or special AST pass)
27Defined types and type table
abstract class Type String name boolean
subtypeof(Type t) ...class IntType extends
Type ...class BoolType extends Type
...class ArrayType extends Type Type
elemTypeclass MethodType extends Type
Type paramTypes Type returnTypeclass
ClassType extends Type ICClass classAST
class A int x int f(int x)
boolean y ... class B extends A
boolean y int tclass C A o int
z
class TypeTable public static Type boolType
new BoolType() public static Type intType
new IntType() ... public static
ArrayType arrayType(Type elemType) public
static ClassType classType(String name, String
super, ICClass
ast) public static MethodType
methodType(String name,Type retType,
Type paramTypes)
TypeTable
IntTypeBoolTypeABCf int-gtint
28Assigning types by declarations
AST
All type bindings available during parsing time
Programfile
classes2
classes0
classes1
ICClassname A
ICClassname Bsuper A
ICClassname C
TypeTable
type
IntTypeBoolType ...
fields0
methods0
Fieldname xtype IntType
Methodname f
ClassTypename C
type
body
parameters0
ClassTypename B
DeclarationvarName yinitExpr nulltype
BoolType
type
Paramname xtype IntType
super
ClassTypename A
type
MethodTypename fretTypeparamTypes
29Symbol tables
Global symtab
AST
A CLASS
B CLASS
C CLASS
Programfile
classes2
classes0
classes1
A symtab
C symtab
ICClassname A
ICClassname Bsuper A
ICClassname C
x FIELD IntType
f METHOD int-gtint
o CLASS A
z FIELD IntType
fields0
methods0
Fieldname xtype IntType
Methodname f
f symtab
B symtab
x PARAM IntType
y VAR BoolType
this VAR A
ret RET_VAR IntType
t FIELD IntType
y FIELD BoolType
body
parameters0
DeclarationvarName yinitExpr nulltype
BoolType
Paramname xtype IntType
abstract class SymbolTable private
SymbolTable parentclass ClassSymbolTable
extends SymbolTable MapltString,Symbolgt
methodEntries MapltString,Symbolgt
fieldEntries class MethodSymbolTable extends
SymbolTable MapltString,Symbolgt
variableEntries
abstract class Symbol String nameclass
VarSymbol extends Symbol class
LocalVarSymbol extends Symbol class
ParamSymbol extends Symbol ...
30Scope nesting in IC
class GlobalSymbolTable extends SymbolTable
class ClassSymbolTable extends SymbolTable
class MethodSymbolTable extends SymbolTable
class BlockSymbolTable extends SymbolTable
Global
Symbol Kind Type Properties
names of all classes
Class
Symbol Kind Type Properties
fields and methods
Method
Symbol Kind Type Properties
formals locals
Block
Symbol Kind Type Properties
variables defined in block
31Symbol tables
Global symtab
AST
A CLASS
B CLASS
C CLASS
Programfile
classes2
classes0
classes1
A symtab
C symtab
ICClassname A
ICClassname Bsuper A
ICClassname C
x FIELD IntType
f METHOD int-gtint
o CLASS A
z FIELD IntType
fields0
methods0
Fieldname xtype IntType
Methodname f
f symtab
B symtab
x PARAM IntType
y VAR BoolType
this VAR A
ret RET_VAR IntType
t FIELD IntType
y FIELD BoolType
body
parameters0
DeclarationvarName yinitExpr nulltype
BoolType
Paramname xtype IntType
this belongs to method scope
Locationname xtype ?
ret can be used later for type-checking return
statements
32Sym. tables phase 1 construction
Global symtab
AST
enclosingScope
A CLASS
B CLASS
C CLASS
Programfile
classes2
classes0
classes1
A symtab
C symtab
ICClassname A
ICClassname Bsuper A
ICClassname C
x FIELD IntType
f METHOD int-gtint
o CLASS A
z FIELD IntType
fields0
methods0
Fieldname xtype IntType
Methodname f
f symtab
B symtab
x PARAM IntType
y VAR BoolType
this VAR A
ret RET_VAR IntType
t FIELD IntType
y FIELD BoolType
body
parameters0
DeclarationvarName yinitExpr nulltype
BoolType
Paramname xtype IntType
Build tables,Link each AST node to enclosing
table
?
Locationname xtype ?
symbol
abstract class ASTNode SymbolTable
enclosingScope
class TableBuildingVisitor implements Visitor
...
33Sym. tables phase 1 construct
Global symtab
AST
A CLASS
B CLASS
C CLASS
Programfile
classes2
classes0
classes1
A symtab
C symtab
ICClassname A
ICClassname Bsuper A
ICClassname C
x FIELD IntType
f METHOD int-gtint
o CLASS A
z FIELD IntType
fields0
methods0
Fieldname xtype IntType
Methodname f
f symtab
B symtab
x PARAM IntType
y VAR BoolType
this VAR A
ret RET_VAR IntType
t FIELD IntType
y FIELD BoolType
body
parameters0
DeclarationvarName yinitExpr nulltype
BoolType
Paramname xtype IntType
During this phase, add symbols from definitions,
not uses, e.g., assignment to variable x
?
Locationname xtype ?
symbol
class TableBuildingVisitor implements Visitor
...
34Sym. tables phase 2 resolve
Global symtab
AST
A CLASS
B CLASS
C CLASS
Programfile
classes2
classes0
classes1
A symtab
C symtab
ICClassname A
ICClassname Bsuper A
ICClassname C
x FIELD IntType
f METHOD int-gtint
o CLASS A
z FIELD IntType
fields0
methods0
Fieldname xtype IntType
Methodname f
f symtab
B symtab
x PARAM IntType
y VAR BoolType
this VAR A
ret RET_VAR IntType
t FIELD IntType
y FIELD BoolType
body
parameters0
DeclarationvarName yinitExpr nulltype
BoolType
Paramname xtype IntType
Resolve each id to a symbol,e.g., in x5 in foo,
x is the formal parameter of f
Locationname xtype?
symbol
check scope rulesillegal symbol
re-definitions,illegal shadowing,illegal use of
undefined symbols...
enclosingScope
class SymResolvingVisitor implements Visitor
...
35Type-check AST
AST
Programfile
TypeTable
classes2
IntTypeBoolType ...
classes0
classes1
ICClassname A
ICClassname Bsuper A
ICClassname C
fields0
methods0
Fieldname xtype IntType
Methodname f
body
parameters0
DeclarationvarName yinitExpr nulltype
BoolType
Paramname xtype IntType
Use type-rules to infer types for all AST
expression nodes
Check type rules for statements
Locationname xtype IntType
class TypeCheckingVisitor implements Visitor
...
36Miscellaneous semantic checks
AST
Programfile
classes2
classes0
classes1
ICClassname A
ICClassname Bsuper A
ICClassname C
fields0
methods0
Fieldname xtype IntType
Methodname f
body
parameters0
DeclarationvarName yinitExpr nulltype
BoolType
Paramname xtype IntType
Check remaining semantic checks single main
method, break/continue inside loops etc.
Locationname xtype IntType
class SemanticChecker ...
37- public Type visit(While whileStatement)
- Type conditionType whileStatement.getCondition()
.accept(this) - whileStatement.setType(PrimitiveTypes.VOID)
- if (conditionType ! PrimitiveTypes.BOOLEAN)
- Error
- loopDepth
- whileStatement.getOperation().accept(this)
- --loopDepth
- return null
38- public Type visit(Break breakStatement)
- if (loopDepth 0)
- error
- setType(PrimitiveTypes.VOID)
- return null