Semantics. Names. Binding Time

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

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

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

CSE 307: Principles of Programming Languages

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

Object Oriented Languages. Hwansoo Han

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

MIDTERM EXAMINATION - CS130 - Spring 2003

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

CSE 3302 Programming Languages Lecture 5: Control

Lecture 16: Static Semantics Overview 1

CSE 431S Type Checking. Washington University Spring 2013

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

10. Abstract Data Types

Chapter 5. Names, Bindings, and Scopes

Names, Scopes, and Bindings II. Hwansoo Han

Final CSE 131B Spring 2004

CSCI312 Principles of Programming Languages!

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

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

CSC 533: Organization of Programming Languages. Spring 2005

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

CSE 307: Principles of Programming Languages

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Tutorial 5. Overflow. Data Types. Structures. Call stack. Stack frames. Debugging Tips. CS 136 Winter 2019 Tutorial 5 1

Scope. Chapter Ten Modern Programming Languages 1

Chapter 11 Introduction to Programming in C

CSCI312 Principles of Programming Languages!

Binding and Variables

Chapter 11 Introduction to Programming in C

CMSC 330: Organization of Programming Languages

Programming Languages Third Edition. Chapter 7 Basic Semantics

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

CS S-11 Memory Management 1

Chapter 11 Introduction to Programming in C

A Fast Review of C Essentials Part II

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

CSCE 314 Programming Languages. Type System

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Final CSE 131B Spring 2005

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

CS 314 Principles of Programming Languages

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Properties of an identifier (and the object it represents) may be set at

LECTURE 18. Control Flow

Imperative Languages!

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Type Analysis. Type Checking vs. Type Inference

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CLASSES AND OBJECT CHAPTER 04 CLASS XII

Chapter7 Expression and Assignment Statement. Introduction

Variables and Bindings

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

LECTURE 14. Names, Scopes, and Bindings: Scopes

HANDLING NONLOCAL REFERENCES

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Special Topics: Programming Languages

Introduction to Programming Using Java (98-388)

QUIZ. Can you find 5 errors in this code?

G Programming Languages - Fall 2012

Compiler construction

Names, Bindings, Scopes

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

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

G Programming Languages - Fall 2012

Informatica 3 Syntax and Semantics

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Chapter 11 Introduction to Programming in C

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Produced by. ICT Skills Summer School. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Functions & Memory Maps Review C Programming Language

Programming Language Dilemma Fall 2002 Lecture 1 Introduction to Compilation. Compilation As Translation. Starting Point

Fundamentals of Programming Languages

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

22c:111 Programming Language Concepts. Fall Types I

This exam is to be taken by yourself with closed books, closed notes, no calculators.


CPS 506 Comparative Programming Languages. Programming Language

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

6. Names, Scopes, and Bindings

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Introduction to C++ with content from

Building Interpreters

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Programming Languages

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Implementing Subprograms

CS 132 Compiler Construction, Fall 2011 Instructor: Jens Palsberg Multiple Choice Exam, Dec 6, 2011

MIDTERM EXAMINATION - CS130 - Spring 2005

Type Bindings. Static Type Binding

Transcription:

/24/ CSE 3302 Programming Languages Semantics Chengkai Li, Weimin He Spring Names Names: identif language entities variables, procedures, functions, constants, data tpes, Attributes: properties of names Eamples of attributes: Data tpe: int n = 5; (datatpe:integer) tpe: int itself is a name Value: ( value: 5) Location: int* ; = new int; Parameters, return value: int f(int n) {... Chengkai Li, Weimin He, Chengkai Li, Weimin He, 2 Binding Binding: associating attributes to names declarations assignments declarations (prototpe) and definition of a function The bindings can be eplicit or implicit e.g. int ; Eplicit binding: the data tpe of Implicit binding: the location of (static or dnamic, depending on where the declaration is) Binding Time Binding Time: the time when an attribute is bound to a name. Static binding (static attribute): occurs before eecution Language definition/implementation time: Therangeofdatatpeint tpe translation time (parsing/semantic analsis): The data tpe of a variable link time: The bod of eternal function load time: Location of global variable Dnamic binding (dnamic attribute): occurs during eecution entr/eit from procedure or program: the value of local variables Chengkai Li, Weimin He, 3 Chengkai Li, Weimin He, 4 Where can declarations happen? Blocks ({, begin-end, Algol descendants: C/C++, Java, Pascal, Ada, ) e.g., C Function bod Anwhere a statement can appear (compound statement) Eternal/global Structured data tpe Class Chengkai Li, Weimin He, 5 C++ Eample const int Maimum = 00; 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; Chengkai Li, Weimin He, 6

/24/ Scope of Binding Scope of Binding: the region of the program where the binding is maintained (is valid, applies). Block-structured language leical scope (static scope): from the declaration to the end of the block containing the declaration. dnamic scope : introduced later. Chengkai Li, Weimin He, 7 int ; char ; { int i; double z; int w[0]; w i z Eample p q main Chengkai Li, Weimin He, 8 int ; char ; Declaration before Use Eception in OO languages: Scope of local declarations inside a class declaration includes the whole class. public class { public int getvalue() { return value; int value; Chengkai Li, Weimin He, value 9 Scope Hole Scope Hole: Declarations in nested blocks take precedence over the previous declarations. That is, binding becomes invisible/hidden. int ; char ; = a ; = 2; (bound with character data tpe) Chengkai Li, Weimin He, (bound with integer data tpe) 0 Access Hidden Declarations Hide a Declaration scope resolution operator :: (C++) int ; File : File 2: etern int ; int ; char ; = a ; (bound with ::=42; (bound with character data tpe) integer data tpe) the hidden integer variable = 2; Chengkai Li, Weimin He, File : File 2: etern int ; static int ; CSE3302 Programming Languages, UT-Arlington Chengkai Li, Weimin He, 2 2

/24/ Smbol Table Smbol Table: maintain bindings. Can be viewed as functions that map names to their attributes. Names SmbolTable Chengkai Li, Weimin He, Attributes 3 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, 4 int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); int = ; 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 ; Chengkai Li, Weimin He, 5 char = b ; Character, global Chengkai Li, Weimin He, 6 int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in q: integer, local to q char = b ; Chengkai Li, Weimin He, 7 int = ; 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 ; Chengkai Li, Weimin He, 8 3

/24/ The smbol table in previous slides are built during compilation The bindings are used in generating the machine code Result: a E.g., semantics of q int = 42; printf( %d\n,); Chengkai Li, Weimin He, The smbol table in q: integer, local to q 9 Practice for Point Point 2 Point 3 int,; void g(void) { = + ; = + ; void f(void) { int ; = + ; = + ; = ; = 2; f(); printf("=%d,=%d\n",,); Question : Draw the smbol table at the given points in the program, using static Question 2: What does the program print, using static 20 Chengkai Li, Weimin He, What if dnamic scope is used? int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); char = b ; Chengkai Li, Weimin He, 2 What if dnamic scope is used? int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); The smbol table in main: the bindings available in main char = b ; Chengkai Li, Weimin He, 22 What if dnamic scope is used? int = ; char = a ; double =2.5; printf( %c\n,); int = 42; printf( %d\n,); 98 The smbol table in q: integer, 42, local to q char = b ; Chengkai Li, Weimin He, 23 What if dnamic scope is used? int = ; 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 integer, 42, local to q char = b ; Chengkai Li, Weimin He, 24 4

/24/ Practice for Dnamic Scope Point Point 2 Point 3 int,; void g(void) { = + ; = + ; void f(void) { int ; = + ; = + ; = ; = 2; f(); printf("=%d,=%d\n",,); Question : Draw the smbol table at the given points in the program, using dnamic Question 2: What does the program print, using dnamic 25 Chengkai Li, Weimin He, 5