Compiler Construction Semantic Analysis II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

Winter Compiler Construction T6 – semantic analysis part I scopes and symbol tables Mooly Sagiv and Roman Manevich School of Computer Science.
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Symbol Table.
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
Programming Languages and Paradigms The C Programming Language.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 Compiler Construction Intermediate Code Generation.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Elaboration or: Semantic Analysis Compiler Baojian Hua
The Symbol Table Lecture 13 Wed, Feb 23, The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Tutorial 6 & 7 Symbol Table
Elaboration or: Semantic Analysis Compiler Baojian Hua
Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Semantic Analysis II Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Summary Mooly Sagiv html://
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Semantic Analysis I Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Semantic Analysis III + Intermediate Representation I.
Semantic Analysis CS 671 February 5, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens –e.g.: main$
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Winter Compiler Construction T8 – semantic analysis recap + IR part 1 Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
MIT Intermediate Formats Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Compiler Construction Compiler Construction Semantic Analysis I.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Semantic Analysis Semantic Analysis v Lexically and syntactically correct programs may still contain other errors v Lexical and syntax analyses.
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Compiler Construction Compiler Construction Semantic Analysis I.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Semantic Analysis III + Intermediate Representation I.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CMSC 330: Organization of Programming Languages Operational Semantics.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
1 Languages and Compilers (SProg og Oversættere) Semantic Analysis.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Lecture 9 Symbol Table and Attributed Grammars
Programming Languages 2nd edition Tucker and Noonan
Constructing Precedence Table
Semantic Analysis with Emphasis on Name Analysis
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Mooly Sagiv and Roman Manevich School of Computer Science
CMPE 152: Compiler Design September 27 Class Meeting
Presentation transcript:

Compiler Construction Semantic Analysis II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University

2 Administration TA1 is up LR parsing Submission deadline 20/12/2009 PA3 is up Submission deadline 16/12/2009

3 Compiler IC Program ic x86 executable exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation IC compiler We saw: Scope Symbol tables Today: Type checking Recap

4 Semantic analysis motivation Syntax analysis is not enough int a; a = “hello”; int a; b = 1; Assigning wrong type Assigning undeclared variable int a; int a; a = 1; Variable re declaration

5 Symbol table An environment that stores information about identifiers A data structure that captures scope information SymbolKindTypeProperties valuefieldint… testmethod-> intprivate setValuemethodint -> voidpublic

6 Examples of type errors int a; a = true; void foo(int x) { int x; foo(5,7); } 1 < true class A {…} class B extends A { void foo() { A a; B b; b = a; } } argument list doesn’t match formal parameters a is not a subtype of b assigned type doesn’t match declared type relational operator applied to non-int type

7 Types Type Set of values computed during program execution boolean = { true,false } int = { } void = {} Type safety Types usage adheres formally defined typing rules

8 Type 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

9 Type judgments E  e : T Formal notation for type judgments In the context E, e is a well-typed expression of T b:bool, x:int  b:bool x:int  1 + x < 4:bool foo:int->string, x:int  foo(x) : string Type context set of type bindings id : T (symbol table)

10 Typing rules Premise Conclusion [Name] Conclusion [Name] Axioms

11 Typing rules for expressions E  true : bool E  false : bool E  int-literal : int E  string-literal : string E  e 1 : intE  e 2 : int E  e 1 +e 2 : int [+] E  null : nullE  new T() : T AST leaves

12 Some IC expression rules 1 E  true : bool E  e1 : intE  e2 : int E  e1 op e2 : int E  false : bool E  int-literal : int E  string-literal : string op  { +, -, /, *, %} E  e1 : intE  e2 : int E  e1 rop e2 : bool rop  {, >=} E  e1 : TE  e2 : T E  e1 rop e2 : bool rop  { ==,!=}

13 Some IC expression rules 2 E  e1 : boolE  e2 : bool E  e1 lop e2 : bool lop  { &&,|| } E  e1 : int E  - e1 : int E  e1 : bool E  ! e1 : bool E  e1 : T[] E  e1. length : int E  e1 : T[]E  e2 : int E  e1[e2] : T E  e1 : int E  new T[e1] : T[] E  new T() : T E  e:C ( id : T)  C E  e.id : T

14 Type-checking algorithm 1. Construct types 1. Add basic types to type table 2. Traverse AST looking for user-defined types (classes,methods,arrays) and store in table 3. Bind all symbols to types 2. Traverse AST bottom-up (using visitor) 1. For each AST node find corresponding rule (there is only one for each kind of node) 2. Check if rule holds 1. Yes: assign type to node according to consequent 2. No: report error

15 45 > 32 && !false BinopExpr UnopExpr BinopExpr … op=AND op=NEG op=GT intLiteral val=45 intLiteral val=32 boolLiteral val=false : int : bool E  false : bool E  int-literal : int E  e1 : intE  e2 : int E  e1 rop e2 : bool rop  {, >=} E  e1 : bool E  e2 : bool E  e1 lop e2 : bool lop  { &&,|| } E  e1 : bool E  !e1 : bool Algorithm example

16 Statement rules Statements have type void Judgments of the form E  S In environment E, S is well-typed E  e:bool E  S E  while (e) S E  e:bool E  S E  if (e) S E  e:bool E  S 1 E  S 2 E  if (e) S 1 else S 2 E  break E  continue

17 Checking return statements Special entry { ret :T r } represents return value Add to symbol table when entering method Lookup entry when hit return statement ret :void  E E  return; ret :T’  E T≤T’ E  return e; E  e:T T subtype of T’

18 Subtyping 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[]

19 Type checking with subtyping S ≤ T S may be used whenever T is expected An Expression E from type S also has type T E  e : S S ≤ T E  e : T

20 IC rules with subtyping E  e1 : T1 E  e2 : T2 T1 ≤ T2 or T2 ≤ T1 op {==,!=} E  e1 op e2 : bool

21 Semantic 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

22 Class hierarchy for types abstract class Type {...} class IntType extends Type {...} class BoolType extends Type {...} class ArrayType extends Type { Type elemType; } class MethodType extends Type { Type[] paramTypes; Type returnType;... } class ClassType extends Type { ICClass classAST;... }...

23 Type comparison Use a unique object for each distinct type Resolve each type expression to same object Use reference equality for comparison (==)

24 Type table implementation class TypeTable { // Maps element types to array types private Map uniqueArrayTypes; private Map 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 doesn’t exist – create and return it ArrayType arrt = new ArrayType(elemType); uniqueArrayTypes.put(elemType,ArrayType); return arrt; } }... }

Recap

26 Semantic analysis flow example class A { int x; int f(int x) { boolean y;... } } class B extends A { boolean y; int t; } class C { A o; int z; }

27 Parsing and AST construction IntType BoolType A B C f : int->int … TypeTable Table populated with user-defined types during parsing (or special AST pass) class A { int x; int f(int x) { boolean y;... } } class B extends A { boolean y; int t; } class C { A o; int z; } parser.parse() ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … …

28 Defined types and type table class A { int x; int f(int x) { boolean y;... } } class B extends A { boolean y; int t; } class 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) {…} } abstract class Type { String name; boolean subtypeof(Type t) {...} } class IntType extends Type {...} class BoolType extends Type {...} class ArrayType extends Type { Type elemType; } class MethodType extends Type { Type[] paramTypes; Type returnType; } class ClassType extends Type { ICClass classAST; } IntType BoolType A B C f : int->int … TypeTable

29 Assigning types by declarations IntType BoolType... TypeTable ClassType name = A ClassType name = B ClassType name = C MethodType name = f retType paramTypes type super All type bindings available during parsing time ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … …

30 Symbol tables ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab abstract class SymbolTable { private SymbolTable parent; } class ClassSymbolTable extends SymbolTable { Map methodEntries; Map fieldEntries; } class MethodSymbolTable extends SymbolTable { Map variableEntries; } abstract class Symbol { String name; } class VarSymbol extends Symbol {…} class LocalVarSymbol extends Symbol {…} class ParamSymbol extends Symbol {…}...

31 Scope nesting in IC SymbolKindTypeProperties Global SymbolKindTypeProperties Class SymbolKindTypeProperties Method SymbolKindTypeProperties Block names of all classes fields and methods formals + locals variables defined in block class GlobalSymbolTable extends SymbolTable {} class ClassSymbolTable extends SymbolTable {} class MethodSymbolTable extends SymbolTable {} class BlockSymbolTable extends SymbolTable {}

32 Symbol tables ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab this belongs to method scope ret can be used later for type-checking return statements Location name = x type = ? …

33 Sym. tables phase 1 : construction ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab class TableBuildingVisitor implements Visitor {... } Location name = x type = ? … Build tables, Link each AST node to enclosing table abstract class ASTNode { SymbolTable enclosingScope; } enclosingScope symbol ?

34 ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab class TableBuildingVisitor implements Visitor {... } During this phase, add symbols from definitions, not uses, e.g., assignment to variable x symbol ? Location name = x type = ? … Sym. tables phase 1 : construct

35 ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab symbol Location name = x type=? … Sym. tables phase 2 : resolve Resolve each id to a symbol, e.g., in x=5 in foo, x is the formal parameter of f check scope rules: illegal symbol re-definitions, illegal shadowing, illegal use of undefined symbols... class SymResolvingVisitor implements Visitor {... } enclosingScope

36 ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … Location name = x type = IntType … Type-check AST IntType BoolType... TypeTable class TypeCheckingVisitor implements Visitor {... } Use type-rules to infer types for all AST expression nodes Check type rules for statements

37 ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … Location name = x type = IntType … Miscellaneous semantic checks class SemanticChecker {... } Check remaining semantic checks: single main method, break/continue inside loops etc.