CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Symbol Table.
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Compiler Construction
The Symbol Table Lecture 13 Wed, Feb 23, The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold.
1 Problem 2 A Scanner / Parser for Simple C. 2 Outline l Language syntax for SC l Requirements for the scanner l Requirement for the parser l companion.
Compiler Summary Mooly Sagiv html://
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
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.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
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 CS 671 February 5, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens –e.g.: main$
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Compiler Construction Compiler Construction Semantic Analysis I.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
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.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Chapter 1 Introduction Major Data Structures in Compiler
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
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.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
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.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Language Implementation Overview John Keyser Spring 2016.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Lecture 9 Symbol Table and Attributed Grammars
A Simple Syntax-Directed Translator
Constructing Precedence Table
Semantic Analysis with Emphasis on Name Analysis
Semantic Analysis Chapter 6.
Basic Program Analysis: AST
Syntax Errors; Static Semantics
CSE 3302 Programming Languages
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Compiler design.
Compiler Construction
COMPILERS Semantic Analysis
Compiler Construction
Presentation transcript:

CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02

CS 412/413 Spring 2002 Introduction to Compilers2 Where We Are Source code (character stream) Lexical Analysis Syntax Analysis (Parsing) Token stream Abstract syntax tree (AST) Semantic Analysis if (b == 0) a = b; if(b)a=b;0== if == b0 = a b

CS 412/413 Spring 2002 Introduction to Compilers3 Incorrect Programs Lexically and syntactically correct programs may still contain other errors! Lexical and syntax analysis are not powerful enough to ensure the correct usage of variables, objects, functions, statements, etc. Example: lexical analysis does not distinguish between different variable or function identifiers (it returns the same token for all identifiers)int a; a = 1;b = 1;

CS 412/413 Spring 2002 Introduction to Compilers4 Incorrect Programs Example 2: syntax analysis does not correlate the declarations with the uses of variables in the program: int a; a = 1; Example 3: syntax analysis does not correlate the types from the declarations with the uses of variables:int a; a = 1;a = 1.0;

CS 412/413 Spring 2002 Introduction to Compilers5 Goals of Semantic Analysis Semantic analysis = ensure that the program satisfies a set of rules regarding the usage of programming constructs (variables, objects, expressions, statements) Examples of semantic rules: –Variables must be defined before being used –A variable should not be defined multiple times –In an assignment statement, the variable and the assigned expression must have the same type –The test expr. of an if statement must have boolean type Two main categories: –Semantic rules regarding types –Semantic rules regarding scopes

CS 412/413 Spring 2002 Introduction to Compilers6 Type Information Type information = describes what kind of values correspond to different constructs: variables, statements, expressions, functions variables: int a;integer expressions: (a+1) == 2 boolean statements: a = 1.0floating-point functions: int pow(int n, int m)int x int  int

CS 412/413 Spring 2002 Introduction to Compilers7 Type Checking Type checking = set of rules which ensures the type consistency of different constructs in the program Examples: –The type of a variable must match the type from its declaration –The operands of arithmetic expressions (+, *, -, /) must have integer types; the result has integer type –The operands of comparison expressions (==, !=) must have integer or string types; the result has boolean type

CS 412/413 Spring 2002 Introduction to Compilers8 Type Checking More examples: –For each assignment statement, the type of the updated variable must match the type of the expression being assigned –For each call statement foo(v 1, …, v n ), the type of each actual argument v i must match the type of the corresponding formal argument f i from the declaration of function foo –The type of the return value must match the return type from the declaration of the function Type checking: next two lectures.

CS 412/413 Spring 2002 Introduction to Compilers9 Scope Information 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 –Statement block –Formal argument list –Object body –Function or method body –Module body –Whole program (multiple modules) Scope of an identifier: the lexical scope its declaration refers to

CS 412/413 Spring 2002 Introduction to Compilers10 Scope Information Scope of variables in statement blocks: { int a; … { int b; } … } Scope of global variables: current module Scope of external variables: whole program scope of variable b scope of variable a

CS 412/413 Spring 2002 Introduction to Compilers11 Scope of formal arguments of functions: Scope of labels: Scope Information int factorial(int n) { … } scope of argument n void f() { … goto l; … l: a =1; … goto l; … } scope of label l

CS 412/413 Spring 2002 Introduction to Compilers12 Scope Information class A { private int x; public void g() { x=1; } … } class B extends A { … public int h() { g(); } … } scope of method f scope of field x Scope of object fields and methods:

CS 412/413 Spring 2002 Introduction to Compilers13 Semantic Rules for Scopes Main rules regarding scopes: Rule 1: Use each identifier only within its scope Rule 2: Do not declare identifiers of the same kind with identical names more than once in the same lexical scope Can declare identifiers with the same name with identical or overlapping lexical scopes if they are of different kinds class X { int X; void X(int X) { X: for(;;) break X; } int X(int X) { int X; goto X; { int X; X: X = 1; } } Not Recommended!

CS 412/413 Spring 2002 Introduction to Compilers14 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 –the name of an identifier –additional information: its kind, its type, if it is constant, … NAMEKINDTYPEATTRIBUTES foofunc int x int  bool extern margint nargintconst tmpvarboolconst

CS 412/413 Spring 2002 Introduction to Compilers15 Scope Information How to capture the scope information in the symbol table? Idea: There is a hierarchy of scopes in the program Use a similar hierarchy of symbol tables One symbol table for each scope Each symbol table contains the symbols declared in that lexical scope

CS 412/413 Spring 2002 Introduction to Compilers16 Example xvarint ffunc int  void gfunc int  int margint xvarfloat yvarfloat ivarint jvarint xvarint llab nvarint tvarbool Global symtab func f symtab func g symtab int x; void f(int m) { float x, y; … { int i, j; …; } { int x; l: …; } } int g(int n) { bool t; …; }

CS 412/413 Spring 2002 Introduction to Compilers17 Identifiers With Same Name The hierarchical structure of symbol tables automatically solves the problem of resolving name collisions (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 in the hierarchy until you find an identifier with the same name

CS 412/413 Spring 2002 Introduction to Compilers18 Example xvarint ffunc int  void gfunc int  int margint xvarfloat yvarfloat ivarint jvarint xvarint llab nvarint tvarbool Global symtab x = 3 x = 1 x = 2 int x; void f(int m) { float x, y; … { int i, j; x = 1; } { int x; l: x = 2; } } int g(int n) { bool t; x = 3; }

CS 412/413 Spring 2002 Introduction to Compilers19 Catching Semantic Errors xvarint ffunc int  void gfunc int  int margint xvarfloat yvarfloat ivarint jvarint xvarint llab nvarint tvarbool x = 3 x = 1 i = 2 Error! int x; void f(int m) { float x, y; … { int i, j; x = 1; } { int x; l: i = 2; } } int g(int n) { bool t; x = 3; }

CS 412/413 Spring 2002 Introduction to Compilers20 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 a lookup function Cannot build symbol tables during lexical analysis hierarchy of scopes encoded in the syntax Build the symbol tables: while parsing, using the semantic actions After the AST is constructed

CS 412/413 Spring 2002 Introduction to Compilers21 List Implementation Simple implementation = list One cell per entry in the table Can grow dynamically during compilation Disadvantage: inefficient for large symbol tables need to scan half the list on average foo func int x int  bool m var int n var int tmp Var bool

CS 412/413 Spring 2002 Introduction to Compilers22 Hash Table Implementation Efficient implementation = hash table It is an array of lists (buckets) Uses a hashing function to map the symbol name to the corresponding bucket: hashfunc : string  int Good hash function = even distribution in the buckets hashfunc(“m”) = 0, hashfunc(“foo”) = 3 m var int tmp var bool n var int foo func …

CS 412/413 Spring 2002 Introduction to Compilers23 Forward References Forward references = use 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; } }

CS 412/413 Spring 2002 Introduction to Compilers24 Summary Semantic checks ensure the correct usage of variables, objects, expressions, statements, functions, and labels in the program Scope semantic checks ensure that identifiers are correctly used within the scope of their declaration Type semantic checks ensures the type consistency of various constructs in the program Symbol tables: a data structure for storing information about symbols in the program Used in semantic analysis and subsequent compiler stages Next time: type-checking