Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:

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
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Intermediate Representation III. 2 PAs PA2 deadline is
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Compilation (Semester A, 2013/14) Lecture 6b: Context Analysis (aka Semantic Analysis) Noam Rinetzky 1 Slides credit: Mooly Sagiv and Eran Yahav.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Compilation Encapsulation Or: Why Every Component Should Just Do Its Damn Job.
Lecture 06 – Semantic Analysis Eran Yahav 1. 2 You are here Executable code exe Source text txt Compiler Lexical Analysis Syntax Analysis Parsing Semantic.
Theory of Compilation Erez Petrank Lecture 4: Semantic Analysis: 1.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Cse321, Programming Languages and Compilers 1 6/23/2015 Lecture #15, March. 5, 2007 Judgments for mini-Java Multiple type environments Class Hierarchy.
Compiler Construction Semantic Analysis II Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Summary Mooly Sagiv html://
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Compiler Construction Semantic Analysis II 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.
Compiler Construction Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
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.
CSE 131 Computer Science 1 Module 1: (basics of Java)
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
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.
CS 363 Comparative Programming Languages Semantics.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
ECE122 Feb. 22, Any question on Vehicle sample code?
Abstract Syntax Trees Compiler Baojian Hua
Theory of Compilation Erez Petrank Lecture 5: Semantic Analysis: 1.
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.
CS536 Types 1. Roadmap Back from our LR Parsing Detour Name analysis – Static v dynamic – Scope Today – Type checking 2 Scanner Parser Tokens Semantic.
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.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Semantic Analysis III + Intermediate Representation I.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
Winter Compiler Construction Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
CMSC 330: Organization of Programming Languages Operational Semantics.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
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.
Arrays Using array as an expression, on the right-hand side Assigning to an array.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Compilation /15a Lecture 6
Constructing Precedence Table
Semantic Analysis with Emphasis on Name Analysis
CS 432: Compiler Construction Lecture 10
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Presentation transcript:

Semantic Analysis II

Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate: “You took too many points for that…” No resubmission Theoretical part to my box (floor 1 near the elevator) 2

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

4 Examples of type errors int a; a = true; void foo(int x) { int y; 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

5 Types Type  Set of possible values (and operations)  boolean = { true,false }  int = { }  void = {} Type safety  Type usage adheres to formally defined typing rules

6 Type judgments e : T  e is a well-typed expression of type T Examples  2 : int  2 * (3 + 4) : int  true : bool  “Hello” : string

7 Type judgments E  e : T In the context E, e is a well-typed expression of T Examples:  b:bool, x:int  b:bool  x:int  1 + x < 4:bool  foo:int->string, x:int  foo(x) : string

8 Typing rules Premise Conclusion [Name] Conclusion [Name]

9 Typing rules for expressions E  e 1 : intE  e 2 : int E  e 1 +e 2 : int [+]

10 Expression rules 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  {, >=}

11 More expression rules 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

12 Subtyping Inheritance induces subtyping relation ≤  S ≤ T  values(S)  values(T)  “A value of type S may be used wherever a value of type T is expected”

13 Subtyping For all types: For reference types: A ≤ A A extends B {…} A ≤ B A ≤ B B ≤ C A ≤ C null ≤ A

Examples 1. int ≤ int ? 2. null ≤ A ? 3. null ≤ string ? 4. string ≤ null ? 5. null ≤ boolean ? 6. null ≤ boolean[] ? 7. A[] ≤ B[] ? 14

Examples 1. int ≤ int ? 2. null ≤ A ? 3. null ≤ string ? 4. string ≤ null ? 5. null ≤ boolean ? 6. null ≤ boolean[] ? 7. A[] ≤ B[] ? “Subtyping is not covariant for array types: if A is a subtype of B then A[ ] is not a subtype of B[ ]. Instead, array subtyping is type invariant, which means that each array type is only a subtype of itself.” 15

16 Expression rules with subtyping E  e1 : T1 E  e2 : T2 T1 ≤ T2 or T2 ≤ T1 op  {==,!=} E  e1 op e2 : bool

17 Rules for method invocations E  e 0 : T 1  …  T n  T r E  e i : T ’ i T ’ i ≤ T i for all i=1..n E  e 0 (e 1, …,e n ): T r (m : static T 1  …  T n  T r )  C E  e i : T ’ i T ’ i ≤ T i for all i=1..n E  C.m(e 1, …,e n ): T r

18 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

19 Return statements ret :T r represents return type of current method ret :void  E E  return; ret :T’  E T≤T’ E  return e; E  e:T

More IC Rules Declarations Method Class Program … 20

21 Type-checking algorithm 1. Construct types 1. Add basic types to a “type table” 2. Traverse AST looking for user-defined types (classes,methods,arrays) and store in table 3. Bind all symbols to types

22 Type-checking algorithm 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

23 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 > e2 : bool E  e1 : bool E  e2 : bool E  e1 && e2 : bool E  e1 : bool E  !e1 : bool Algorithm example

24 Semantic analysis flow Parsing and AST construction  Combine library AST with IC program AST Construct and initialize global type table  Construct class hierarchy and verify the hierarchy is tree Phase 1: Symbol table construction  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