CSE 3302 Programming Languages

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Intermediate Code Generation
The University of Adelaide, School of Computer Science
Programming Languages and Paradigms
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
CSE 452: Programming Languages Expressions and Control Flow.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Structure of Programming Language
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Basic Semantics Associating meaning with language entities.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Programming Languages and Paradigms Imperative Programming.
Exam Delay Exam II will be held during the first fifty (50) minutes of class on 13 November 2003.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSE 3302 Programming Languages
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Control I Expressions and Statements Lecture 9 – Control I, Spring CSE3302 Programming.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Information and Computer Sciences University of Hawaii, Manoa
Functions.
Chapter 7 User-Defined Methods.
Chapter 7: Expressions and Assignment Statements
CSE 3302 Programming Languages
Operators And Expressions
Expressions and Assignment
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 7: Expressions and Assignment Statements
CSE 3302 Programming Languages
Programmazione I a.a. 2017/2018.
User-Defined Functions
Flow of Control.
CSE 3302 Programming Languages
Chap. 6 :: Control Flow Michael L. Scott.
Flow of Control.
CSE 3302 Programming Languages
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
Dynamic Scoping Lazy Evaluation
Chap. 6 :: Control Flow Michael L. Scott.
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
CSE 3302 Programming Languages
Semantic Analysis Chapter 6.
Languages and Compilers (SProg og Oversættere)
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Chap 7. Advanced Control Statements in Java
OPERATORS in C Programming
CSC215 Lecture Control Flow.
Controlling Program Flow
OPERATORS in C Programming
CSE 3302 Programming Languages
Presentation transcript:

CSE 3302 Programming Languages Control I Expressions and Statements Chengkai Li Fall 2007 Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Control Control: what gets executed, when, and in what order. Abstraction of control: Expression Statement Exception Handling procedures and functions Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Expression vs. Statement In pure (mathematical) form: Expression: no side effect return a value Statement: side effect no return value functional languages aim at achieving this pure form No clear-cut in most languages Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Expression Constructed recursively: Basic expression (literal, identifiers) Operators, functions, special symbols Number of operands: unary, binary, ternary operators Operator, function: equivalent concepts (3+4)*5 (infix notation) mul( add(3,4), 5) “*”(“+”(3,4),5) (Ada, prefix notation) (* (+ 3 4) 5) (LISP, prefix notation) Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Postfix notation PostScript: %!PS /Courier findfont 20 scalefont setfont 72 500 moveto (Hello world!) show showpage http://en.wikipedia.org/wiki/PostScript Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Expression and Side Effects changes to memory, input/output Side effects can be undesirable But a program without side effects does nothing! Expression: No side effect: Order of evaluating subexpressions doesn’t matter (mathematical forms) Side effect: Order matters Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Applicative Order Evaluation (Strict Evaluation) Evaluate the operands first, then apply operators (bottom-up evaluation) (subexpressions evaluated, no matter whether they are needed) But is 3+4 or 5-6 evaluated first? * - 4 + 3 6 5 Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Order Matters C: int x=1; int f(void) { x=x+1; return x; } main(){ printf(“%d\n”, x + f()); return 0; Java: class example { static int x = 1; public static int f() { x = x+1; return x; } public static void main(String[] args) { System.out.println(x+f()); } } 4 3 Many languages don’t specify the order, including C, java. C: usually right-to-left Java: always left-to-right, but not suggested to rely on that. Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Expected Side Effect Assignment (expression, not statement) x = (y = z) (right-associative operator) x++, ++x int x=1; int f(void) { return x++; } main(){ printf(“%d\n”, x + f()); return 0; Why? Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Sequence Operator (expr1, expr2, …, exprn) Left to right (this is indeed specified in C) The return value is exprn x=1; y=2; x = (x=x+1, y++, x+y); printf(“%d\n”,x); Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Non-strict evaluation Evaluating an expression without necessarily evaluating all the subexpressions. short-circuit Boolean expression if-expression, case-expression Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Short-Circuit Evaluation if (false and x) … if (true or x)… No need to evaluate x, no matter x is true or false What is it good for? if (i <=lastindex and a[i]>=x)… if (p != NULL and p->next==q)… Ada: allow both short-circuit and non short-circuit. if (x /= 0) and then (y/x > 2) then ... if (x /= 0) and (y/x > 2) then ... ? if (ptr = null) or else (ptr.x = 0) then ... if (ptr = null) or (ptr.x = 0) then ... ? Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 if-expression if (test-exp, then-exp, else-exp) ternary operator test-exp is always evaluated first Either then-exp or else-exp are evaluated, not both if e1 then e2 else e3 (ML) e1 ? e2 : e3 (C) Different from if-statemnt? Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 case-expression ML: case color of red => “R’’ | blue => “B’’ | green => “G” | _ => “AnyColor”; Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Normal order evaluation (lazy evaluation) When there is no side-effect: Normal order evaluation (Expressions evaluated in mathematical form) Operation evaluated before the operands are evaluated; Operands evaluated only when necessary. int double (int x) { return x+x; } int square (int x) { return x*x; } Applicative order evaluation : square(double(2)) = … Normal order evaluation : square(double(2)) = … Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 What is it good for? (p!=NULL) ? p->next : NULL int if_exp(bool x, int y, int z) { if (x) return y; else return z; } if_exp(p!=NULL, p->next, NULL); With side effect, it may hurt you: int get_int(void) { int x; scanf(“%d” &x); return x; Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Examples Call by Name (Algol60) Macro #define swap(a, b) {int t; t = a; a = b; b = t;} What are the problems here? Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Unhygienic Macros Call by Name (Algol60) Macro #define swap(a, b) {int t; t = a; a = b; b = t;} main (){ int t=2; int s=5; swap(s,t); } main (){ int t=2; int s=5; {int t; t = s; s = t; t = t;} } #define DOUBLE(x) {x+x;} main() { int a; a = DOUBLE(get_int()); printf("a=%d\n", a); } main() { int a; a = get_int()+get_int(); printf("a=%d\n", a); } Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Statements If-statements, case-(switch-)statements, loops Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 GOTO Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Exception Handling Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE 3302 Programming Languages Control II Procedures and Environments Chengkai Li Fall 2007 Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Procedures vs. Functions no side effect return a value Function call: expression Procedure: side effect, executed for it no return value Procedure call: statement No clear distinction made in most languages C/C++: void Ada/FORTRAN/Pascal: procedure/function Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Syntax Terminology: body specification interface Name type of return value parameters (names and types) int f(int y); //declaration int f(int y){ //definition int x; x=y+1; return x; } int f(int y) { int x; x=y+1; return x; } Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Procedure Call Caller: Callee: … int f(int y){ f(a); int x; … if (y==0) return 0; x=y+1; return x; } Control transferred from caller to callee, at procedure call Transferred back to caller when execution reaches the end of body Can return early Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Environment Environment: binding from names to their attributes stack static(global) area heap (unallocated) automatically-allocated spaces (local variables, procedures (chapter 8) under the control of runtime system both for dynamic binding manually-allocated spaces under the control of programmer Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Activation Record for Nested Blocks Activation record: memory allocated for the local objects of a block Entering a block: activation record allocated Exit from inner block to surrounding block: activation record released int x; //global { int x,y; x = y*10; { int i; i = x/2; } } x x y Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Activation Record for Nested Blocks int x; //global { int x,y; x = y*10; { int i; i = x/2; } } x x y X: Nonlocal variable, in the surrounding activation record i Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } x x y Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } x x x: global variable in defining environment y i Need to retain information in calling environment Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } Can only access global variables in defining environment No direct access to the local variables in the calling environment (Need to communicate through parameters) x x y i Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Activation Record for Procedures Why not access global variables through parameters as well? Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007