CSCE 330 Programming Language Structures Operational Semantics Slides





























- Slides: 29
CSCE 330 Programming Language Structures Operational Semantics (Slides mainly based on Chapter 2 of Ghezzi and Jazayeri) Fall 2011 Marco Valtorta and Jingsong Wang mgv@cse. sc. edu Sentences are not a mixture of words. Sentences are articulated. (Wittgenstein) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Declarations, Expressions, Commands • A command is executed to update variables and perform I/O • An expression is evaluated to yield a value • A declaration is elaborated (at compile time) to produce bindings. It may also have the side effect of allocating and initializing variables UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Binding • Binding is the specification of the attribute for a programming language entity – Entities: variables, statements, subprograms, declarations, etc. – Attributes for a variable: name, type, storage area, etc. • Binding times: – Language definition time – Language implementation time – Compile time – Run time • Example: the Fortran type INTEGER is bound partly at language definition time and partly at language implementation time • Static binding is established before run time, cannot be changed during program execution • Dynamic binding can be changed during program execution UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Variables • We consider variables for imperative languages • Abstraction of memory cells; each cell has an address • Most variables have six attributes: name, scope, lifetime, type, l-value, r-value – Some variables have no name • The lifetime of a variable is the length of time during which a storage area is bound to a variable • The type of a variable is the range of values the variable can take, together with operations to create, access, and modify values UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Variable name and scope The scope of a variable is the range of program instructions over which the variable is known and manipulable UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Static vs. Dynamic Scoping UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
User-defined Data Types Warning: this program uses pointer arithmetic! UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Pointers to Unnamed Variables UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Routines: Procedures and Functions UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Declaration, Definition, and Mutual Recursion UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Generic Routines UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Overloading and Aliasing • Operators are overloaded in almost all programming languages – E. g. , + is bound to integer or float addition depending on the arguments it is applied to • Two names are aliases if they denote the same entity at the same program point. E. g. , i and j are aliases in the C program fragment below: – int x = 0; – int *i = &x; – int *j = &x; UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
The SIMPLESEM Machine UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
A Static Language (C 1) Program UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Initial State UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Adding Simple Routines UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
A Snapshot UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Separate Compilation UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
A Language with Recursion: C 3 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Data Memory as a Stack of ARs UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
SIMPLESEM Code for C 3 Example UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Snapshots of Data Memory UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Block Structure I: Nested Blocks UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Block Structure II: Locally Declared Routines UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Call and Return for C 4’’ UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Dynamic Scope: C 5 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Parameter Passing UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering
Routine Parameters UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering