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.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Basic Semantics.
Chapter 5 Basic Semantics
Programming Languages Third Edition
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
Chapter 5K. Louden, Programming Languages1 Chapter 5 Basic Semantics.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 4: Names Fall 2009 Marco Valtorta.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
CSC321: Programming Languages Names Chapter 4: Names 4.1 Syntactic Issues 4.2 Variables 4.3 Scope 4.4 Symbol Table 4.5 Resolving References 4.6 Dynamic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Review of C++ Programming Part II Sheng-Fang Huang.
Scope.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Names. 2 Variables  binding is an association between an entity (such as a variable) and a property (such as its value). A binding is static if the association.
Basic Semantics Attributes, Bindings, and Semantic Functions
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.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
CSE 3302 Programming Languages
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advanced Programming in C
Lecture 9 Symbol Table and Attributed Grammars
Name Spaces: ALL versus OOL
Names and Attributes Names are a key programming language feature
CSE 3302 Programming Languages
Type Checking, and Scopes
CSE 3302 Programming Languages
Names, Bindings, and Scopes
Names.
Semantic Analysis Chapter 6.
Semantics CSE 340 – Principles of Programming Languages Spring 2016
Names, Scopes, and Bindings: Scopes
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Scope of Variables.
Scope, Visibility, and Lifetime
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Classes and Objects.
CSE 3302 Programming Languages
Semantic Analysis Chapter 6.
CSE 3302 Programming Languages
Programming Languages
Names, Bindings, and Scopes
CSE 341 Lecture 11 b closures; scoping rules
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
CSE 3302 Programming Languages
Types and Related Issues
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Presentation transcript:

CSE 3302 Programming Languages Semantics Chengkai Li, Weimin He Spring 2008 Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Names Names: identify language entities variables, procedures, functions, constants, data types, … Attributes: properties of names Examples of attributes: Data type: int n = 5; ( data type: integer) int itself is a name Value: ( value: 5) Location: int* y; y = new int; Parameters, return value: int f(int n) {...} … Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Binding Binding: associating attributes to names declarations assignments declarations (prototype) and definition of a function The bindings can be explicit or implicit e.g. int x; Explicit binding: the data type of x Implicit binding: the location of x (static or dynamic, depending on where the declaration is) Lecture 5 - Semantics, Spring 2008 3 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Binding Time Binding Time: the time when an attribute is bound to a name. Static binding (static attribute): occurs before execution Language definition/implementation time: The range of data type int translation time (parsing/semantic analysis): The data type of a variable link time: The body of external function load time: Location of global variable Dynamic binding (dynamic attribute): occurs during execution entry/exit from procedure or program: the value of local variables Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Where can declarations happen? Blocks ({}, begin-end, … Algol descendants: C/C++, Java, Pascal, Ada, …) e.g., C Function body Anywhere a statement can appear (compound statement) External/global Structured data type Class Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

C++ Example const int Maximum = 100; struct FullName {string Lastname, string FirstName}; class Student { private: struct FullName name; int Age; public: void setValue(const int a, struct FullName name); int TStudent(); … }; void Student::setAge(const int a, string lName, string fName) { int i; Age = a; { int j; name.LastName = lName; name.FirstName = fName; } Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Scope of Binding Scope of Binding: the region of the program where the binding is maintained (is valid, applies). Block-structured language lexical scope (static scope): from the declaration to the end of the block containing the declaration. dynamic scope : introduced later. Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Example int x; void p(void) { char y; . . . { int i; } void q(void) { double z; main() { int w[10]; x p y i q z main w Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Declaration before Use void p(void) { int x; . . . char y; } Exception in OO languages: Scope of local declarations inside a class declaration includes the whole class. public class { public int getValue() { return value; } int value; x y value Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Scope Hole Scope Hole: Declarations in nested blocks take precedence over the previous declarations. That is, binding becomes invisible/hidden. int x; void p(void) { char x; x = ‘a’; . . . } main() { x = 2; x (bound with integer data type) x (bound with character data type) Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Access Hidden Declarations scope resolution operator :: (C++) int x; void p(void) { char x; x = ‘a’; ::x=42; . . . } main() { x = 2; x (bound with character data type) x (bound with integer data type) the hidden integer variable x Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

 Hide a Declaration File 1: File 2: extern int x; int x; extern int x; static int x;  Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Symbol Table Symbol Table: maintain bindings. Can be viewed as functions that map names to their attributes. Names Attributes SymbolTable Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

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 5 - Semantics, Spring 2008 14 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Static Scope x y integer, global character, global 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 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Static Scope x y The symbol table in p: the bindings available in p 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 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Static Scope x y The symbol table in q: the bindings available in q 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 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Static Scope x y The symbol table in main: 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 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Static Scope The symbol table in previous slides are built during compilation The bindings are used in generating the machine code Result: 1 a E.g., semantics of q The symbol table in q: the bindings available in q x integer, global void q(void) { int y = 42; printf(“%d\n”,x); p(); } integer, local to q y character, global Lecture 5 - Semantics, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

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 5 - Semantics, Spring 2008 20 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(); x integer, 1, global y character, ‘a’, global Lecture 5 - Semantics, Spring 2008 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 main: the bindings available in main character, ‘b’, local to main x integer, 1, global y character, ‘a’, global Lecture 5 - Semantics, Spring 2008 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 q: the bindings available in q character, ‘b’, local to main x integer, 1, global 98 integer, 42, local to q y character, ‘a’, global Lecture 5 - Semantics, Spring 2008 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 98 * integer, 42, local to q y character, ‘a’, global Lecture 5 - Semantics, Spring 2008 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 5 - Semantics, Spring 2008 25 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008