Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.

Slides:



Advertisements
Similar presentations
Winter Compiler Construction T6 – semantic analysis part I scopes and symbol tables Mooly Sagiv and Roman Manevich School of Computer Science.
Advertisements

CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Symbol Table.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
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 Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Tutorial 6 & 7 Symbol Table
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Compiler Construction Semantic Analysis II Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Compiler Construction Semantic Analysis II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Chapter 2 A Simple Compiler
Compiler Construction Semantic Analysis I 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.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
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$
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
7-1 7 Contextual analysis  Aspects of contextual analysis  Scope checking  Type checking  Case study: Fun contextual analyser  Representing types.
Winter Compiler Construction T8 – semantic analysis recap + IR part 1 Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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
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.
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:
Bernd Fischer RW713: Compiler and Software Language Engineering.
Semantic Analysis III + Intermediate Representation I.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
6. Semantic Analysis. Semantic Analysis Phase – Purpose: compute additional information needed for compilation that is beyond the capabilities of Context-
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
Design issues for Object-Oriented Languages
Lecture 9 Symbol Table and Attributed Grammars
Constructing Precedence Table
Java Primer 1: Types, Classes and Operators
Semantic Analysis with Emphasis on Name Analysis
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Syntax Errors; Static Semantics
Java Programming Language
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Mooly Sagiv and Roman Manevich School of Computer Science
CS 432: Compiler Construction Lecture 11
Presentation transcript:

Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2 TA1 Parsing Submission is not in groups Deadline January 5 th 2009 Submission either in class or to פז גרימברג box

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

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 Goals of semantic analysis Check “correct” use of programming constructs Context-sensitive Beyond context free grammars Deeper analysis than lexical and syntax analysis Semantic rules for checking correctness Scope rules Type-checking rules Specific rules Guarantee partial correctness only Runtime checks pointer dereferencing array access

6 Example of semantic rules A variable must be declared before used A variable should not be declared multiple times A variable should be initialized before used Non-void method should contain return statement along all execution paths break / continue statements allowed only in loops this keyword cannot be used in static method main method should have specific signature …

7 Example of semantic rules Type rules are important class of semantic rules In an assignment RHS and LHS must have the same type In a condition test expression must have boolean type

8 Scope Scope of identifier portion of program where identifier can be referred to Lexical scope Statement block Method body Class body Module / package / file Whole program (multiple modules)

9 Scope example class Foo { int value; int test() { int b = 3; return value + b; } void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } class Bar extends Foo { int value; void setValue(int c) { value = c; test(); } scope of field value scope of local variable b scope of formal parameter c scope of c scope of value scope of local variable in statement block d scope of method test

10 Scope nesting Scopes may be enclosed in other scopes void foo(){ int a; … { int a; } } same name but different symbol

11 Scope tree Generally scope hierarchy forms a tree class Foo { int value; int test() { int b = 3; return value + b; } void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } Foo value test b setValue c block I d block I

12 Subclasses Scope of subclass enclosed in scope of its superclass Subtype relation must be acyclic Class Foo { int a; } Class Bar extends Foo { } Bar sees “a” as well

13 Scope hierarchy in IC Global scope The names of all classes defined in the program Class scope Instance scope: all fields and methods of the class Static scope: all static methods Scope of subclass nested in scope of its superclass Method scope Formal parameters and local variables Variables defined in block Code block scope Variables defined in block

14 Scope rules in IC “When resolving an identifier at a certain point in the program, the enclosing scopes are searched for that identifier.” “local variables and method parameters can only be used after they are defined in one of the enclosing block or method scopes.” “Fields and virtual methods can be used in expressions of the form e.f or e.m() when e has class type C and the instance scope of C contains those fields and methods.” “ static methods can be used in expressions of the form C.m() if the static scope of C contains m.” … (Section 10 in IC specification)

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

16 Symbol table Each entry in symbol table contains name of an identifier kind (variable/method/field…) Type (int, string, myClass…) Additional properties, e.g., final, public (not needed for IC) One symbol table for each scope

17 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 Scope nesting mirrored in hierarchy of symbol tables

18 class Foo { int value; int test() { int b = 3; return value + b; } void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } class Bar { int value; void setValue(int c) { value = c; } scope of value scope of b scope of c scope of value scope of d Symbol table example block1

19 class Foo { int value; int test() { int b = 3; return value + b; } void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } class Bar { int value; void setValue(int c) { value = c; } Symbol table example SymbolKindTypeProperties valuefieldint… testmethod-> int setValuemethodint -> void (Foo) SymbolKindTypeProperties bvarint… (test) SymbolKindTypeProperties cvarint… (setValue) SymbolKindTypeProperties dvarint… (block1)

20 SymbolKindTypeProperties valuefieldint… testmethod-> int setValuemethodint -> void SymbolKindTypeProperties bvarint… SymbolKindTypeProperties cvarint… SymbolKindTypeProperties dvarint… (Foo) (test) … Symbol table example cont. (setValue) (block1)

21 Checking scope rules SymbolKindTypeProperties valuefieldint… testmethod-> int setValuemethodint -> void SymbolKindTypeProperties bvarint… SymbolKindTypeProperties cvarint… SymbolKindTypeProperties dvarint… (Foo) (test)(setValue) (block1) void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } lookup(value)

22 SymbolKindTypeProperties valuefieldint… testmethod-> int setValuemethodint -> void SymbolKindTypeProperties bvarint… SymbolKindTypeProperties cvarint… SymbolKindTypeProperties dvarint… (Foo) (test)(setValue) (block1) void setValue(int c) { value = c; { int d = c; c = c + d; myValue = c; } lookup(myValue) Error ! undefined symbol Catching semantic errors

23 Symbol table operations insert Insert new symbol (to current scope) lookup Try to find a symbol in the table May cause lookup in parent tables Report an error when symbol not found How do we check illegal re-definitions?

24 class MethodDecl Stmt MethodDecl ClassDecl root name=Foo name=setValue name=test VarDecl id=b StmtBlock Stmt VarDecl id=d Symbolkind globals Symbolkind Foo Symbol test Symbol setValue Foo testmethod setValuemethod bvar c Symbol block1 dvar Symbol table construction via AST traversal

25 class MethodDecl Stmt MethodDecl ClassDecl root name=Foo name=setValue name=test VarDecl id=b StmtBlock Stmt VarDecl id=d Symbolkind globals Symbolkind Foo Symbol test Symbol setValue Foo testmethod setValuemethod bvar c Symbol block1 dvar Linking AST nodes to enclosing table

26 public abstract class ASTNode { /** line in source program **/ private int line; /** reference to symbol table of enclosing scope **/ private SymbolTable enclosingScope; /** accept visitor **/ public abstract void accept(Visitor v); /** accept propagating visitor **/ public abstract U accept(PropagatingVisitor v,D context); /** return line number of this AST node in program **/ public int getLine() {…} /** returns symbol table of enclosing scope **/ public SymbolTable enclosingScope() {…} } What’s in an AST node

27 Symbol table implementation Each table could be implemented using java.util.HashMap Implement a hierarchy of symbol tables Can implement a Symbol class HashMap keys should obey equals / hashcode contracts Safe when key is symbol name ( String )

28 Symbol table implementation public class SymbolTable { /** map from String to Symbol **/ private Map entries; private String id; private SymbolTable parentSymbolTable; public SymbolTable(String id) { this.id = id; entries = new HashMap (); } … } public class Symbol { private String id; private Type type; private Kind kind; … }

29 Implementing table structure Hierarchy of symbol tables Pointer to enclosing table Can also keep list of sub-tables Symbol table key should include id and kind Can implement using 2-level maps (kind->id->entry) Separating table in advance according to kinds also acceptable

30 Implementation option 1 public class SymbolTable { /** Map kind->(id->entry) Kind enum->(String->Symbol) **/ private Map > entries; private SymbolTable parent; … public Symbol getMethod(String id) { Map methodEntries = entries.get(METHOD_KIND); return methodEntries.get(id); } public void insertMethod(String id, Type t) { Map methodEntries = entries.get(METHOD_KIND); if (methodEntries == null) { methodEntries = new HashMap (); entries.put(METHOD_KIND, methodEntries); } methodEntries.put(id,new Symbol(id, t)); } … }

31 Implementation option 2 public class SymbolTable { /** Method Map id->entry **/ private Map methodEntries; … private Map variableEntries; … private SymbolTable parent; public Symbol getMethod(String id, Type t) { return methodEntries.get(id); } public void insertMethod(String id, Type t) { methodEntries.put(new Symbol(id,METHOD_KIND,t); } …

32 Forward references class A { void foo() { bar(); } void bar() { … } Program root ClassDecl id=A MethodDecl id=foo retType=void MethodDecl id=bar retType=void Call id=bar() class Symbolkind globals Symbolkind A A foomethod barmethod Undefined identifier bar() bar used before encountered declaration How do we handle forward references? We will see two solutions

33 Solution 1 – multiple phases Building visitor A propagating visitor Propagates reference to the symbol table of the current scope Checking visitor On visit to node perform check using symbol tables Resolve identifiers Look for symbol in table hierarchy

34 class MethodDecl ClassDecl root name=Foo name=setValue name=test VarDecl id=b StmtBlock Stmt VarDecl id=d Symbolkind globals Symbolkind Foo SymbolKind test Symbolkind setValue Foo testmethod setValuemethod bvar c Symbolkind block1 dvar Building phase unresolved symbol Stmt setValue()

35 class MethodDecl ClassDecl root name=Foo name=setValue name=test VarDecl id=b StmtBlock Stmt VarDecl id=d Symbolkind globals Symbolkind Foo SymbolKind test Symbolkind setValue Foo testmethod setValuemethod bvar c Symbolkind block1 dvar Checking phase Stmt setValue()

36 Forward references – solution 2 Use forward reference marker Update symbol table when symbol defined Remove forward-reference marker Count unresolved symbols Upon exit check that #unresolved=0

37 Forward reference flag example class A { void foo() { bar(); } void bar() { … } Program root ClassDecl id=A MethodDecl id=foo retType=void MethodDecl id=bar retType=void Call id=bar() class Symbolkind globals SymbolkindFREF A A foomethod barmethodtrue

38 Class hierarchy extends relation should be acyclic Can check acyclicity in separate phase Build symbol tables for classes first and check absence of cycles

39 Next phase: type checking First, record all pre-defined types ( string, int, boolean, void, null ) Second, record all user-defined types (classes, methods, arrays) Recording done by storing in type table Now, run type-checking algorithm

40 Type table Keeps a single copy for each type Can compare types for equality by == Records primitive types: int, bool, string, void, null Initialize table with primitive types User-defined types: arrays, methods, classes Used to record inheritance relation Types should support subtypeOf(Type t1, Type t2) For IC enough to keep one global table Static field of some class (e.g., Type ) In C/Java associate type table with scope

41 Possible type hierarchy Type IntType RefType BoolType VoidType NullTypeStringTypeClassType ICClass : c ArrayType Type : elemType RefType MethodType type ::= int | boolean | … | type `[` `]`