CSE 307: Principles of Programming Languages

Similar documents
CSE 504: Compilers. Expressions. R. Sekar

LECTURE 18. Control Flow

CSE 307: Principles of Programming Languages

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

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Chapter 7 Control I Expressions and Statements

Expressions vs statements

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Intermediate Code Generation

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

CSE302: Compiler Design

CSE P 501 Exam 12/1/11

Expressions and Statements. Department of CSE, MIT, Manipal

! Non-determinacy (unspecified order) Maria Hybinette, UGA 2. 1 Landin adding sugar to a language to make it easier to read (for humans)

CSE 307: Principles of Programming Languages

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

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

CSE P 501 Exam Sample Solution 12/1/11

CSCE 531, Spring 2015 Final Exam Answer Key

G Programming Languages - Fall 2012

Flow of Control Execution Sequence

Flow Control. CSC215 Lecture

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Compilers. Compiler Construction Tutorial The Front-end

APT Session 6: Compilers

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

CSE 582 Autumn 2002 Exam 11/26/02

APT Session 7: Compilers

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

Lecture 15: Iteration and Recursion

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Syntax. A. Bellaachia Page: 1

Chapter 8. Statement-Level Control Structures

Program Representations

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

CSE 307: Principles of Programming Languages

Java Basic Programming Constructs

M1-R4: Programing and Problem Solving using C (JAN 2019)

5 The Control Structure Diagram (CSD)

Languages and Compiler Design II IR Code Optimization

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

1 Lexical Considerations

CS293S Redundancy Removal. Yufei Ding

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Comments. Comments: /* This is a comment */

CS558 Programming Languages

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Introduction to C/C++ Lecture 3 - Program Flow Control

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

The SPL Programming Language Reference Manual

Imperative Paradigm. Syntax. Role of Programming Languages. Expressions. Expressions. Role of Programming Languages. Symbols.

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Control Flow. Stephen A. Edwards. Fall Columbia University

Iteration. Side effects

Syntax Errors; Static Semantics

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

CMSC 330: Organization of Programming Languages

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

A Short Summary of Javali

Low level programming (37-023)

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Scope, Functions, and Storage Management

Introduction to Parsing Ambiguity and Syntax Errors

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

CSE 582 Autumn 2002 Exam Sample Solution

Configuration Sets for CSX- Lite. Parser Action Table

Introduction to Parsing Ambiguity and Syntax Errors

CMSC330 Spring 2017 Midterm 2

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

ECE251 Midterm practice questions, Fall 2010

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

Introduction to Model Checking

Lexical Considerations

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

CS1100 Introduction to Programming

CS 432 Fall Mike Lam, Professor. Code Generation

CMPT 755 Compilers. Anoop Sarkar.

Exercise 1: Balanced Parentheses

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

A Simple Syntax-Directed Translator

Introduction to Computing Lecture 01: Introduction to C

Lexical Considerations

Processing control structures. Friday, October 4, 13

Copyright 2008 CS655 System Modeling and Analysis. Korea Advanced Institute of Science and Technology

n Group of statements that are executed repeatedly while some condition remains true

Part I Part 1 Expressions

Transcription:

1 / 18 CSE 307: Principles of Programming Languages Statements and Control Flow R. Sekar

2 / 18 Topics If-Then-Else 1. If-Then-Else

3 / 18 Control Statements Structured Control Statements: Case Statements: Implementation using if-then-else Understand semantics in terms of the semantics of simple constructs actual implementation in a compiler Loops while, repeat, for

4 / 18 Section 1 If-Then-Else

If-Then-Else If-then-else. It is in two forms: if cond then s1 else s2 if cond then s1 evaluate condition: if and only if evaluates to true, then evaluate s1 otherwise evaluate s2. Dangling else problem: if c1 then if c2 then s1 else s2 may be intrepreted as: if c1 then if c2 then s1 else s2 or if c1 then if c2 then s1 else s2 5 / 18

If-Then-Else (Continued) This ambiguity can be avoided by bracketing syntax: if cond then s1 fi if cond then s1 else s2 fi The above intended statements can be written as: if c1 then fi or if c2 then s1 else s2 fi if c1 then if c2 then s1 fi else s2 fi Another way to avoid ambiguity is to use: associate else with closest if that doesn t have else. This is used in most programming languages (C, C++ etc) 6 / 18

7 / 18 Case Statement Case statement switch(<expr>){ case <value> : case <value> :... default : } Evaluate <expr> to get value v. Evaluate the case that corresponds to v. Restriction: <value> has to be a constant of an original type e.g., int, enum Why?

Implementation of case statement Naive algorithm: Sequential comparison of value v with case labels. This is simple, but inefficient. It involves O(N) comparisons switch(e){ case 0:s0; case 1:s1; case 2:s2; case 3:s3; } can be translated as: v = e; if (v==0) s0; else if (v == 1) s1; else if (v == 2) s2; else if (v == 3) s3; 8 / 18

Implementation of case statement (Continued) Binary search: O(log N) comparisons, a drastic improvement over sequential search for large N. Using this, the above case statement can be translated as v = e; if (v<=1) if (v==0) s0; else if (v==1) s1; else if (v>=2) if (v==2) s2; else if (v==3) s3; 9 / 18

10 / 18 If-Then-Else Implementation of case statement (Continued) Another technique is to use hash tables. This maps the value v to the case label that corresponds to the value v. This takes constant time (expected).

11 / 18 Control Statements (contd.) while: let s1 = while C do S then it can also be written as s1 = if C then {S; s1} repeat: let s2 = repeat S until C then it can also be written as s2 = S; if (!C) then s2 loop let s = loop S end its semantics can be understood as S; s

12 / 18 For-loop Semantics of for (S2; C; S3) S can be specified in terms of while: S2; while C do { S; S3 } In some languages, additional restrictions imposed to enable more efficient code Value of index variable can t change loop body, and is undefined outside the loop Bounds may be evaluated only once

Unstructured Control Flow Unstructured control transfer statements (goto) can make programs hard to understand: 40:if (x > y) then goto 10 if (x < y) then goto 20 goto 30 10:x = x - y goto 40 20:y = y -x goto 40 30:gcd = x 13 / 18

Unstructured Control Flow (Continued) Unstructured control transfer statements (goto) can make programs hard to understand: 40:if (x > y) then goto 10 if (x < y) then goto 20 goto 30 10:x = x - y goto 40 20:y = y -x goto 40 30:gcd = x Equivalent program with structured control statements is easier to understand: while (x!=y) { if (x > y) then x=x-y else y=y-x } 14 / 18

15 / 18 Control Statements (contd.) goto should be used in rare circumstances e.g., error handling. Java doesn t have goto. It uses labeled break instead: l1: for (... ) { while (...) {... break l1 } } break l1 causes exit from loop labeled with l1

16 / 18 Control Statements (contd.) Restrictions in use of goto: jumps across procedures jumps from outer blocks to inner blocks or unrelated blocks goto l1; if (...) then { int x; x = 5; l1: y = x*x; } Jumps from inner to outer blocks are permitted.

update_store(s, l, v) gives a new store sn which is identical to s except that location l in sn contains value v. 17 / 18 Statements If-Then-Else S id = E ; S if C S [else S] S while C S S { S+ } type stmt = Assign of id * expr If of cond * stmt * stmt While of cond * stmt Block of stmt list ;; What does the statement y = x + 1; do? The effect of a statement is to change the store. eval_stmt: stmt * environment * store -> store We will use a function update_store to change the store:

Evaluating statements: The Program eval_stmt(stmt, env, store) = match stmt with Assign(x, e) -> let l = binding_of(env, x) and v = eval_expr(e, env, store) in update_store(store, l, Intval(v)) If(c, s1, s2) -> if (eval_cond(c, env, store)) then eval_stmt(s1, env, store) else eval_stmt(s2, env, store) While(c, s) -> if (eval_cond(c, env, store)) then let store' = eval_stmt(s, env, store) in eval_stmt(while(c, s), env, store') else store... 18 / 18