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

Similar documents
Semantics. Names. Binding Time

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

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

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

Object Oriented Languages. Hwansoo Han

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

CS S-11 Memory Management 1

CSE 3302 Programming Languages Lecture 5: Control

MIDTERM EXAMINATION - CS130 - Spring 2005

CS 415 Midterm Exam Spring 2002

Binding and Variables

Names, Scopes, and Bindings II. Hwansoo Han

TYPES, VALUES AND DECLARATIONS

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

Programming Languages

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

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

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

Concepts Introduced in Chapter 7

CSC 1600 Memory Layout for Unix Processes"

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

Chapter 5. Names, Bindings, and Scopes

CSE 307: Principles of Programming Languages

Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

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

Chapter 5 Names, Bindings, Type Checking, and Scopes

CS 314 Principles of Programming Languages. Lecture 11

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Class Information ANNOUCEMENTS

Programmiersprachen (Programming Languages)

Informatica 3 Syntax and Semantics

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

References and pointers

CSE 307: Principles of Programming Languages

Final CSE 131B Spring 2005

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Chapter 5 Names, Binding, Type Checking and Scopes

Names, Bindings, Scopes

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

Lecture 3. Variables. Variables

Dynamic Data Structures. CSCI 112: Programming in C

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Final CSE 131B Spring 2004

Chapter 11 Introduction to Programming in C

Advances in Programming Languages: Regions

Examples for Programming Language Features

Lecture 3: C Programm

Fundamentals of Programming Session 12

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

CSC 533: Organization of Programming Languages. Spring 2005

UNIT V Sub u P b ro r g o r g a r m a s

Chapter 11 Introduction to Programming in C

CIT Week13 Lecture

G Programming Languages - Fall 2012

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

CSE 333 Lecture 2 Memory

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

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

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

CSCI312 Principles of Programming Languages!

6. Names, Scopes, and Bindings

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Lecture 4 Memory Management

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

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

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

M1-R4: Programing and Problem Solving using C (JAN 2019)

Data, memory. Pointers and Dynamic Variables. Example. Pointers Variables (or Pointers) Fall 2018, CS2

CS 314 Principles of Programming Languages

NOTE: Answer ANY FOUR of the following 6 sections:

Chapter 11 Introduction to Programming in C

Run-time Environments

Types II. Hwansoo Han

Data Types In Text: Ch C apter 6 1

Programmin Languages/Variables and Storage

Implementing Object-Oriented Languages. Implementing instance variable access. Implementing dynamic dispatching (virtual functions)

Run-time Environments

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Programming Languages CRN Test 2 Version 1 CMSC 4023 Autumn 2013

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

Compiler construction

CS 345. Functions. Vitaly Shmatikov. slide 1

Names and Types. standard hue names. Dr. Philip Cannata 1

Chapter 11 Introduction to Programming in C

Name, Scope, and Binding. Outline [1]

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

G Programming Languages - Fall 2012

Chapter 11 Introduction to Programming in C

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

CMSC 330: Organization of Programming Languages. Ownership, References, and Lifetimes in Rust

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

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

Memory management COSC346

Programming Languages

A Fast Review of C Essentials Part II

Advanced Programming & C++ Language

Transcription:

-2-1 CSE 3302 Programming Languages Semantics (cont.) Smbol Table Smbol Table: maintain bindings. Can be viewed as functions that map names to their attributes. Names SmbolTable Attributes Chengkai Li, Weimin He Spring Chengkai Li, Weimin He, 1 Chengkai Li, Weimin He, 2 Static vs. Dnamic Scope Static scope (leical scope): scope maintained staticall (during compilation) follow the laout of source codes used in most languages Dnamic scope: scope maintained dnamicall (during eecution) follow the eecution path few languages use it (The bindings cannot be determined staticall, ma depend on user input). Lisp: considered a bug b its inventor. Perl: can choose leical or dnamic scope Chengkai Li, Weimin He, 3 int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); char = b ; character, global Chengkai Li, Weimin He, 4 int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in p: the bindings available in p double, local to p char = b ; Character, global Chengkai Li, Weimin He, 5 int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in q: the bindings available in q integer, local to q char = b ; character, global Chengkai Li, Weimin He, 6 1

-2-1 int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in main: the bindings available in main character, local to main char = b ; character, global Chengkai Li, Weimin He, 7 Practice for Point 1 Point 2 Point 3 int,; void g(void) { = + 1; = + 1; void f(void) { int ; = + 1; = + 1; = 1; = 2; f(); printf("=%d,=%d\n",,); Question 1: Draw the smbol table at the given points in the program, using static scope? Question 2: What does the program print, using static scope? 8 Chengkai Li, Weimin He, What if dnamic scope is used? int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); char = b ; Chengkai Li, Weimin He, 9 What if dnamic scope is used? int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in main: the bindings available in main character, b, local to main char = b ; Chengkai Li, Weimin He, 10 What if dnamic scope is used? int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); 98 The smbol table in q: the bindings available in q character, b, local to main integer, 42, local to q char = b ; Chengkai Li, Weimin He, 11 What if dnamic scope is used? int = 1; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); 98 * The smbol table in p: the bindings available in p double, 2.5, local to p character, b, local to main integer, 42, local to q char = b ; Chengkai Li, Weimin He, 12 2

-2-1 Practice for Dnamic Scope Point 1 Point 2 Point 3 int,; void g(void) { = + 1; = + 1; void f(void) { int ; = + 1; = + 1; = 1; = 2; f(); printf("=%d,=%d\n",,); Question 1: Draw the smbol table at the given points in the program, using dnamic scope? Question 2: What does the program print, using dnamic scope? 13 Chengkai Li, Weimin He, Overloading What is overloading? Wh overloading? What can be overloaded? Chengkai Li, Weimin He, 14 Overload Resolution Overload Resolution: select one entit. Name isn t sufficient in resolution: need etra information (often data tpes) Function/Method Overloading C: no overloading C++/Java/Ada: resolution b number and tpes of parameters. Perfect if eact match eists; No perfect match: different conversion rules Ada: automatic conversions not allowed. Java: conversions allowed in certain directions. C++: automatic conversions more fleible. e.g., int sum(int a, int b) { double sum(double a, double b) { double sum(double a, int b) { sum(1); sum(1, 2); sum(1.0, 2.0); sum(1, 2.0); Chengkai Li, Weimin He, 15 Chengkai Li, Weimin He, 16 Overload Resolution Eample (1) int sum(int, int); (2) double sum(double, int); (3) double sum(double, double); int ; double ; C++ Java Ada = sum(3,4); 1 1 1 = sum(3,4); 1 1 0 = sum(3,4.5); 0 0 0 = sum(3,4.5); 0 3 0 = sum(3.5,4); 2 0 0 = sum(3.5,4); 2 2 2 = sum(3.5,4.5); 3 0 0 = sum(3.5,4.5); 3 3 3 Environment Location: one specific attribute of names Environment: maintain bindings of names to locations Static vs. dnamic FORTRAN: completel static LISP: completel dnamic Algol-descendants (C, C++, Ada, Java) : combination global variables: static local variables: dnamic Chengkai Li, Weimin He, 17 Chengkai Li, Weimin He, 18 3

-2-1 Stack-Based Allocations Eample static(global) area stack (unallocated) heap automaticall-allocated spaces (local variables, procedures (chapter 8) under the control of runtime sstem manuall-allocated spaces under the control of programmer A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; Chengkai Li, Weimin He, 19 Chapter 5 K. Louden, Programming Languages 20 Eample Eample A: { int ; char ; B: { double ; Lifetime is the duration of B C: { char ; D: { int ; double ; a A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; b Chapter 5 K. Louden, Programming Languages 21 Chapter 5 K. Louden, Programming Languages 22 Eample Eample A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; b A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; b Chapter 5 K. Louden, Programming Languages 23 Chapter 5 K. Louden, Programming Languages 24 4

-2-1 Heap-Based Allocation Scope vs. Lifetime C int *; =(int *)malloc(sizeof(int)); free(); C++ int *; = new int; delete ; Java Integer = new Integer(2); //no delete //need garbage collection) static(global) area stack (unallocated) heap Lifetime beond scope: alive in scope hole alive outside scope Scope beond lifetime (unsafe) Chengkai Li, Weimin He, 25 Chengkai Li, Weimin He, 26 Eample: Alive in scope hole Eample: Alive outside scope A: { int ; char ; B: { double ; C: { char ; D: { int ; double ; a int func(void) { static int counter = 0; counter += 1; return counter; counter main() { int i; int ; for (i=0; i<10; i++) { =func(); printf( %d\n, ); Chengkai Li, Weimin He, 27 Chengkai Li, Weimin He, 28 Eample: Scope beond lifetime Dangling pointer: int *, *, *z; =(int *) malloc(sizeof(int)); *=2; =; free();... printf( %d\n,*); Bo-and-Circle Diagram for Variables Name l-value Location assignment Value = r-value Chengkai Li, Weimin He, 29 Chengkai Li, Weimin He, 30 5

-2-1 Assignment b sharing Assignment b cloning Java: Student = new Student( Am ); Student = new Student( John );.setage(19); = ;.setage(21); = Chengkai Li, Weimin He, 31 Chengkai Li, Weimin He, 32 (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 1: (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 2: Chengkai Li, Weimin He, 33 Chengkai Li, Weimin He, 34 (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 3: (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 4: Chengkai Li, Weimin He, 35 Chengkai Li, Weimin He, 36 6

-2-1 (1) int *, *; (2) = (int *)malloc(sizeof(int)); (3) * = 1; (4) = ; (5) * = 2; (6) printf( %d\n,*); After line 5: Practice for (1) #include <stdio.h> (2) main(){ (3) int **; (4) int *; (5) int z; (6) = (int**)malloc(sizeof(int*)); (7) = (int*)malloc(sizeof(int)); (8) z = 1; (9) * = 2; (10) * = ; (11) ** = z; (12) printf( %d\n,*); (13) z = 3; (14) printf( %d\n,*); (15) ** = 4; (16) printf( %d\n,z); (17) return 0; (18) Question 1: Draw bo-and-circle diagrams of the variables after line 11 and 15. Question 2: Which variables are aliases at each of those points? Question 3: What does the program print? Chengkai Li, Weimin He, 37 Chengkai Li, Weimin He, 38 Dangling References int *, *; = (int *)malloc(sizeof(int)); * = 2; = ; free(); /* * is now a dangling reference */ printf( %d\n,*); /*illegal reference*/ Dangling References {int *; { int ; = 2; = &; /* * is now a dangling reference */ Chengkai Li, Weimin He, 39 Chengkai Li, Weimin He, 40 Dangling References int* dangle(void) { int ; return &; = dangle(); /* * is a dangling reference */ Chengkai Li, Weimin He, 41 7