CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Similar documents
Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Programming Languages Third Edition. Chapter 7 Basic Semantics

G Programming Languages - Fall 2012

Chapter 5. Names, Bindings, and Scopes

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

CS558 Programming Languages

TYPES, VALUES AND DECLARATIONS

CS558 Programming Languages

CSE 307: Principles of Programming Languages

Names, Scopes, and Bindings II. Hwansoo Han

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Names, Bindings, Scopes

Chapter 5 Names, Binding, Type Checking and Scopes

Concepts Introduced in Chapter 7

Semantics (cont.) Symbol Table. Static Scope. Static Scope. Static Scope. CSE 3302 Programming Languages. Static vs. Dynamic Scope

CSE 307: Principles of Programming Languages

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

The role of semantic analysis in a compiler

CS558 Programming Languages

Informatica 3 Syntax and Semantics

CMSC 330: Organization of Programming Languages

CPSC 3740 Programming Languages University of Lethbridge. Data Types

6. Names, Scopes, and Bindings

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Types II. Hwansoo Han

G Programming Languages - Fall 2012


Programming Languages

Binding and Variables

Compiler Construction

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Scope. Chapter Ten Modern Programming Languages 1

Types and Type Inference

NOTE: Answer ANY FOUR of the following 6 sections:

Programming Languages

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CSC 533: Organization of Programming Languages. Spring 2005

Names, Scope, and Bindings

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Lecture 16: Static Semantics Overview 1

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Chapter 8 :: Composite Types

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Imperative Programming

Compiler Construction

G Programming Languages - Fall 2012

Programmiersprachen (Programming Languages)

Names and Abstractions: What s in a Name?

CSCE 314 Programming Languages. Type System

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

names names identifiers variables subroutines constants

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS 230 Programming Languages

Lecture 4 Memory Management

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Programming Languages, Summary CSC419; Odelia Schwartz

Names, Scope, and Bindings

Data Types In Text: Ch C apter 6 1

Weeks 6&7: Procedures and Parameter Passing

Types. What is a type?

LECTURE 18. Control Flow

Syntax Errors; Static Semantics

CS 314 Principles of Programming Languages

CS558 Programming Languages. Winter 2013 Lecture 3

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

CPS 506 Comparative Programming Languages. Programming Language

CS201 Some Important Definitions

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Names, Scope, and Bindings

Programmin Languages/Variables and Storage

Programming Languages

CA31-1K DIS. Pointers. TA: You Lu

C Introduction. Comparison w/ Java, Memory Model, and Pointers

A brief introduction to C programming for Java programmers

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

CSCI312 Principles of Programming Languages!

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Class Information ANNOUCEMENTS

CMSC 331 Final Exam Section 0201 December 18, 2000

In Java we have the keyword null, which is the value of an uninitialized reference type

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

[0569] p 0318 garbage

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Object-oriented Programming. Object-oriented Programming

CSE 3302 Programming Languages Lecture 5: Control

Pointers, Dynamic Data, and Reference Types

Transcription:

CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can be used Lexically scoped language with block structure C, Pascal, Ada, (Java, C++) etc. Needs stack like operation (entry-exit) from block Establishing binding int x; double f(int x); Declarations compound statements { } begin end class declarations scope of binding is the region of program were a binding is maintained lexical score C scope rules scope of binding is limited to the block in which the associated declaration appears declaration before use rule scope hole visibility vs score scope resolution operator

: int x; : char y; Lexical Scope An example : void p(void) { double x; :... { 5: int y[]; }... 6: } 7: void q(void) { 8: int y; } 9: main() { : char x;... } Symbol Table Label : x -> double local to p -> int global y -> char global p -> void function Label 5: x -> double local to p -> int global y -> int array local to nested block in p -> char global p -> void function Label 6: x -> int global y -> char global p -> void function 5 6 : int x = ; : char y = 'a'; : void p(void) { : double x =.5; 5: printf(%c\n, y); 6: { int y[]; }} Dynamic vs Static Scoping 7: void q(void) { 8: int y = ; 9: printf( %d\n, x); p();} : : main() { char x = 'b'; : q(); return 0;} Line : x -> char = b local to main -> int = global y -> char = a global Line : x -> char = b local to main -> int = global y -> int = local to q -> char = a global Line 9: x -> double =.5 -> char = b local to main -> int global y -> int -> char a global Static scoping output: a Dynamic scoping : 9(b) *() Problems with dynamic scoping Semantics are based on program execution not reading static typing and dynamic scoping can't coexist Maintaining lexical scope in interpreter hard Scheme, ML Dynamic scoping easier to implement APL, Snobol, (old Perl), (old LISP) 7 8

Nested symbol tables Name resolution & overloading ad-hoc polymorphism (the + operator) vs parametric polymorphism (the list length function) C++, Ada = overloading of operators, functions Java = overloading of functions Haskell = overloading of operators, functions plus new operators 9 How can overloading be done? Extend lookup with calling context Still complex situations can arise max(.5, )? Java : only lossless coersion Different namespaces (Java, ML) The environment Bindings of names to locations Fortran static environment Lisp - dynamic environment Most languages combination Some names don't need location const int MAX = ;

Compilers vs Interpreters Stack Compilers: symbol table what allocation code to generate as declaration is processed Interpreters: symbol table and environment are combined Typically globals are allocated statically, locals dynamically X Y X A unused space Environment = linear sequence of memory cells what about if I call a function p many times? Activation records Pointers Is a storage location whose stored value is a reference to another object unused space In C: int *x; causes allocation of a pointer variable, but NOT the allocation of a object to which x points Convention: 0 or NULL Java: null, Pascal nil *x = ; unused space More pointers the value pointed by x (a pointer variable) is int *x; x = ; *x = ; unused space 5 6

Anonymous pointers void* x; (anonymous pointer variable x) x = (int ) malloc(sizeof(int)); Allocate a block of memory that fits an integer Dereferencing operator * (*x) Pointer type is also confusingly * (for example int* or float*) free(x); Dynamic allocation Heap Memory used for calls to malloc, free is called the heap In C, C++ manual allocation is possible Java and ML don't allow allocation Static, Dynamic, Stack-based and Heap allocation 7 8 static global area stack Memory layout Heap storage can be released anywhere leaving holes. Simple stack doesn't work. Functional languages automatically manage the heap. Java allows heap allocation but not deallocation. Variables Storage semantics Value can be changed during execution name location value x = y x 5 x heap Manual control of the heap results in very few cases in more efficient code but invites all kinds of unsafe operations. y y 9 0

l-value, r-value x = y x is the name of a location of a variable y is the value of the variable named y In ML distinction explicit: x :=!x +; x :=!y; int x; Address of operator in C &x is the address of x and can be assigned to a pointer; For example: int x; x = ; int *y = &x; int z = *y; int k = &y; (what does this one do?) The swamp of C Address arithmetic (pointers can be added subtracted like integers) mixing dereferencing and address of operators expressions and assignment can lead to some very confusing and complex situations Pointer semantics Assignment by sharing Assignment by cloning done in Java by implicit pointers x *x = *y (pointers under y under the hood) x y 5 5 clone

Value semantics constants No location just a value Not necessarily known at compile time once computed never updated Examples: ML, Single assignment C In Java, keyword final is used for constants (gets only one final value) and static can be used when value can be computed prior to execution. Function Definitions In virually all languages functions are essentially constants whose values are functions In ML: val square = fn(x:int) => x * x; 5 6 Function Pointers in C int gcd(int u, int v) { if (v == 0) return u; else return gcd(v, u % v); } /* function variable pointer syntax necessary otherwise prototype */ int (*gcdv)(int, int) = gcd; /* can be called */ Aliases Same thing bound to two different names at the same time int *x, *y; x = (int *) malloc(sizeof(int)); *x = ; y = x; y = ; /* changes x although x doesn't appear in the assignment */ printf( %d\n, *x); gcdv(5,) 7 8

Dangling references Location that has been dellocated from the environment but can still be accessed pointer to a deallocated object: int *x, *y; x = (int *) malloc(sizeof(int)); *x = ; y = x; free(x); printf( %d\n, *y); Garbage Eliminate dangling reference by never deallocating Garbage only wastes memory doesn't corrupt the program behavior int *x;... x = (int *) malloc(sizeof(int)); x = NULL; 9 0 Garbage collection Lisp, Smalltalk, Java ML has a very efficient garbage collector There is a lot of interesting work in how to implement garbage collectors some of you may learn about it when you write a Compiler