CSE 3302 Programming Languages

Slides:



Advertisements
Similar presentations
Chapter 3:: Names, Scopes, and Bindings
Advertisements

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:
Names and Bindings.
Basic Semantics.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 5 Basic Semantics
Programming Languages Third Edition
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Chapter 5K. Louden, Programming Languages1 Chapter 5 Basic Semantics.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Slide 1 Chapter 5-b Attributes of variables. Slide 2 Attributes of variables { int sum = 5;... } sum int IdentifierType Value 0110.
Chapter 9: Subprogram Control
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Chapter 5 - Basic Semantics
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Runtime Environments Compiler Construction Chapter 7.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Basic Semantics Attributes, Bindings, and Semantic Functions
Basic Semantics Associating meaning with language entities.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
© Kenneth C. Louden, Chapter 5 - Basic Semantics Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 3302 Programming Languages
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Names, Scope, and Bindings Programming Languages and Paradigms.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Advanced Programming in C
Data Types In Text: Chapter 6.
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
CSE 374 Programming Concepts & Tools
CSE 3302 Programming Languages
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
CSE 3302 Programming Languages
Names, Bindings, and Scopes
Concepts of programming languages
Semantic Analysis Chapter 6.
CSE 3302 Programming Languages
CSC 253 Lecture 8.
Semantics CSE 340 – Principles of Programming Languages Spring 2016
Names, Binding, and Scope
CSC 253 Lecture 8.
CSE 3302 Programming Languages
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSE 3302 Programming Languages
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSC 533: Programming Languages Spring 2015
Memory Allocation CS 217.
CSE 3302 Programming Languages
Semantic Analysis Chapter 6.
CSE 3302 Programming Languages
Names and Binding In Text: Chapter 5.
Programming Languages and Paradigms
CSE 3302 Programming Languages
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Presentation transcript:

CSE 3302 Programming Languages Semantics (cont.) Kelly Scott French Spring 2017 Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Symbol Table Symbol Table: maintain bindings. Can be viewed as functions that map names to their attributes. Names Attributes SymbolTable Lecture 6 - Semantics, Spring 2017 2 CSE3302 Programming Languages, UT-Arlington

Static vs. Dynamic Scope Static scope (lexical scope): scope maintained statically (during compilation) follow the layout of source codes used in most languages Dynamic scope: scope maintained dynamically (during execution) follow the execution path few languages use it (The bindings cannot be determined statically, may depend on user input). Lisp: considered a bug by its inventor. Perl: can choose lexical or dynamic scope Lecture 6 - Semantics, Spring 2017 3 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Static Scope int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); x integer, global y character, global Lecture 6 - Semantics, Spring 2017 4 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Static Scope int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in p: the bindings available in p double, local to p x integer, global y Character, global Lecture 6 - Semantics, Spring 2017 5 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Static Scope int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in q: the bindings available in q x integer, global integer, local to q y character, global Lecture 6 - Semantics, Spring 2017 6 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Static Scope int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in main: the bindings available in main character, local to main x integer, global y character, global Lecture 6 - Semantics, Spring 2017 7 CSE3302 Programming Languages, UT-Arlington

Practice for Static Scope int x,y; void g(void) { x = x + 1; y = x + 1; } void f(void) { int x; y = y + 1; x = y + 1; g(); main() { x = 1; y = 2; f(); printf("x=%d,y=%d\n",x,y); Point 1 Question 1: Draw the symbol table at the given points in the program, using static scope? Point 2 Question 2: What does the program print, using static scope? Point 3 Lecture 6 - Semantics, Spring 2017 8 CSE3302 Programming Languages, UT-Arlington

What if dynamic scope is used? int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); x integer, 1, global y character, ‘a’, global Lecture 6 - Semantics, Spring 2017 9 CSE3302 Programming Languages, UT-Arlington

What if dynamic scope is used? int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in main: the bindings available in main character, ‘b’, local to main x integer, 1, global y character, ‘a’, global Lecture 6 - Semantics, Spring 2017 10 CSE3302 Programming Languages, UT-Arlington

What if dynamic scope is used? int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in q: the bindings available in q character, ‘b’, local to main x integer, 1, global integer, 42, local to q y character, ‘a’, global Lecture 6 - Semantics, Spring 2017 11 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

What if dynamic scope is used? int x = 1; char y = ‘a’; void p(void) { double x=2.5; printf(“%c\n”,y); } void q(void) { int y = 42; printf(“%d\n”,x); p(); main() { char x = ‘b’; q(); The symbol table in p: the bindings available in p double, 2.5, local to p character, ‘b’, local to main x integer, 1, global integer, 42, local to q y character, ‘a’, global Lecture 6 - Semantics, Spring 2017 12 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Practice for Dynamic Scope int x,y; void g(void) { x = x + 1; y = x + 1; } void f(void) { int x; y = y + 1; x = y + 1; g(); main() { x = 1; y = 2; f(); printf("x=%d,y=%d\n",x,y); Point 1 Question 1: Draw the symbol table at the given points in the program, using dynamic scope? Point 2 Question 2: What does the program print, using dynamic scope? Point 3 Lecture 6 - Semantics, Spring 2017 13 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Overloading What is overloading? Why overloading? What can be overloaded? Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Overload Resolution Overload Resolution: select one entity. Name isn’t sufficient in resolution: need extra information (often data types) Lecture 6 - Semantics, Spring 2017 15 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Function/Method Overloading C: no overloading C++/Java/Ada: resolution by number and types of parameters. Perfect if exact match exists; No perfect match: different conversion rules Ada: automatic conversions not allowed. Java: conversions allowed in certain directions. C++: automatic conversions more flexible. 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); Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Overload Resolution Example (1) int sum(int, int); (2) double sum(double, int); (3) double sum(double, double); int x; double y; C++ Java Ada x = sum(3,4); 1 1 1 y = sum(3,4); 1 1 0 x = sum(3,4.5); 0 0 0 y = sum(3,4.5); 0 3 0 x = sum(3.5,4); 2 0 0 y = sum(3.5,4); 2 2 2 x = sum(3.5,4.5); 3 0 0 y = sum(3.5,4.5); 3 3 3 Lecture 6 - Semantics, Spring 2017 17 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Environment Location: one specific attribute of names Environment: maintain bindings of names to locations Static vs. dynamic FORTRAN: completely static LISP: completely dynamic Algol-descendants (C, C++, Ada, Java) : combination global variables: static local variables: dynamic Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Stack-Based Allocations static(global) area heap (unallocated) automatically-allocated spaces (local variables, procedures (chapter 8) under the control of runtime system manually-allocated spaces under the control of programmer Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

K. Louden, Programming Languages Example A: { int x; char y; B: { double x; int a; } C: { char y; int b; D: { int x; double y; x y Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Example A: { int x; char y; B: { double x; int a; } C: { char y; int b; D: { int x; double y; x y x Lifetime is the duration of B a Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Example A: { int x; char y; B: { double x; int a; } C: { char y; int b; D: { int x; double y; x y y b Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Example A: { int x; char y; B: { double x; int a; } C: { char y; int b; D: { int x; double y; x y y b x y Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Example A: { int x; char y; B: { double x; int a; } C: { char y; int b; D: { int x; double y; x y y b Chapter 5 K. Louden, Programming Languages

Heap-Based Allocation int *x; x=(int *)malloc(sizeof(int)); free(x); C++ x= new int; delete x; Java Integer x = new Integer(2); //no delete //need garbage collection) static(global) area stack (unallocated) heap x Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Environment Declaring versus Defining C allows for multiple modules and so allows the definition to be separate from the declaration (http://stackoverflow.com/questions/496448/how-to-correctly-use-the-extern-keyword-in-c?noredirect=1&lq=1) Problem Solution File stdio.h: int errno; /* other stuff...*/ File stdio.h: extern int errno; /* other stuff...*/ myCFile1.c: #include <stdio.h> Code... File stdio.c: int errno; myCFile2.c: #include <stdio.h> Code... myCFile1.c: #include <stdio.h> Code... myCFile2.c: #include <stdio.h> Code... Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Scope vs. Lifetime Lifetime beyond scope: alive in scope hole alive outside scope Scope beyond lifetime (unsafe) Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Example: Alive in scope hole A: { int x; char y; B: { double x; int a; } C: { char y; int b; D: { int x; double y; x x y x a Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Example: Alive outside scope int func(void) { static int counter = 0; counter += 1; return counter; } main() { int i; int x; for (i=0; i<10; i++) { x=func(); } printf(“%d\n”, x); counter Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Example: Scope beyond lifetime Dangling pointer: int *x, *y, *z; x=(int *) malloc(sizeof(int)); *x=2; y=x; free(x); . . . printf(“%d\n”,*y); Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Box-and-Circle Diagram for Variables Value Name Location r-value l-value x x = y y assignment Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Assignment by sharing Java: Student x = new Student(“Amy”); Student y = new Student(“John”); x.setAge(19); x = y; y.setAge(21); x y Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Assignment by cloning x = y x y Lecture 6 - Semantics, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Aliases (1) int *x, *y; (2) x = (int *)malloc(sizeof(int)); (6) printf(“%d\n”,*x); After line 1: Lecture 6 - Semantics, Spring 2017 34 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Aliases (1) int *x, *y; (2) x = (int *)malloc(sizeof(int)); (6) printf(“%d\n”,*x); After line 2: Lecture 6 - Semantics, Spring 2017 35 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Aliases (1) int *x, *y; (2) x = (int *)malloc(sizeof(int)); (6) printf(“%d\n”,*x); After line 3: Lecture 6 - Semantics, Spring 2017 36 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Aliases (1) int *x, *y; (2) x = (int *)malloc(sizeof(int)); (6) printf(“%d\n”,*x); After line 4: Lecture 6 - Semantics, Spring 2017 37 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Aliases (1) int *x, *y; (2) x = (int *)malloc(sizeof(int)); (6) printf(“%d\n”,*x); After line 5: Lecture 6 - Semantics, Spring 2017 38 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Practice for Aliases #include <stdio.h> (2) main(){ (3) int **x; (4) int *y; (5) int z; (6) x = (int**)malloc(sizeof(int*)); (7) y = (int*)malloc(sizeof(int)); (8) z = 1; (9) *y = 2; (10) *x = y; (11) **x = z; (12) printf(“%d\n”,*y); (13) z = 3; (14) printf(“%d\n”,*y); (15) **x = 4; (16) printf(“%d\n”,z); (17) return 0; (18)} Question 1: Draw box-and-circle diagrams of the variables after line 11 and 15. Question 2: Which variables are aliases at each of those points? Questions can be left to the student or done in class, time depending Question 3: What does the program print? Lecture 6 - Semantics, Spring 2017 39 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Dangling References int *x, *y; … x = (int *)malloc(sizeof(int)); free(x); /* *y is now a dangling reference */ printf(“%d\n”,*y); /*illegal reference*/ Lecture 6 - Semantics, Spring 2017 40 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Dangling References {int *x; { int y; y = 2; x = &y; } /* *x is now a dangling reference */ Lecture 6 - Semantics, Spring 2017 41 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Dangling References /* *y is a dangling reference */ int* dangle(void) { int x; return &x; } … y = dangle(); /* *y is a dangling reference */ Lecture 6 - Semantics, Spring 2017 42 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008