36
Lecture 9. Semantic Analysis – Scoping and Symbol Table Wei Le 2015.10

Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

  • Upload
    dophuc

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Lecture 9. Semantic Analysis – Scoping andSymbol Table

Wei Le

2015.10

Page 2: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Outline

I Semantic analysis

I Scoping

I The Role of Symbol Table

I Implementing a Symbol Table

Page 3: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Semantic Analysis

I Parser builds abstract syntax tree

I Now need to extract semantic information and check constraints

I Can sometimes be done during the parse, but often easier toorganize as a separate pass

I Some things cannot be done on the fly during the parse, e.g.,information about identifiers that are used before they are declared(fields, classes)

I Last phase in front end

Page 4: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Semantic Analysis

I Extract types and other information from the program

I Check language rules that go beyond the grammar

I Assign storage locations

I Key data structures: symbol tables – For each identifier in theprogram, record its attributes (kind, type, etc.)

I Find more errors

Page 5: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Semantic Analysis: Detecting Errors

I Lexical analysis: detecting illegal tokens (e.g., string constant toolong, not legal characters in the string)

I Syntax analysis: structure errors, no parse tree for the string givengrammar (brackets not match)

I Semantic analysis (dependent on the language): e.g., specify wrongtypes (int a = b+ c (c cannot be string)), buffer overflows, divide byzero

I scoping: e.g., all variables must be declaredI typing: certain operations only can be performed on the certain

types of variables; e.g., actual parameter types have to match formalparameter types

Page 6: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Examples of Semantic errors

I The type of the right-side expression of an assignment statementshould match the type of the left-side, and the left-side needs to bea properly declared and assignable identifier (i.e. not some sort ofconstant).

I The parameters of a function should match the arguments of afunction call in both number and type.

I The language may require that identifiers are unique, disallowing aglobal variable and function of the same name.

I The operands to multiplication operation will need to be of numerictype, perhaps even the exact same type depending on the strictnessof the language.

Page 7: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Cool

Page 8: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Scope

I Match identifiers’ declaration with uses

I Visibility of an entity

I Binding between declaration and uses

I The scope of an identifier is the portion of a program in which thatidentifier is accessible

I The same identifier may refer to different things in different parts ofthe program: Different scopes for same name dont overlap

I An identifier may have restricted scope

Page 9: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Static vs Dynamic Scope

I Most languages have static scope: Scope depends only on theprogram text, not runtime behavior (e.g., COO, Java and C): mostclosely nested rule

I A few languages are dynamically scoped: Scope depends onexecution of the program (e.g., Lisp, SNOBOL, Perl allow dynamicscope through certain keywords) – context specific scoping

I Dynamic scoping means that when a symbol is referenced, thecompiler/interpreter will walk up the symbol-table stack to find thecorrect instance of the variable to use.

Page 10: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Static Scope

Page 11: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Static Scope

Page 12: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10
Page 13: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Scoping and Symbol Table

I Given a declaration of a name, is there already a declaration of thesame name in the current scope, i.e., is it multiply declared?

I Given a use of a name, to which declaration does it correspond(using the ”most closely nested” rule), or is it undeclared?

I Symbol table: a data structure that tracks the current bindings ofidentifiers (for performing semantic checks and generating codeefficiently)

I large part of semantic analysis consists of trackingvariable/function/type declarations .As we enter each new identifierin our symbol table, we need to record the type information of thedeclaration.

Page 14: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

The Roles of Symbol Table

I Generally, symbol table is only needed to answer those two questions,i.e., once all declarations have been processed to build the symboltable, and all uses have been processed to link each ID node in theabstract-syntax tree with the corresponding symbol-table entry, thenthe symbol table itself is no longer needed, because no more lookupsbased on name will be performed (In practice, symbol table oftenkeeps through the entire compilation and sometimes even beyond)

I The process of building symbol table, you are able to check thescoping rules

Page 15: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Implementing Symbol Table: Symbol Table Entry

I Semantic of a variable: 1) When is used; 2) The effective contextwhere a name is valid; 3) Where it is stored: storage address

Page 16: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Implementing Symbol Table: Key Operations

High level

I enter scope

I process a declaration

I process a use

I exit scope

low level

Page 17: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Implementing Symbol Table: Data Structures

Page 18: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

A List of Hash Table

I One hash table for each currently visible scope

I At a given point, have symbol table accessible for each nesting level≤ current nesting level

Page 19: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

An Example

Page 20: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

A List of Hash Table: Operations

Page 21: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

A List of Hash Table: Operations

Page 22: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Insert Method Name

Page 23: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Runtime for Each Operation

Page 24: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Scoping Example

Page 25: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Scoping Example

Page 26: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Single Hash Table to Implement Symbol Table

I Link together different entries for the same identifier and associatenesting level with each occurrence of same name.

I The first one is the latest occurrence of the name, i.e., highestnesting level

Page 27: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Single Hash Table to Implement Symbol Table

I During exit from a procedure - delete all entries of the nesting levelwe are exiting

Page 28: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

An Example

Page 29: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

A List of Operations

Page 30: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

A List of Operations

Page 31: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Scope Rules for Cool

Beyond most closely nested rule:

I Class definition: globally visible, a class can be used before it isdefined

An Example:

Page 32: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Scope Rules for Cool

I Class names can be used before being definedI We can’t check this property using a symbol tableI Or even in one pass

I Solution:I Pass 1: Gather all class namesI Pass 2: Do the checking

I Semantic analysis requires multiple passes: probably more than two

Page 33: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Scope Rule for Cool

Attribute names are global within the class in which they are definedAn example:

Page 34: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Scope Rule for Cool

I Method and attribute names have complex rules

I A method need not be defined in the class in which it is used, but insome parent class

I Methods may also be redefined (overridden)

Page 35: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Implementing a Symbol Table for Object-OrientedPrograms

I Single global table to map class names to class symbol tablesI Created in pass over class definitionsI Used in remaining parts of compiler to check field/method names

and extract information

I All global tables persist throughout the compilation (and beyond ina real Java or C compiler...)

I One or multiple symbol tables for each class: 1 entry for eachmethod/field; Contents: type information, public/private, storagelocations (later)

I Local symbol table for each method 1 entry for each local variable orparameter Contents: type information, storage locations (later)

I the scope for a derived class is often placed inside of the scope of abase class

Page 36: Lecture 9. Semantic Analysis Scoping and Symbol Tableweb.cs.iastate.edu/~weile/cs440.540/5.SemanticAnalysis.scope.pdf · Semantic Analysis { Scoping and Symbol Table Wei Le 2015.10

Summary

I Concepts: Scope (static and dynamic scope), Symbol Table

I Basic scoping rules (different programming languages can implementdifferent rules):

I variables have to be declared before useI variables should be declared twiceI most closely nested rule: the use of a variable always binds to the

declaration that is in the closet scope

I Implementation of a symbol tableI Symbol table entry: type, storage type, scope informationI Data structureI OperationsI ComplexityI Comparisons of a set of algorithms in terms tradeoffs