Principles of Programming Languages

Similar documents
References and pointers

CS 314 Principles of Programming Languages

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

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

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Concepts Introduced in Chapter 7

Chapter 5 Names, Bindings, Type Checking, and Scopes

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Weeks 6&7: Procedures and Parameter Passing

Data Types In Text: Ch C apter 6 1

CSE 307: Principles of Programming Languages

G Programming Languages - Fall 2012

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

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

Semantic analysis. Semantic analysis. What we ll cover. Type checking. Symbol tables. Symbol table entries

(Refer Slide Time: 1:36)

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programmiersprachen (Programming Languages)

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Exam 3 Chapters 7 & 9

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

TYPES, VALUES AND DECLARATIONS

... Lecture 12. Pointers

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

CPSC 427a: Object-Oriented Programming

CS 314 Principles of Programming Languages. Lecture 13

CSE 3302 Programming Languages Lecture 5: Control

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

LECTURE 17. Expressions and Assignment

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

POINTER & REFERENCE VARIABLES

Launchpad Lecture -10

Section we will not cover section 2.11 feel free to read it on your own

Final-Term Papers Solved MCQS with Reference

Rvalue References as Funny Lvalues

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CSE 307: Principles of Programming Languages

CS 415 Midterm Exam Spring SOLUTION

Pointers, Dynamic Data, and Reference Types

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Special Topics: Programming Languages

CS:3820 Programming Language Concepts

CS 314 Principles of Programming Languages

Parameters. However, one important class of variables is still missing Þ parameters. Terminology: Example: Java, C, C++ Chap 18.

G Programming Languages - Fall 2012

NOTE: Answer ANY FOUR of the following 6 sections:

Types. What is a type?

Chapter 5 Names, Binding, Type Checking and Scopes

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Programming Languages

Memory Management and Run-Time Systems

UNIT V Sub u P b ro r g o r g a r m a s

Implementing Subprograms

CSE 452: Programming Languages. Where are we? Types: Intuitive Perspective. Data Types. High-level Programming Languages.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Lecture08: Scope and Lexical Address

Programming Languages

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Programming Languages

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

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

Informatica 3 Syntax and Semantics

Software II: Principles of Programming Languages. Why Expressions?

Programming Languages, Summary CSC419; Odelia Schwartz

Programs as Data 6 Imperative languages, environment and store, micro-c

Control Flow February 9, Lecture 7

Lectures Basics in Procedural Programming: Machinery

Alias Analysis. Last time Interprocedural analysis. Today Intro to alias analysis (pointer analysis) CS553 Lecture Alias Analysis I 1

6. Names, Scopes, and Bindings

Object-Oriented Principles and Practice / C++

Administration CS 412/413. Advanced Language Support. First-class vs. Second-class. First-class functions. Function Types

Programming Languages: Lecture 12

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Programming Languages

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

G Programming Languages - Fall 2012

CSC 533: Organization of Programming Languages. Spring 2005

Chapter 5. Names, Bindings, and Scopes

known as non-void functions/methods in C/C++/Java called from within an expression.

Review of the C Programming Language

PROGRAMMAZIONE I A.A. 2017/2018

The New C Standard (Excerpted material)

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Variation of Pointers

The PCAT Programming Language Reference Manual

A Taxonomy of Expression Value Categories

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Names, Bindings, Scopes

Today s lecture. Continue exploring C-strings. Pointer mechanics. C arrays. Under the hood: sequence of chars w/ terminating null

Principles of Programming Languages

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

MYcsvtu Notes LECTURE 34. POINTERS

Pointers and Dynamic Memory Allocation

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

C++ for Java Programmers

The paramaterless ctor (aka default ctor)

Compiler Construction

Transcription:

Principles of Programming Languages Lecture 10 Variables and Assignment 1

Variable Variable (Math) (CS) Math π CS pi symbol 3.1415828 R definite singular description 3.14 referent symbol R indefinite singular description referent e.g. Prolog & FP (IFP) e.g. Pascal & Algol 68 2

Constants Algol 68: pi = 3.1416; C: (not quite) #define PI 3.1416 Pascal: const pi = 3.1416; Ada: pi: constant FLOAT := 3.1416; pi 3.1416 3.1416 3.1416 same logical status as a literal No reference available No object eists pi replaced by literal No stack (or any other) space allocated (stored in instruction) Pure definition 3

Variables Algol 68: begin,y; C: { float,y; Pascal: var,y: ; Ada:,y: FLOAT; y alloc on stack := 2.0; y := 3.0; 3.0 2.0 4

Assignment (after y gets 3) Algol 68: := y; C: = y; Bliss:.y y 3.0 3.0 5

Variables: Bliss v. C Bliss Eplicit dereferencing Contet dereferencing C (lvalue). =.y +.z a +.j.(a +.i) const (rvalue) = y + z a[j] = a[i] lvalue rvalue Synonym: *(a+j) = *(a+i) { Only lvalue is actually computed 6

Algol 68 aside: identity declaration := c NOT same as = c Identity declaration = 2.718; means 2.718 Initialized variable := 2.718; means 2.718 Why? Implicit local (stack) allocation 7

identity declaration (cont d) ; abbreviates loc ; which abbreviates the following identity declaration: = loc ; Local generator Yields a (lvalue) Ascribed to identifier 8

Initialized Variables Algol 68: begin p := 3.1416; C: { float p = 3.1416; Pascal: var p: ; begin p := 3.1416; Ada: p: FLOAT := 3.1416; p What is ``variable about a variable?! Not reference (location, lvalue) Ref cannot be changed; ``ascribed to or ``possessed by ident! Not identifier! Not type (in most languages)! The value (rvalue) 3.1416 stack 9

Algol 68 identity declaration: unifies variable m & constant declaration 1. loc m := 3; 2. = m; 3 m 3 3 3. = m; m 3 10

Algol 68 identity declaration (cont d) 4. := m; loc :=m; = loc := m; 3 3 5. t = *m; 9 t const. In new environment *m evaluated when declaration encountered 11

Pointers Refs to variable refs Algol 68: p ; C: float *p ; Pascal: var p: ^; Ada: type PTR is access FLOAT; p : PTR; (PTR=``access type to ``base type ) p ref 12

Algol 68 pointer declaration p ref p = loc ; ref stack 13

Pointers (cont d) Algol 68: p := ; C: p = & ; Bliss: p p ref stack 2.0 NOT Pascal, Ada picture. Closest is:! Pascal: new(p); p^ := ; Ada: p := new FLOAT(); ``anonymous variable 14

Pointers in Pascal/Ada: point into heap Defeats ``dangling pointers at block eit Pointers cannot point into the stack var : ; var p, py: ^; p py stack heap 2.0 15

Pascal/Ada pointers (cont d) new(p); p^ := ; p py stack heap 2.0 2.0 ``anonymous variable p^ Pascal/Ada pointers point only to ``unnamed data objects 16

Pascal/Ada pointers (cont d) := 4.0; new(py); py := p; 1. p py stack heap 2.0 4.0 17

Pascal/Ada pointers (cont d) := 4.0; py^ := p^; 2. p py stack heap 2.0 4.0 ``pointer value out of legal range 18

Pascal/Ada pointers (cont d) := 4.0; new(py); py^ := p^; 3. p py stack heap 2.0 2.0 4.0 19

Algol68 has a heap allocator ref p = heap ; h = heap ;! Abbreviated heap h; p stack h heap Pascal: lvalues of all var identifiers are in stack! lvalues of all anonymous variables are in the heap Algol68: no restriction! allocation orthogonal to declaration 20

Refs and Aliasing Algol68: py; py := p; p py stack p py 1101 1102 ``pointer aliasing 2.0 1100 1100 1100: 2.0 21

Refs and Aliasing (cont d) Algol 68: y = ; (identity declaration) y y no alloc! 1101 1101 2.0 1100: ``name aliasing 2.0 ``equivalencing (Fortran) 22

Bliss: untyped bit strings = local ; 2; local p; p p p p 0010 0010 ``ptr to stored in p picture after assignment. 0010 23

Semantics of Variable Uses: C v. Bliss On LHS = / On RHS C: = & Meaning of contet-sensitive Not ``referentially transparent Bliss:. Meaning of is ``referentially transparent. 24

Variable Uses: C v. Bliss (cont d) C: & not a unary operator! &(+y) meaningless! && meaningless (can t reverse a pointer)! &2 meaningless Bliss:. is a unary operator!.(+y) sensible!.. sensible!.2 sensible 2 = 0010.2 0010: 1101..2 1101: 1000 25

C * Bliss. Bliss: C: On RHS. lvalue of rvalue of * Bliss variable occurrences are ``referentially transparent Semantics of is independent of contet 26