Statement Basics. Assignment Statements

Similar documents
11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

10/30/18. Dynamic Semantics DYNAMIC SEMANTICS. Operational Semantics. An Example. Operational Semantics Definition Process

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Programming Languages: Lecture 11

Chapter 9. Subprograms

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

9. Subprograms. 9.2 Fundamentals of Subprograms

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Chapter 9 Subprograms

Organization of Programming Languages CS 3200/5200N. Lecture 09

G Programming Languages - Fall 2012

Chapter 9. Subprograms

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

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

Implementing Subroutines. Outline [1]

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Chapter 9 Subprograms

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Chapter 10. Implementing Subprograms ISBN

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Programming Languages: Lecture 12

Subprogram Concept. COMP3220 Principle of Programming Languages. Zhitao Gong Spring

Programming Languages

Chap. 8 :: Subroutines and Control Abstraction

Type Bindings. Static Type Binding

Implementing Subprograms

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

Chapter 10. Implementing Subprograms

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

CS 314 Principles of Programming Languages

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

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

Run-Time Data Structures

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Extrinsic Procedures. Section 6

Chapter 5. Names, Bindings, and Scopes

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

Chapter 9 :: Subroutines and Control Abstraction

NOTE: Answer ANY FOUR of the following 6 sections:

Informatica 3 Syntax and Semantics

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

IA010: Principles of Programming Languages

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Type Checking Binary Operators

G Programming Languages - Fall 2012

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

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

Special Topics: Programming Languages

G Programming Languages - Fall 2012

Review of the C Programming Language

References and pointers

Lecture 16: Static Semantics Overview 1

Chapter 3. Describing Syntax and Semantics

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

5. Semantic Analysis!

Principles of Programming Languages

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

Control Abstraction. Hwansoo Han

Binding and Variables

1 Lexical Considerations

Weeks 6&7: Procedures and Parameter Passing

Qualifying Exam in Programming Languages and Compilers

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Chapter 3:: Names, Scopes, and Bindings (cont.)

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Chapter 3:: Names, Scopes, and Bindings (cont.)

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

CS 415 Midterm Exam Spring SOLUTION

Implementing Subprograms

Parameter Binding. Value: The formal parameter represents a local variable initialized to the value of the corresponding actual parameter.

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Lexical Considerations

Expression Evaluation and Control Flow. Outline

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

CSC 533: Organization of Programming Languages. Spring 2005

Memory Management and Run-Time Systems

Programmiersprachen (Programming Languages)

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Principles of Programming Languages. Lecture Outline

Chapter 5 Names, Binding, Type Checking and Scopes

CS4215 Programming Language Implementation

INSTITUTE OF AERONAUTICAL ENGINEERING

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 5 Names, Bindings, Type Checking, and Scopes

Informatica 3 Marcello Restelli

Lexical Considerations

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Programming Languages, Summary CSC419; Odelia Schwartz

Hybrid Verification in SPARK 2014: Combining Formal Methods with Testing

Transcription:

Statement Basics The meaning of a single statement executed in a state s is a new state s, which reflects the effects of the statement M stmt ( stmt, s) = s N. Meng, S. Arthur 1 Assignment Statements M a (x := E, s) Δ= s = {<i 1, v 1 >, <i 2, v 2 >,..., <i n,v n >}, where for j = 1, 2,..., n, v j = VARMAP(i j, s) if i j x v j = M e (E, s) if i j = x N. Meng, S. Arthur 2 1

Sequence of Statements M stmt ( stmt1; stmt2, s) Δ= M stmt ( stmt2, M stmt ( stmt1, s)) or Mstmt ( stmt1; stmt2, s) = s where s = M stmt ( stmt1, s) s = M stmt ( stmt2, s ) N. Meng, S. Arthur 3 Sequence of Statements x := 5; y := x + 1; write(x * y); } P2 } P1 } P0 Initial state s 0 = <mem 0, i 0, o 0 > M stmt ( P 0, s 0 ) = M stmt ( P 1, M a ( x := 5, s 0 )) s 1 s 1 = <mem 1, i 1, o 1 > where VARMAP(x, s 1 ) = 5 VARMAP(z, s 1 ) = VARMAP(z, s 0 ) for all z x i 1 = i 0, o 1 = o 0 N. Meng, S. Arthur 4 2

Sequence of Statements x := 5; y := x + 1; write(x * y); } P2 } P1 } P0 M stmt ( P 1, s 1 ) = M stmt ( P 2, M a ( y := x + 1, s 1 )) s 2 s 2 = <mem 2, i 2, o 2> where VARMAP(y, s 2 ) = M e ( x + 1, s 1 ) = 6 VARMAP(z, s 2 ) = VARMAP(z, s 1 ) for all z y i 2 = i 1, o 2 = o 1 N. Meng, S. Arthur 5 Sequence of Statements x := 5; y := x + 1; write(x * y); } P2 } P1 } P0 M stmt ( P 2, s 2 ) = M stmt ( write(x * y), s 2 ) = s 3 s 3 = <mem 3, i 3, o 3> where VARMAP(z, s 3 ) = VARMAP(z, s 2 ) for all z i 3 = i 2, o 3 = o 2 M e ( x * y, s 2 ) = o 2 30 N. Meng, S. Arthur 6 3

Sequence of Statements Therefore, M stmt ( P, s 0 ) = s 3 = <mem 3, i 3, o 3 > where VARMAP(y, s 3 ) = 6 VARMAP(x, s 3 ) = 5 VARMAP(z, s 3 ) = VARMAP(z, s 0 ) for all z x, y i 3 = i 0 o 3 = o 0 30 N. Meng, S. Arthur 7 Logical Pretest Loops The meaning of the loop is the value of program variables after the loop body has been executed the prescribed number of times, assuming there have been no errors The loop is converted from iteration to recursion, where the recursion control is mathematically defined by other recursive state mapping functions Recursion is easier to describe with mathematical rigor than iteration N. Meng, S. Arthur 8 4

Logical Pretest Loop M l (while B do L, s) Δ= if M b (B, s) = false then s else M l (while B do L, M stmt (L, s)) N. Meng, S. Arthur 9 Postest Loop? M ptl (do L until not B, s) Δ =? N. Meng, S. Arthur 10 5

Key Points of Denotational Semantics Advantages Compact & precise, with solid mathematical foundation Provide a rigorous way to think about programs Can be used to prove the correctness of programs Can be an aid to language design N. Meng, S. Arthur 11 Key Points of Denotational Semantics Disadvantages Require mathematical sophistication Hard for programmer to use Uses Semantics for Algol-60, Pascal, etc. Compiler generation and optimization N. Meng, S. Arthur 12 6

Summary Each form of semantic description has its place Operational semantics Informally describe the meaning of language constructs in terms of their effects on an ideal machine Denotational semantics Formally define mathematical objects and functions to represent the meanings N. Meng, S. Arthur 13 Subroutine In Text: Chapter 9 7

Outline [1] Definitions Design issues for subroutines Parameter passing modes and mechanisms Advanced subroutine issues N. Meng, S. Arthur 15 Subroutine A sequence of program instructions that perform a specific task, packaged as a unit The unit can be used in programs whenever the particular task should be performed N. Meng, S. Arthur 16 8

Subroutine Subroutines are the fundamental building blocks of programs They may be defined within programs, or separately in libraries that can be used by multiple programs In different programming languages, a subroutine may be called a procedure, a routine, a method, or a subprogram N. Meng, S. Arthur 17 Characteristics of Subroutines/ Subprograms Each subroutine has a single entry point The caller is suspended during the execution of the callee subroutine Control always returns to the caller when callee subroutine s execution terminates N. Meng, S. Arthur 18 9

Parameters A subroutine may be written to expect one or more data values from the calling program The expected values are called parameters or formal parameters The actual values provided by the calling program are called arguments or actual parameters N. Meng, S. Arthur 19 Actual/Formal Parameter Correspondence Two options Positional parameters In nearly all programming languages, the binding is done by position E.g., the first actual parameter is bound to the first formal parameter Keyword parameters Each formal parameter and the corresponding actual parameter are specified together E.g., Sort (List => A, Length => N) N. Meng, S. Arthur 20 10

Keyword Parameters Advantages Order is irrelevant When a parameter list is long, developers won t make the mistake of wrongly ordered parameters Disadvantages Users must know and specify the names of formal parameters N. Meng, S. Arthur 21 Default Parameter A parameter that has a default value provided to it If the user does not supply a value for this parameter, the default value will be used If the user does supply a value for the default parameter, the user-specified value is used N. Meng, S. Arthur 22 11

An Example in Ada procedure sort (list : List_Type; length : Integer := 100);... sort (list => A); N. Meng, S. Arthur 23 Design issues for subroutines What parameter passing methods are provided? Are parameter types checked? What is the referencing environment of a passed subroutine? Can subroutine definitions be nested? Can subroutines be overloaded? Are subroutines allowed to be generic? Is separate/independent compilation supported? N. Meng, S. Arthur 24 12

Parameter-Passing Methods Ways in which parameters are transmitted to and/or from callee subroutines Semantic models Implementation models N. Meng, S. Arthur 25 Semantic Models Formal parameters are characterized by one of three distinct semantic models In mode: They can receive data from the corresponding actual parameters Out mode: they can transmit data to the actual parameters Inout mode: they can do both N. Meng, S. Arthur 26 13

Models of Parameter Passing N. Meng, S. Arthur 27 An Example public int[] merge(int[] arr1, int[] arr2) { int[] arr = new int[arr1.length + arr2.length]; for (int i = 0; i < arr2.length; i++) { arr[i] = arr1[i]; arr2[i] = arr1[i] + arr2[i]; arr[i + arr1.length] = arr2[i]; } return arr; } Which parameter is in mode, out mode, or inout mode? N. Meng, S. Arthur 28 14

Implementation Models A variety of models have been developed by language designers to guide the implementation of the three basic parameter transmission modes Pass-by-value Pass-by-result Pass-by-value-result Pass-by-reference Pass-by-name N. Meng, S. Arthur 29 Pass-by-Value The value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable in the subprogram Implement in-mode semantics Implemented by copy N. Meng, S. Arthur 30 15

Pros and Cons Pros Fast for scalars, in both linkage cost and access time No side effects to the parameters Cons Require extra storage for copying data The storage and copy operations can be costly if the parameter is large, such as an array with many elements N. Meng, S. Arthur 31 Pass-by-Result No value is transmitted to a subroutine The corresponding formal parameter acts as a local variable, whose value is transmitted back to the caller s actual parameter E.g., void Fixer(out int x, out int y) { x = 17; y = 35; } Implement out-mode parameters N. Meng, S. Arthur 32 16

Pros and Cons Pros Same as pass-by-value Cons The same cons of pass-by-value Parameter collision E.g., Fixer(x, x), what will happen? If the assignment statements inside Fixer() can be reordered, what will happen? N. Meng, S. Arthur 33 Pass-by-Value-Result A combination of pass-by-value and passby-result, also called pass-by-copy Implement inout-mode parameters Two steps The value of the actual parameter is used to initialize the corresponding formal parameter The formal parameter acts as a local variable, and at subroutine termination, its value is transmitted back to the actual parameter N. Meng, S. Arthur 34 17

Pros and Cons Pros Same as pass-by-reference, which is to be discussed next Cons Same as pass-by-result N. Meng, S. Arthur 35 Pass-by-Reference A second implementation model for inout-mode parameters Rather than copying data values back and forth, it shares an access path, usually an address, with the caller E.g., void fun(int &first, int &second) N. Meng, S. Arthur 36 18

Pros and Cons Pros Passing process is efficient in terms of time and space Cons Access to the formal parameters is slower than pass-by-value parameters due to indirect access via reference Side effects to parameters Aliases can be created N. Meng, S. Arthur 37 19