Compiler Construction Compiler Construction Semantic Analysis I.

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 Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Mooly Sagiv and Roman Manevich School of Computer Science
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
Tutorial 6 & 7 Symbol Table
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.
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Compiler Construction Semantic Analysis II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
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.
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
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 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.
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.
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.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Compiler Construction Compiler Construction Semantic Analysis I.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
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.
Quiz 7: Tuesday, Oct 20 1.(1 pt What are the main steps of your MJ Driver for PA3? 1.(2 pts) Show the JavaCup action for a given grammar rule for building.
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.
6. Semantic Analysis. Semantic Analysis Phase – Purpose: compute additional information needed for compilation that is beyond the capabilities of Context-
Bernd Fischer RW713: Compiler and Software Language Engineering.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
Lecture 5: LR Parsing CS 540 George Mason University.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
Parser Generation Tools (Yacc and Bison) CS 471 September 24, 2007.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Design issues for Object-Oriented Languages
Lecture 9 Symbol Table and Attributed Grammars
Announcements/Reading
A Simple Syntax-Directed Translator
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.
Syntax Errors; Static Semantics
Parsing #2 Leonidas Fegaras.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Parsing #2 Leonidas Fegaras.
COMPILERS Semantic Analysis
Presentation transcript:

Compiler Construction Compiler Construction Semantic Analysis I

2

CFG -> DFA Item: E ::= E +. E - indicates the rule E ::= E + E,and that we have parsed E, seen a +, and we are ready to parse another E State has many items – S ::= id. := E – S ::= id. : S If the grammar has no reduce-reduce and no shift-reduce conflicts, it is LR(0) 3

Closures E ::= E +. T – E ::= E +. T – T ::=. T * F – T ::=. T / F – T ::=. F – F ::=. num – F ::=. id 4 CFG: E ::= E + T T ::= T * F T ::= T / F T ::= F F ::= num F ::= id

Example S ::= R $ R ::= R b R ::= a 5

Example2 S ::= E $ E ::= E + T | T T ::= id | ( E ) 6 sactiongoto id()+$SET 0s5s s3s2 2aaaaa 3s5s6 4 4r2 5r4 6s5s s8s3 8r5 9r3

A grammar that is not LR(0) S ::= E $ E ::= E + T | T T ::= T * F | F F ::= id | ( E ) State I1 has a shift-reduce conflict! 7 I0: S ::=. E $ E ::=. E + T E ::=. T T ::=. T * F T ::=. F F ::=. id F ::=. ( E ) I1: E ::= T. T ::= T. * F

Another grammar that is not LR(0) S ::= X X ::= Y | id Y ::= id State I1 has a reduce-reduce conflict! 8 I0: S ::=. X $ X ::=. Y X ::=. id Y ::=. id I1: X ::= id. Y ::= id.

Can we fix it? S ::= E $ E ::= E + T | T T ::= T * F | F F ::= id | ( E ) We can see that * can’t follow E. Use the look-ahead token to resolve conflict. This is SLR(1) which is more powerful than LR(0) 9

LR(1) vs LALR(1) In LR(1),item A ::= a, s1 != A ::= a, s2 if s1 != s2. This results to a large number of states LALR(1) : – New item: A := a, s3, where s3 is the union of s1 and s2 Since we make the expected lookahead sets larger, there is a danger that some conflicts will have worse chances to be resolved. 10

LR variants LR(0) – what we’ve seen so far SLR(1) – Removes infeasible reduce actions via FOLLOW set reasoning LR(1) – LR(0) with one lookahead token in items LALR(1) – LR(1) with merging of states with same LR(0) component 11

Grammar Hierarchy 12 Non-ambiguous CFG LR(1) LALR(1) SLR(1) LL(1) LR(0)

13

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

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 – Other specific rules Guarantee partial correctness – Runtime checks pointer dereferencing array access … 15

Semantic rules: examples A variable must be declared before being used A variable should not be declared multiple times A variable should be initialized before being 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 16

Example of semantic rules Type rules are an important class of semantic rules – In an assignment, RHS and LHS must have the same type – The type of a conditional test expression must be Boolean 17

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

Scope example 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 extends Foo { void setValue(int c) { value = c; test(); } scope of local variable b scope of formal parameter c scope of c scope of local variable in statement block d scope of method test scope of field value

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

Scope tree Generally scope hierarchy forms a tree 21 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

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

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 Code block scope – Variables defined in block 23

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.” … 24

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

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 One symbol table for each scope 26

Scope nesting in IC 27 SymbolKindTypeProperties Global SymbolKindTypeProperties Class SymbolKindTypeProperties Method SymbolKindTypeProperties Block names of all classes fields and methods formals + locals variables defined in block

Symbol table example 28 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; } scope of value scope of b scope of c scope of d block1

Symbol table example 29 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; } SymbolKindTypeProperties valuefieldint… testmethod-> int setValuemethodint -> void (Foo) SymbolKindTypeProperties bvarint… (test) SymbolKindTypeProperties cvarint… (setValue) SymbolKindTypeProperties dvarint… (block1)

Checking scope rules 30 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)

Catching semantic errors 31 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

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? 32

Symbol table construction via AST traversal 33 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 34 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

What’s in an AST node 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() {…} } 35

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; … } 36

Symbol table implementation Using java.util.HashMap – HashMap keys should obey equals / hashcode contracts – Safe when key is symbol name ( String ) 37

Forward references class A { void foo() { bar(); } void bar() { … } 38 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?

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 39

Building phase 40 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 unresolved symbol Stmt setValue()

Checking phase 41 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 Stmt setValue()

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 42

Forward reference flag example class A { void foo() { bar(); } void bar() { … } 43 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