Bernd Fischer RW713: Compiler and Software Language Engineering.

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.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Elaboration or: Semantic Analysis Compiler Baojian Hua
Compiler Construction
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
Tutorial 6 & 7 Symbol Table
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Elaboration or: Semantic Analysis Compiler Baojian Hua
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
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 -
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
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.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
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.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
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.
Basic Semantics Associating meaning with language entities.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Wu Hang.  Introduction  Requirements  Checking  Environment  How to start  Tips.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
COMPILERS Semantic Analysis hussein suleman uct csc3003s 2009.
Lecture 9 Symbol Table and Attributed Grammars
Name Spaces: ALL versus OOL
Names and Attributes Names are a key programming language feature
Constructing Precedence Table
CS 326 Programming Languages, Concepts and Implementation
Semantic Analysis with Emphasis on Name Analysis
Overview of Compilation The Compiler BACK End
Semantic Analysis Chapter 6.
Names, Binding, and Scope
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Overview of Compilation The Compiler BACK End
Semantic Analysis Semantic analysis includes
Semantic Analysis Chapter 6.
CS 432: Compiler Construction Lecture 11
Programming Languages and Paradigms
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
COMPILERS Semantic Analysis
SYMBOL TABLE Chuen-Liang Chen Department of Computer Science
Corresponds with Chapter 5
Presentation transcript:

Bernd Fischer RW713: Compiler and Software Language Engineering

Contextual Analysis

Appel’s view of a compiler

Contextual Analysis “Semantic” Analysis is a misnomer –context-sensitive syntax Determine whether program is well-formed –scope rules(i.e., visibility) –type rules(i.e., compatibility) Two phases –name resolution- what does an identifier refer to? –type checking- can you do that with it?

Name Resolution Nested block structure  multiple scope levels Match identifier use with corresponding declaration: int i, j;... void f(int k) { char i;... for (int i = 0; i < 10; i++){... if (i > 5) {...} } i = “a”; }... j = i + 1;

Names vs. Symbols Operations on strings are expensive –comparison, hashing, … Instead: –enter all names into common area (lexeme pool) –can be done by lexer –work with symbols

Representing Symbols package Symbol; public class Symbol { private String name; private Symbol(String n) { name = n; } private static java.util.Dictionary pool = new java.util.Hashtable(); public String toString() { return name; } public static Symbol toSymbol(String n) { String u = n.intern(); Symbol s = (Symbol) pool.get(u); if (s == null) { s = new Symbol(u); pool.put(u,s); } return s; }

Symbol Tables Mapping symbols (identifiers) ↦ attributes –constant:type, value, … –variable:type, pointer to declaration, … –method:return and argument types, modifiers, … –class: pointer to (public) symbol table, … Also called environments = set of bindings Must cope with –nested scopes:block structure –parallel scopes:multiple name spaces Efficiency is important (but not paramount…) –lookup is most common operation

Symbol Table Interface public class Table { public Table(); public void put(Symbol key, Object value); public Object get(Symbol key); public void beginScope(); public void endScope(); public java.util.Enumeration keys(); }

Symbol Table Implementation Use hash table for efficiency –allow multiple entries with same key (external chaining) New binding for identifier x added to head of list for hash table bucket –“hides” earlier occurrences Uses auxiliary stack to implement “undo”: –beginScope() pushes marker onto stack –subsequent entries recorded on stack –endScope() pops symbols from stack, removes binding from table.

Hyperscope Standard Environment –standard collection of types, functions, etc. –can be used without declaration / import –e.g., built-in Pascal-functions, java.lang Initialize symbol table with these –at outermost scope level –watch for re-definitions (if required by language)

Multiple Name Spaces In many languages, a single symbol table is not sufficient: Solutions: –change symbol table type: Tag*Symbol public void put(Tag tbl, Symbol key, Object value); public Object get(Tag tbl, Symbol key); –separate tables for types and variables let type a = int var a : a = 5 var b : a = a in... end Type and variable declaration must be visible at same time!

Multiple Name Spaces (2) Modules/classes have separate name spaces –accessible by qualification / import –separate tables for each compilation unit class A { public static int x = 2; } class B { public static int x = A.x; } class Test { public static void main(String[] args) { System.out.println(""+(A.x + B.x)); }

Multiple Name Spaces (2) Modules/classes have separate name spaces –accessible by qualification / import –separate tables for each compilation unit Separate compilation requires persistent symbol tables –Modula-2:.sym -files –C: delayed to linker, no real error check

Type Checking Checking that identifiers are used correctly (i.e. in accordance with their [declared] type) Statically-typed languages –(Fortran, C), Ada, C++, Java, ML, Haskell,… –types always known at compile time Dynamically-typed languages –Lisp, Scheme, Smalltalk, Python, … –types determined at run time int x; void f() { f = x(2); }

Type Checking (2) Recursive walk over tree Uses current environment | AssignmentStmt | | Variable Expression –look up type of Variable –check and determine type of Expression –check for compatibility Compatibility rules –subtyping –coercion: insert operations to enforce compatibility

Type Checking Function Calls Type of greater is int x int  boolean To check applied occurrence: –look up greater in symbol table –check types of actual parameters match –result type of function (boolean) becomes result type of function call boolean greater (int x, int y){ return (x > y); }... if (greater(a, b)) {... }

Name-Binding Languages

Type Systems

Structural Operational Semantics

Reference Attribute Grammars

NaBL (Name Binding Language) domain-specific language to describe scope rules part of compiler-generation tool suite (Spoofax) compiled into name resolution algorithm semantics based on scope graphs

Definitions and References

Unique and Non-Unique Definitions

Namespaces

Scope

C# Namespaces are Scopes

Imports

Interaction with Type System (1)

Interaction with Type System (2)

Interaction with Type System (3)

NaBL use cases static checking editor services transformation refactoring code generation

NaBL use cases – reference resolution

NaBL use cases – code completion