CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Symbol Table.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 Compiler Construction Intermediate Code Generation.
Compiler Construction
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
ISBN Chapter 10 Implementing Subprograms.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Tutorial 6 & 7 Symbol Table
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
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 -
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
Compilation (Chapter 3) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Semantic Analysis CS 671 February 5, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens –e.g.: main$
CS 2104 Prog. Lang. Concepts Subprograms
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Compiler Construction
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.
Basic Semantics Associating meaning with language entities.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Chapter 1 Introduction Major Data Structures in Compiler
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Bernd Fischer RW713: Compiler and Software Language 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.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
ISBN Chapter 10 Implementing Subprograms.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Lecture 9 Symbol Table and Attributed Grammars
CS 326 Programming Languages, Concepts and Implementation
Implementing Subprograms
Semantic Analysis with Emphasis on Name Analysis
CS 536 / Fall 2017 Introduction to programming languages and compilers
Syntax Errors; Static Semantics
Names, Binding, and Scope
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Compiler Construction
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
COMPILERS Semantic Analysis
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Compiler Construction
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1

Where we are at So far, we’ve only defined the structure of a program—aka the syntax We are now diving into the semantics of the program 2

Semantics: The Meaning of a Program The parser can guarantee that the program is structurally correct The parser does not guarantee that the program makes sense: – void var; – Undeclared variables – Ill-typed statements int doubleRainbow; doubleRainbow = true; 3

Static Semantic Analysis Two phases – Name analysis (aka name resolution) For each scope – Process declarations, add them to symbol table – Process statements, update IDs to point to their entry – Type analysis Process statements – Use symbol table info to determine the type of each expression 4

Why do we need this phase? Code generation – Different operations use different instructions: Consistent variable access Integer addition vs floating point addition Operator overloading Optimization – Symbol table knows where a variable is used Can remove dead code Can weaken the type (e.g., int -> bool) NOTE: pointers can make this occasionally impossible Error checking 5

Semantic Error Analysis For non-trivial programming languages, we run into fundamental undecidability problems – Halting? – Crashes? Sometimes practical feasibility as well – Thread interleavings – Interprocedural dataflow 6

Catch Obvious Errors We may not be able to guarantee the absence of errors We can at least catch some, though – Undeclared identifiers – Multiply declared identifiers – Ill-typedness 7

Name analysis Associating ids with their uses Need to bind names before we can type uses – What definitions do we need about identifiers? Symbol table – How do we bind definitions and uses together? scope 8

Symbol table entries A table that binds a name to information we need Information typically needed in an entry – Kind (struct, variable, function, class) – Type (int, int × string → bool, struct) – Nesting level – Runtime location (where it’s stored in memory) 9

Symbol table operations – Insert entry – Lookup – Add new table – Remove/forget a table When should we use these operations? 10

Scope: the lifetime of a name Block of code in which a name is visible/valid – No scope Assembly / FORTRAN – static / most deeply nested scope Should be familiar – C / Java / C++ 11

Many decisions related to scope!! 12

Static vs Dynamic Scope Static – Correspondence between a variable use / decl is known at compile time Dynamic – Correspondence determined at runtime 13

Exercises 14 What uses/decl are OK in this Java code?

Exercises 15 void main() { int x = 0; f1(); g(); f2(); } void f1() { int x = 10; g(); } void f2() { int x = 20; f1(); g(); } void g() { print(x); } What does this return, assuming dynamic scoping?

Variable shadowing Do we allow names to be reused in nesting relations? What about when the kinds are different? 16

Overloading Same name different type 17

Forward references Use of a name before it is filled out in the symbol table Requires two passes over the program – 1 to fill symbol table, 1 to use it 18

Example 19 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; } if (x == k) { int x = y; } Determine which uses correspond to which decl

Example 20 int (1)k=10, (2)x=20; void (3)foo(int (4)k) { int (5)a = x(2); int (6)x = k(4); int (7)b = x(6); while (...) { int (8)x; if (x(8) == k(4)) { int (9)k, (10)y; k(9) = y(10) = x(8); } if (x(8) == k(4)) { int (11)x = y(ERROR); } Determine which uses correspond to which decl

Name analysis for YES Time to make some decisions – What scoping rules will we allow? – What info does a YES compiler need in its symbol table? – Relevant for P4 21

YES: A statically scoped language YES is designed for ease of symbol table use – global scope + nested scopes – All declarations are made at the top of a scope – Declarations can always be removed from table at end of scope 22

YES: Nesting Like Java or C, we’ll use most deeply nested scope to determine binding – Shadowing Variable shadowing allowed Struct definition shadowing allowed 23

YES: Symbol table implementation We want the symbol table to efficiently add an entry when we need it, remove it when we’re done with it We’ll go with a list of hashmaps – This makes sense since we expect to remove a lot of names from scope at once 24

Example 25

YES: Symbol kinds Identifier types – Variables Carries a name, primitive type – Function declarations Carries a name, return type, list of param types – Struct definitions Carries a name, list of fields (types with names), size 26

YES: Sym class implementation There are many ways to implement your symbols Here’s one suggestion – Sym class for variable definitions – FnSym subclass for function declarations – StructDefSym for struct type definitions Contains it’s OWN symbol table for it’s field definitions – StructSym for when you want an instance of a struct 27

Implementing name analysis with an AST At this point, we’re basically done with the Parse Tree Walk the AST, much like the unparse() method – Augment AST nodes with a link to the relevant name in the symbol table – Build new entries into the symbol table when a declaration is encountered 28

29