Compilation 2016
Semantic Analysis
Aslan Askarov aslan@cs.au.dk Partially based on slides by E. Ernst
Semantic Analysis Aslan Askarov aslan@cs.au.dk Partially based on - - PowerPoint PPT Presentation
Compilation 2016 Semantic Analysis Aslan Askarov aslan@cs.au.dk Partially based on slides by E. Ernst Where are we Earlier this week: Abstract syntax trees tree representation of a program Scoping rule how to
Compilation 2016
Aslan Askarov aslan@cs.au.dk Partially based on slides by E. Ernst
adheres to the typing rules of the language.
reported to the user
with type information
information, because it is strictly speaking not needed from this point on
let var x : Int := 10 var s : String := “hi” in if x then s else 42 end
Is this program well-typed?
structure Types : TYPES = struct type unique = unit ref datatype ty = INT | STRING | RECORD of (Symbol.symbol * ty) list * unique | ARRAY of ty * unique | NIL | UNIT | NAME of Symbol.symbol * ty option ref | ERROR (* ambiguity exists due to type error *) end
class C1 {.. /* some declarations */ ..} class C2 {.. /* identical to C1, rename as needed */ ..}
are not the same type
‘unit ref’ is always a unique value
structure Types : TYPES = struct type unique = unit ref datatype ty = ... | RECORD of (Symbol.symbol * ty) list * unique | ARRAY of ty * unique ... end
another AST traversal
A separate AST traversal to generate unique identifiers for each record/array declaration would be a cleaner implementation approach, but at the cost of writing/maintaining an extra AST pass; so the unit ref hack is okay
TypeDec 'rectype2' RecordTy 'string' 'name' 'arrtype1' 'dates' 238378 unique_id TypeDec 'rectype2' RecordTy 'string' 'name' 'arrtype 1' 'dates'
type = …
full Tiger language needs two environments
friends take an extra environment argument transExp (venv, tenv) exp
representation, choice of variant, …