Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.

Slides:



Advertisements
Similar presentations
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Advertisements

Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Foundations of Programming Languages: Introduction to Lambda Calculus
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Quantum One: Lecture 7. The General Formalism of Quantum Mechanics.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
EOPL3: Section 3.3 PROC and App B: SLLGEN
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
C H A P T E R TWO Syntax and Semantic.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CPS 506 Comparative Programming Languages Syntax Specification.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Abstract Syntax cs7100 (Prasad) L7AST.
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Corky Cartwright January 18, 2017
4 (c) parsing.
CSE 3302 Programming Languages
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
The Metacircular Evaluator
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Scoping and Binding of Variables
Procedures App B: SLLGEN 1.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
CSE S. Tanimoto Lambda Calculus
Recursive Procedures and Scopes
COMPILER CONSTRUCTION
Presentation transcript:

Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008

Variables What is a variable? A legal symbol without a pre-defined (reserved) meaning that can be bound to a value (and perhaps rebound to a different value) during program execution. –Examples in Scheme/Java x y z –Non-examples in Java + null true false 7f –Complication in Java: variables vs. fields What happens when the same name is used for more than one variable? –Example in Scheme: (lambda (x) (x (lambda (x) x))) We use scoping rules to distinguish them.

Some scoping examples Java: class Foo { static void doNothing() { int[] a =...; for int i = 0; i... } } What is the scope (part of the program where it can be accessed/referenced) of a? What is the scope of i?

Formalizing Scope Focus on language LC (from last lecture). Recall that LC is the language generated by the root symbol Exp in the following grammar Exp ::= Num | Var | (Exp Exp) | (lambda Var Exp) where Var is the set of alphanumeric identifiers excluding lambda and is the set of integers written in conventional decimal radix notation. If we interpret LC as a sub-language of Scheme, it contains only one binding construct: lambda-expressions. In (lambda (a-variable) an-expression) ‏  a-variable is introduced as a new, unique variable whose scope is the body an-expression of the lambda-expression (with the exception of possible "holes", which we describe in a moment).

Free and Bound Occurrences An important building block in characterizing the scope of variables is defining when a variable i occurs free in an expresssion. For LC, this notion is easy to define inductively. Definition (Free occurrence of a variable in LC): Let x,y range over the elements of Var. Let M, N range over the elements of Exp. Then x occurs free in: –y if x = y; – (lambda (y) M ) if x != y and x occurs free in – ( M N ) if it occurs free either in M or in N. The relation x occurs free in y is the least relation on LC expressions satisfying the preceding constraints. Definition: x occurs bound in M iff x does not occur free in M. It is straightforward but tedious to define when a particular occurrence of a variable x (identified by a path of tree selectors) is free or bound; the definition proceeds along similar lines to the definition of occurs free given above.

Static Distance Representation The choice of bound variable names in an expression is arbitrary (modulo ensuring distinct, potentially conflicting variables have distinct names). We can eliminate explicit variable names by using the notion of “relative addressing”: a variable reference simply has to identify which lambda abstraction introduces the variables to which it refers. We can number the lambda abstractions enclosing a variable occurrence 1, 2,... and simply use these indices instead of variable names. Since LC includes integer constants, we will embolden the indices referring to variables to distinguish them from integer constants. Examples: – (lambda (x) x) ---> (lambda 1) ‏ – (lambda (x) (lambda (y) (lambda (z) ((xz)(yz))))) ---> (lambda (lambda (lambda ((3 1)(2 1))))) ‏

A Simple Example Exp ::= Num | Var | (Exp Exp) | (lambda Var Exp) ‏ Num is the set of numeric constants (given in the lexer spec)‏ Var is the set of variable names (given in the lexer spec)‏ To represent this syntax as trees (abstract syntax) in Scheme ; exp := (make-num number) + (make-var symbol) + (make-app exp exp) + ; (make-proc symbol exp) ‏ (define-struct (num n)) (define-struct (var s)) (define-struct (app rator rand)) (define-struct (proc param body)) ;; param is a symbol not a var! app represents a function application proc represents a function definition

Top Down (Predictive) Parsing Idea: design the grammar so that we can always tell what rule to use next starting from the root of the parse tree by looking ahead some small number of token (LL(k) parsing). Can easily be implemented by hand: called recursive descent. Conceptual aid: syntax diagrams.