Semantic Analysis II Symbol Tables, Intro to Type Checking - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Semantic Analysis II Symbol Tables, Intro to Type Checking

Description:

tmp var char const. Symbol Tables ... x var int. f func int void. g func int int. m arg int. x var float. y var float. n arg int ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 31
Provided by: scottm3
Category:

less

Transcript and Presenter's Notes

Title: Semantic Analysis II Symbol Tables, Intro to Type Checking


1
Semantic Analysis II Symbol Tables, Intro to
Type Checking
  • EECS 483 Lecture 10
  • University of Michigan
  • Monday, October 11, 2004

2
Semantic Analysis
  • Lexically and syntactically correct programs may
    still contain other errors
  • Lexical and syntax analyses are not powerful
    enough to ensure the correct usage of variables,
    objects, functions, ...
  • Semantic analysis Ensure that the program
    satisfies a set of rules regarding the usage of
    programming constructs (variables, objects,
    expressions, statements)

3
Class Problem
Classify each error as lexical, syntax, semantic,
or correct.
int foo(int a) foo 3
int a a 1 a 2
int a a 1.0
1int x x 2
int foo(int a) a 3
int a b b a
in a a 1
4
Categories of Semantic Analysis
  • Examples of semantic rules
  • Variables must be defined before being used
  • A variable should not be defined multiple times
  • In an assignment stmt, the variable and the
    expression must have the same type
  • The test expr. of an if statement must have
    boolean type
  • 2 major categories
  • Semantic rules regarding types
  • Semantic rules regarding scopes

5
Type Information/Checking
  • Two main categories of semantic analysis
  • Type information
  • Scope information
  • Type Information Describes what kind of values
    correspond to different constructs variables,
    statements, expressions, functions, etc.
  • variables int a integer
  • expressions (a1) 2 boolean
  • statements a 1.0 floating-point
  • functions int pow(int n, int m) int int,int
  • Type Checking Set of rules which ensures the
    type consistency of different constructs in the
    program

6
Scope Information
  • Characterizes the declaration of identifiers and
    the portions of the program where it is allowed
    to use each identifier
  • Example identifiers variables, functions,
    objects, labels
  • Lexical scope textual region in the program
  • Examples Statement block, formal argument list,
    object body, function or method body, source
    file, whole program
  • Scope of an identifier The lexical scope its
    declaration refers to

7
Variable Scope
  • Scope of variables in statement blocks
  • Scope of global variables current file
  • Scope of external variables whole program

int a ... int b ... ....
scope of variable a
scope of variable b
8
Function Parameter and Label Scope
  • Scope of formal arguments of functions
  • Scope of labels

int foo(int n) ...
scope of argument n
void foo() ... goto lab ... lab
i ... goto lab ...
scope of label lab, Note in Ansi-C all labels
have function scope regardless of where they are
9
Scope in Class Declaration
  • Scope of object fields and methods

class A public void f() x1 ...
private int x ...
scope of variable x and method f
10
Semantic Rules for Scopes
  • Main rules regarding scopes
  • Rule 1 Use each identifier only within its scope
  • Rule 2 Do not declare identifier of the same
    kind with identical names more than once in the
    same lexical scope

int X(int X) int X goto X int
X X X 1
class X int X void X(int X) X
... goto X
Are these legal? If not, identify the illegal
portion.
11
Symbol Tables
  • Semantic checks refer to properties of
    identifiers in the program their scope or type
  • Need an environment to store the information
    about identifiers symbol table
  • Each entry in the symbol table contains
  • Name of an identifier
  • Additional info about identifier kind, type,
    constant?

NAME KIND TYPE ATTRIBUTES foo func int,int ?
int extern m arg int n arg int const tmp var
char const
12
Scope Information
  • How to capture the scope information in the
    symbol table?
  • Idea
  • There is a hierarchy of scopes in the program
  • Use similar hierarchy of symbol tables
  • One symbol table for each scope
  • Each symbol table contains the symbols declared
    in that lexical scope

13
Example
Global symtab
int x void f(int m) float x, y ...
int i, j .... int x l ... int
g(int n) char t ...
x var int f func int ? void g func int ? int
func g symtab
func f symtab
m arg int x var float y var float
n arg int t var char
i var int j var int
x var int l label
14
Identifiers with Same Name
  • The hierarchical structure of symbol tables
    automatically solves the problem of resolving
    name collisions
  • E.g., identifiers with the same name and
    overlapping scopes
  • To find which is the declaration of an identifier
    that is active at a program point
  • Start from the current scope
  • Go up the hierarchy until you find an identifier
    with the same name

15
Class Problem
Associate each definition of x with its
appropriate symbol table entry
Global symtab
x var int f func int ? void g func int ? int
int x void f(int m) float x, y ...
int i, j x1 int x l x2 int
g(int n) char t x3
m arg int x var float y var float
n arg int t var char
i var int j var int
x var int l label
16
Catching Semantic Errors
Error!
Global symtab
int x void f(int m) float x, y ...
int i, j x1 int x l i2 int
g(int n) char t x3
undefined variable
x var int f func int ? void g func int ? int
m arg int x var float y var float
n arg int t var char
i var int j var int
x var int l label
i2
17
Symbol Table Operations
  • Two operations
  • To build symbol tables, we need to insert new
    identifiers in the table
  • In the subsequent stages of the compiler we need
    to access the information from the table use
    lookup function
  • Cannot build symbol tables during lexical
    analysis
  • Hierarchy of scopes encoded in syntax
  • Build the symbol tables
  • While parsing, using the semantic actions
  • After the AST is constructed

18
List Implementation
  • Simple implementation using a list
  • One cell per entry in the table
  • Can grow dynamically during compilation
  • Disadvantage inefficient for large tables
  • Need to scan half the list on average

. foo func int,int ? int
. m var int
. n var int
. tmp var char
19
Hash Table Implementation
  • Efficient implementation using hash table
  • Array of lists (buckets)
  • Use a hash on symbol name to map to corresponding
    bucket
  • Hash func identifier name (string) ? int
  • Note include identifier type in match function

20
Forward References
  • Use of an identifier within the scope of its
    declaration, but before it is declared
  • Any compiler phase that uses the information from
    the symbol table must be performed after the
    table is constructed
  • Cannot type-check and build symbol table at the
    same time
  • Example

class A int m() return n() int n()
return 1
21
Back to Type Checking
  • What are types?
  • They describe the values computed during the
    execution of the program
  • Essentially they are a predicate on values
  • E.g., int x in C means 231 lt x lt 231
  • Type Errors improper or inconsistent operations
    during program execution
  • Type-safety absence of type errors

22
How to Ensure Type-Safety
  • Bind (assign) types, then check types
  • Type binding defines type of constructs in the
    program (e.g., variables, functions)
  • Can be either explicit (int x) or implicit (x1)
  • Type consistency (safety) correctness with
    respect to the type bindings
  • Type checking determine if the program correctly
    uses the type bindings
  • Consists of a set of type-checking rules

23
Type Checking
  • Semantic checks to enforce the type safety of the
    program
  • Examples
  • Unary and binary operators (e.g. , , ) must
    receive operands of the proper type
  • Functions must be invoked with the right number
    and type of arguments
  • Return statements must agree with the return type
  • In assignments, assigned value must be compatible
    with type of variable on LHS
  • Class members accessed appropriately

24
4 Concepts Related to Types/Languages
  • Static vs dynamic checking
  • When to check types
  • Static vs dynamic typing
  • When to define types
  • Strong vs weak typing
  • How many type errors
  • Sound type systems
  • Statically catch all type errors

25
Static vs Dynamic Checking
  • Static type checking
  • Perform at compile time
  • Dynamic type checking
  • Perform at run time (as the program executes)
  • Examples of dynamic checking
  • Array bounds checking
  • Null pointer dereferences

26
Static vs Dynamic Typing
  • Static and dynamic typing refer to type
    definitions (i.e., bindings of types to
    variables, expressions, etc.)
  • Static typed language
  • Types defined at compile-time and do not change
    during the execution of the program
  • C, C, Java, Pascal
  • Dynamically typed language
  • Types defined at run-time, as program executes
  • Lisp, Smalltalk

27
Strong vs Weak Typing
  • Refer to how much type consistency is enforced
  • Strongly typed languages
  • Guarantee accepted programs are type-safe
  • Weakly typed languages
  • Allow programs which contain type errors
  • These concepts refer to run-time
  • Can achieve strong typing using either static or
    dynamic typing

28
Soundness
  • Sound type systems can statically ensure that
    the program is type-safe
  • Soundness implies strong typing
  • Static type safety requires a conservative
    approximation of the values that may occur during
    all possible executions
  • May reject type-safe programs
  • Need to be expressive reject as few type-safe
    programs as possible

29
Class Problem
Classify the following languages C, C, Pascal,
Java, Scheme ML, Postscript, Modula-3, Smalltalk,
assembly code
Strong Typing
Weak Typing
Static Typing
Dynamic Typing
30
Why Static Checking?
  • Efficient code
  • Dynamic checks slow down the program
  • Guarantees that all executions will be safe
  • Dynamic checking gives safety guarantees only for
    some execution of the program
  • But is conservative for sound systems
  • Needs to be expressive reject few type-safe
    programs
Write a Comment
User Comments (0)
About PowerShow.com