Chapter 5 Basic Semantics

Slides:



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

Programming Languages and Paradigms
Names and Bindings.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter 5 Names, Bindings, and Scopes
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Basic Semantics.
ISBN Chapter 10 Implementing Subprograms.
Programming Languages Third Edition
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Chapter 5K. Louden, Programming Languages1 Chapter 5 Basic Semantics.
ISBN Chapter 10 Implementing Subprograms.
Variables Six properties: Binding times of properties:
Run-Time Storage Organization
Run time vs. Compile time
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Names, Bindings, and Scopes
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.
ISBN Chapter 10 Implementing Subprograms.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Chapter 8 - Control II: Procedures and Environments
Basic Semantics Attributes, Bindings, and Semantic Functions
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
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.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
© Kenneth C. Louden, Chapter 5 - Basic Semantics Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Names, Scope, and Bindings Programming Languages and Paradigms.
ISBN Chapter 10 Implementing Subprograms.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Object Lifetime and Pointers
Chapter 10 : Implementing Subprograms
Data Types In Text: Chapter 6.
Implementing Subprograms Chapter 10
Implementing Subprograms
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
Names, Bindings, and Scopes
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Names, Bindings, and Scopes
Implementing Subprograms
Implementing Subprograms
Presentation transcript:

Chapter 5 Basic Semantics K. Louden, Programming Languages

Ways to specify semantics Language reference manual – commonly used, but lack of precision By a defining translator – questions cannot be answered in advance – only by trying it By formal definition – denotational semantics – complex and abstract A fundamental step in defining semantics is to describe the meaning of each identifier. Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Attributes Properties of language entities, especially identifiers used in a program. Important examples: Value of an expression Data type of an identifier Maximum number of digits in an integer Location of a variable Code body of a function or method Declarations ("definitions") bind attributes to identifiers. Different declarations may bind the same identifier to different sets of attributes. Chapter 5 K. Louden, Programming Languages

Binding times can vary widely: Value of an expression: during execution or during translation (constant expression). Data type of an identifier: translation time (C++) or execution time (Ruby). Maximum number of digits in an integer: language definition time or language implementation time. Location of a variable: load or execution time. Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Two general categories of binding: static (prior to execution) and dynamic Interpreters will perform most bindings dynamically Concern is earliest time when it COULD be bound, not when it actually is Possible times Language definition Language implementation Translation time Link time Load time Execution time - dynamic }static Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Classes of Binding Times (listed from late to early) 1. Execution Time (Late Binding). Variables to their values. Variables to a particular storage location (termed dynamic storage allocation). At entry to a subprogram or block. Example: formal to actual parameters and formal parameters to actual locations. At arbitrary points during execution. Example: variables to values. In some languages, variables to types. Consider Prolog - variable type is determined dynamically 2. Load time: globals bound to location 3. Link time: body of external function bound to call instruction 4. Compile time (Early Binding). Bindings chosen by programmer. Variable names, types, names. Bindings chosen by translator. Example: variables to storage (load time) (termed static storage allocation). Example: particular machine instruction for a statement. Example: initial values of variables (if none specified) Example: in C declaration defines type by gives no space 5. Language Implementation Time. Example: Association of enumerated type values with integers. Example: maxint 6. Language definition Time - probably the most important binding time. Example: structure of language fixed, set of all basic data types, set of statements: syntax and semantics fixed, predefined types. Chapter 5 K. Louden, Programming Languages

Symbol table and environment A dictionary or table is used to maintain the identifier/attribute bindings. It can be maintained either during translation (symbol table) or execution (environment) or both. Pre-translation entities are entered into the initial or default table. If both are maintained, the environment can usually dispense with names, keeping track only of locations (names are maintained implicitly). Chapter 5 K. Louden, Programming Languages

Example of Environment struct People { ... } x; int f ( int a) { double y; int x; ... Point 1 } int main () { double a; ... Point 2 Point 1: struct People → type, x→int, f → func, a → int, y → double Point 2: x → struct People, f → func, a →double, main → func Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Scope The scope of a declaration is the region of the program to which the bindings established by the declaration apply. Informally - Scope of a variable: range of statements in which the variable is visible A variable is visible in a statement if it can be referenced in that statement. (Scope holes caused by new declarations) In a block-structured language, the scope is typically the code from the end of the declaration to the end of the "block" (indicated by braces {…} in C and Java) in which the declaration occurs. Scope can extend backwards to the beginning of the block in certain cases (class declarations in Java and C++, top-level declarations in Scheme). Chapter 5 K. Louden, Programming Languages

Lexical vs. dynamic scope Scope is maintained by the properties of the lookup operation in the symbol table or environment. If scope is managed statically (prior to execution), the language is said to have static or lexical scope ("lexical" because it follows the layout of the code in the file). If scope is managed directly during execution, then the language is said to have dynamic scope. It is possible to maintain lexical scope during execution - via static links in the call stack. Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Java scope example public class Test { public static int x = 2; public static void f() { System.out.println(x); q(); // is q known? Two-Pass } public static void q(String[] args) { int x = 0; int y = 4 System.out.println(y); } public static void main(String[] args) { int x = 3; f(); } } This prints 2 4, but under dynamic scope it would print 3 4(the most recent declaration of x in the execution path is found). Chapter 5 K. Louden, Programming Languages

Dynamic scope evaluated Almost all languages use lexical scope (including Ruby). Why do you think that is true? With dynamic scope the meaning of a variable cannot be known until execution time, thus there cannot be any static checking. Originally used in Lisp. Scheme could still use it, but doesn't. Some languages still use it: VBScript, Javascript, Perl (older versions). Lisp inventor (McCarthy) now calls it a bug. Still useful as a pedagogical tool to understand the workings of scope. Chapter 5 K. Louden, Programming Languages

Symbol table construction Symbol table is constructed as declarations are encountered (insert operation). Lookups occur as names are encountered In lexical scope, lookups occur either as names are encountered in symbol table to that point (declaration before use—C), or all lookups are delayed until after the symbol table is fully constructed and then performed (Java class—scope applies backwards to beginning of class). In dynamic scope, need links to tell you which declarations to use If a different scope applies (like Ruby code blocks), the environment needs to be passed. Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Overloading Overloading is a property of symbol tables that allows them to successfully handle declarations that use the same name within the same scope. It is the job of the symbol table to pick the correct choice from among the declarations for the same name in the same scope. This is called overload resolution. Overloading typically applies only to functions or methods. Overloading must be distinguished from dynamic binding in an OO language. Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages An example in Java: public class Overload { public static int max(int x, int y) { return x > y ? x : y;} public static double max(double x, double y) { return x > y ? x : y;} public static int max(int x, int y, int z) { return max(max(x,y),z);} public static void main(String[] args) { System.out.println(max(1,2)); System.out.println(max(1,2,3)); System.out.println(max(4,1.3)); }} Adding more max functions that mix double and int parameters is ok. But adding ones that mix double and int return values is not! Chapter 5 K. Louden, Programming Languages

Allocation of space for variables Can be constructed entirely statically (Fortran): all vars and functions have fixed locations for the duration of execution. Can also be entirely dynamic: functional languages like Scheme and ML. Names of constants may represent purely compile time quantities. Most languages use a mix: C, C++, Java, Ada. Consists of three components: A fixed area for static allocation A stack area for lifo allocation (usually the processor stack) A "heap" area for on-demand dynamic allocation (with or without garbage collection) Chapter 5 K. Louden, Programming Languages

Typical environment organization [Figure 5.25, p. 165)] © 2003 Brooks/Cole - Thomson Learning™ Typical environment organization [Figure 5.25, p. 165)] Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages The Runtime Stack Used for: Procedure/function/method calls temporaries local variables Runtime stack is what overflows in infinite recursion Temporaries: intermediate results that cannot be kept in registers; not considered here further. Procedure calls: (parameters and return values) Local variables: part of calls, but can be considered independently, showing LIFO behavior for nested scopes (next slide). Chapter 5 K. Louden, Programming Languages

Typical Activation Record for a Language with Stack-Dynamic Local Variables

Implementing Subprograms with Stack-Dynamic Local Variables: Activation Record The activation record format is static, but its size may be dynamic The dynamic link points to the top of an instance of the activation record of the caller An activation record instance is dynamically created when a subprogram is called Run-time stack

An Example: C Function void sub(float total, int part) { int list[4]; float sum; … } [4] [3] [2] [1] [0]

An Example Without Recursion void A(int x) { int y; ... C(y); } void B(float r) { int s, t; A(s); void C(int q) { void main() { float p; B(p); main calls B B calls A A calls C

An Example Without Recursion The environment would be a pointer to an activation record in the runtime stack.

K. Louden, Programming Languages Heap Allocation In "standard" languages (C, C++, Java) heap allocation requires a special operation: new. Any kind of data can be allocated on the heap in C/C++; in Java all objects and only objects are allocated on the heap. the stack is still used to represent calls. In C/C++, deallocation is typically by hand (destructors), but it is hard to do right. Java uses a garbage collector that periodically sweeps the heap looking for data that cannot be accessed any more by the program and adding it back to free space. Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Lifetime The lifetime of a program entity is the duration of its allocation in the environment. Allocation is static when the lifetime is the duration of the entire program execution. Lifetime is related to but not identical to scope. With scope holes, lifetime can extend to regions of the program where the program entity is not accessible. It is there, but you can’t access it. Chapter 5 K. Louden, Programming Languages

Variables and Constants A variable is an object whose stored value can change during execution. x=y (we want value of y but address of x) referred to as l-value and r-value A constant is an object whose value does not change throughout its lifetime. Constants are often confused with literals: constants have names, literals do not. Chapter 5 K. Louden, Programming Languages

K. Louden, Programming Languages Aliases An alias occurs when the same object is bound to two different names at the same time. This is fairly common with C++ objects. Side Effect: changes in value that persists after execution. mathematical function: changes nothing, just returns a value imperative functions: can change parameters or other values Chapter 5 K. Louden, Programming Languages

Dangling References, and Garbage A dangling reference is a location that has been deallocated from the environment, but is still accessible within the program. Dangling references are impossible in a garbage-collected environment with no direct access to addresses. Garbage is memory that is still allocated in the environment but has become inaccessible to the program. Garbage can be a problem in a non-garbage collected environment, but is much less serious than dangling references. Chapter 5 K. Louden, Programming Languages