CPSC 388 – Compiler Design and Construction

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

Semantic Analysis and Symbol Tables
Symbol Table.
Semantics Static semantics Dynamic semantics attribute grammars
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.
1 Compiler Construction Intermediate Code Generation.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Tutorial 6 & 7 Symbol Table
Environments and Evaluation
Compiler Summary Mooly Sagiv html://
Overview of Compiler Design CIS 631, CSE 691, CIS400, CSE 400 Compiler Design Dr. Nancy McCracken January 15, 2008.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
COP4020 Programming Languages
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
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.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
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.
1 Week 7 Questions / Concerns What’s due: Lab3 next Monday 5/19 Coming up: Lab2 & Lab3 check-off next week Lab3: LL(1) Bottom-Up Parsers LR(1) parsers.
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Introduction to Code Generation and Intermediate Representations
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Chapter 1 Introduction Major Data Structures in Compiler
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Semantic analysis Jakub Yaghob
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Construction (CS-636)
Semantic Analysis with Emphasis on Name Analysis
Compiler design Introduction to code generation
Overview of Compilation The Compiler BACK End
CS 536 / Fall 2017 Introduction to programming languages and compilers
CPSC 388 – Compiler Design and Construction
Syntax Errors; Static Semantics
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Overview of Compilation The Compiler BACK End
Compilers B V Sai Aravind (11CS10008).
Compiler design.
CS 432: Compiler Construction Lecture 11
Corresponds with Chapter 5
Presentation transcript:

CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking

Compiler Design Lexical Analyzer Syntax Analyzer (Scanner) (Parser) Symantic Analyzer Intermediate Code Generator Symbol Table Optimizer Code Generator

Introduction Parser ensures proper syntax but cannot (or does not) check for: A variable should not be declared more than once in the same scope. A variable should not be used before being declared. The type of the left-hand side of an assignment should match the type of the right-hand side. Methods should be called with the right number and types of arguments.

Static Semantic Analyzer Traverses the Abstract Syntax Tree: For each scope in the program: Process the declarations, adding new entries to the symbol table and reporting any variables that are multiply declared; process the statements, finding uses of undeclared variables, and updating the "ID" nodes of the abstract-syntax tree to point to the appropriate symbol-table entry. Process all of the statements in the program again, using the symbol-table information to determine the type of each expression, and finding type errors.

Symbol Table Holds names declared in a program: Classes Fields Methods Variables Each name in Symbol Table has attributes: Kind of name Type Nesting level Location at runtime

Scoping Rules Say when a program is allowed to reuse a name Match use of a name with the corresponding declaration

Reusing Names in Java? class Test { int Test; void Test( ) { … } also reuse names for more than one method as long as the number and/or types of parameters are unique

Reusing Names in C/C++ void f(int k) { int x = 0; /* x is declared here */ while (...) { int x = 1; /* another x is declared */ ... if (...) { float x = 5.5; /*another x */ }

Matching Uses to Declarations Static Scoping Determination of match made at compile time Dynamic Scoping Determination of match made at runtime Java, C, and C++ use Static Scoping

C and C++ Scoping Rules “Most Closely Nested” rule Scoping Levels A use of variable x matches the declaration in the most closely enclosing scope such that the declaration precedes the use. Scoping Levels One outermost scope includes function names and global variables Each Function has one (or more) scopes One scope for parameters and “top-level” declarations One scope for each block in the function C++ has a scope for each for loop, i.e. you can declare variables in for-loop header

You Try It (What reuses are allowed in Java?) class animal { // methods void attack(int animal) { for (int animal=0; animal<10; animal++) { int attack; } int attack(int x) { for (int attack=0; attack<10; attack++) { int animal; void animal() { } // fields double attack;

You Try It Match uses to declarations (in C++) int k=10, x=20; void foo(int k) { int a = x; int x = k; int b = x; while (...) { int x; if (x == k) { int k, y; k = y = x; } int x = y;

Dynamic Scoping A use of a variable that has no corresponding declaration in the same function corresponds to the declaration in the most-recently-called still active function.

Example Dynamic Scoping void main() { f1(); f2(); } void f1() { int x = 10; g(); void f2() { String x = "hello"; f3(); void f3() { double x = 30.5; void g() { print(x); Output: “10 hello”

You Try It: What is Output? void main() { int x = 0; f1(); g(); f2(); } void f1() { int x = 10; void f2() { int x = 20; void g() { print(x);

Using Names Before Defining (in Java) class Test { void f() { val = 0; g(); x = 1; int x; } void g() {} int val; // field val has not yet been declared -- OK // method g has not yet been declared -- OK // variable x has not yet been declared -- ERROR!

Scoping in Our (C--) Language uses static scoping requires that all names be declared before they are used does not allow multiple declarations of a name in the same scope (even for different kinds of names) does allow the same name to be declared in multiple nested scopes (but only once per scope) uses the same scope for a method's parameters and for the local variables declared at the beginning of the method

Symbol Table Given a declaration of a name, is there already a declaration of the same name in the current scope (i.e., is it multiply declared)? Given a use of a name, to which declaration does it correspond (using the "most closely nested" rule), or is it undeclared?

Symbol Table Operations Look up a name in the current scope only (to check if it is multiply declared). Look up a name in the current and enclosing scopes (to check for a use of an undeclared name, and to link a use with the corresponding symbol-table entry). Insert a new name into the symbol table with its attributes. Do what must be done when a new scope is entered. Do what must be done when a scope is exited.

Symbol Table Entries in Table Use List of Hashtables Symbol Name Type Nesting Level of declaration Use List of Hashtables Declarations In S Declarations made in scopes That enclose S

Example Symbol Table void f(int a, int b) { double x; while (...) { int x, y; ... } void g() { f(); x: int, 3 y: int, 3 a: int, 2 b: int, 2 x: double, 2 f: (int, int), 1 g: (), 1

Operations Performed during Scope Analysis On scope entry: increment the current level number and add a new empty hashtable to the front of the list. To process a declaration of x: look up x in the first table in the list. If it is there, then issue a "multiply declared variable" error; otherwise, add x to the first table in the list. To process a use of x: look up x starting in the first table in the list; if it is not there, then look up x in each successive table in the list. If it is not in any table then issue an "undeclared variable" error. On scope exit, remove the first table from the list and decrement the current level number.

Type Checking Determine the type of each expression in the program (each node in the AST that corresponds to an expression). Find type errors.

Type Rules specify, for every operator (including assignment), what types the operands can have, and what is the type of the result. Examples: Addition of int and double: result double (in java and C++) Assignment of double to int: C++ OK, Java error

Table of Operators and Types (in Java) 1st Operand 2nd Operand Result + int double = && boolean < …

Context Method Call Type Errors Context Type Errors the condition of an if statement the condition of a while loop the termination condition part of a for loop Method Call Type Errors calling something that is not a method calling a method with the wrong number of arguments calling a method with arguments of the wrong types