Principles of Programming Languages

Similar documents
LECTURE 17. Expressions and Assignment

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

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

Related Course Objec6ves

G Programming Languages - Fall 2012

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

Expressions and Assignment

Principles of Programming Languages

Control Flow February 9, Lecture 7

A simple syntax-directed

Operators. Java operators are classified into three categories:

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

LECTURE 18. Control Flow

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Principles of Programming Languages

Ways to implement a language

SECTION II: LANGUAGE BASICS

A Simple Syntax-Directed Translator

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

Expressions and Assignment Statements

Control Flow. CSE 307 Principles of Programming Languages Stony Brook University

MIDTERM EXAMINATION - CS130 - Spring 2003

2/20/2018. Chapter 6:: Control Flow. control flow or ordering. basic categories for control flow (cont.): basic categories for control flow:

Expressions & Assignment Statements

Chapter 7. Expressions and Assignment Statements ISBN

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

Principles of Programming Languages COMP251: Syntax and Grammars

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Supplemental Materials: Grammars, Parsing, and Expressions. Topics. Grammars 10/11/2017. CS2: Data Structures and Algorithms Colorado State University

CSC 1214: Object-Oriented Programming

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

Operators and Expressions

Principles of Programming Languages

Preview of Expressions

Lecture 13: Expression Evaluation

1 Lexical Considerations

Expression Evaluation and Control Flow. Outline

Computers Programming Course 6. Iulian Năstac

Intermediate Code Generation

Formal Languages and Automata Theory, SS Project (due Week 14)

LISP: LISt Processing

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

The PCAT Programming Language Reference Manual

HANDLING NONLOCAL REFERENCES

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Operators & Expressions

This book is licensed under a Creative Commons Attribution 3.0 License

Chapter 3: Operators, Expressions and Type Conversion

Chap 6 - Introduction to HDL (b)

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Chapter 7. Expressions and Assignment Statements

Lexical Considerations

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Prof. Navrati Saxena TA: Rochak Sachan

Functional Programming. Pure Functional Programming

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

SMURF Language Reference Manual Serial MUsic Represented as Functions

Lecture 3: Modeling in VHDL. EE 3610 Digital Systems

Chapter Two MIPS Arithmetic

Information Science 1

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Principles of Programming Languages

JAVA OPERATORS GENERAL

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Operators in java Operator operands.

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Chapter 6 Control Flow. June 9, 2015

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

D0010E Object- oriented programming and design. Today. Today An introduc<on to the basic syntax and seman<cs of Java

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

UNIT 3 OPERATORS. [Marks- 12]

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Lexical Considerations

CPS 506 Comparative Programming Languages. Syntax Specification

CT 229. Java Syntax 26/09/2006 CT229

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

Intermediate Representations

Lecture Chapter 6 Recursion as a Problem Solving Technique

ECE Digital System Design & Synthesis Exercise 1 - Logic Values, Data Types & Operators - With Answers

The SPL Programming Language Reference Manual

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

INSTITUTE OF AERONAUTICAL ENGINEERING

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

IT T35 Digital system desigm y - ii /s - iii

Lecture 2: Big-Step Semantics

Programming in C++ 5. Integral data types

UNIT- 3 Introduction to C++

Operators in C. Staff Incharge: S.Sasirekha

Principles of Programming Languages

Transcription:

Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Control Flow Expression evalua?on Lesson 17! 1

Control Flow: Ordering the Execu?on of a Program Constructs for specifying the execu?on order: 1. Sequencing: the execu?on of statements and evalua?on of expressions is usually in the order in which they appear in a program text 2. Selec>on (or alterna>on): a run-?me condi?on determines the choice among two or more statements or expressions 3. Itera>on: a statement is repeated a number of?mes or un?l a run-?me condi?on is met 4. Procedural abstrac>on: subrou?nes encapsulate collec?ons of statements and subrou?ne calls can be treated as single statements 2

Control Flow: Ordering the Execu?on of a Program (cont d) 5. Recursion: subrou?nes which call themselves directly or indirectly to solve a problem, where the problem is typically defined in terms of simpler versions of itself 6. Concurrency: two or more program fragments executed in parallel, either on separate processors or interleaved on a single processor 7. Excep>on handling: when abnormal situa?ons arise in a protected fragment of code, execu?on branches to a handler that executes in place of the fragment 8. Nondeterminacy: the execu?on order among alterna?ve constructs is deliberately lel unspecified, indica?ng that any alterna?ve will lead to a correct result 3

Expression Syntax and Effect on Evalua?on Order An expression consists of An atomic object, e.g. number or variable An operator applied to a collec?on of operands (or arguments) that are expressions Common syntac?c forms for operators: Func?on call nota?on, e.g. somefunc(a, B, C) Infix nota?on for binary operators, e.g. A + B Prefix nota?on for unary operators, e.g. - A Pos*ix nota?on for unary operators, e.g. i++ Cambridge Polish nota?on, e.g. (* (+ 1 3) 2) in Lisp "Mul7- word" infix ("mixfix"), e.g. a > b? a : b in C mybox displayon: myscreen at: 100@50 in Smalltalk, where displayon: and at: are wriren infix with arguments mybox, myscreen, and 100@50 4

Operator Precedence and Associa?vity The use of infix, prefix, and posuix nota?on some?mes lead to ambiguity as to what is an operand of what Fortran example: a + b * c**d**e/f a + ((b * (c**(d**e)))/f) Operator precedence: higher operator precedence means that a (collec?on of) operator(s) group more?ghtly in an expression than operators of lower precedence Operator associa7vity: determines grouping of operators of the same precedence LeA associa7ve: operators are grouped lel- to- right (most common) Right associa7ve: operators are grouped right- to- lel (Fortran power operator **, C assignment operator = and unary minus) Non- associa7ve: requires parenthesis when composed (Ada power operator **) 5

Fortran Pascal C Ada ++, -- (post-inc., dec.) ** not ++, -- (pre-inc., dec.), +, - (unary), &, * (address, contents of),!, (logical, bit-wise not) *, / *, /, div, mod, and +, - (unary and binary).eq.,.ne.,.lt.,.le.,.gt.,.ge. (comparisons).not. +, - (unary and binary), or <, <=, >, >=, =, <>, IN * (binary), /, % (modulo division) abs (absolute value), not, ** *, /, mod, rem +, - (binary) +, - (unary) <<, >> (left and right bit shift) <, <=, >, >= (inequality tests) ==,!= (equality tests) & (bit-wise and) ˆ (bit-wise exclusive or) (bit-wise inclusive or) +, - (binary), & (concatenation) =, /=, <, <=, >, >=.and. && (logical and) and, or, xor (logical operators).or..eqv.,.neqv. (logical comparisons) Precedence levels (logical or)?: (if... then... else) =, +=, -=, *=, /=, %=, >>=, <<=, &=, ˆ=, = (assignment), (sequencing) 6

Operator precedence levels and associa?vity in Java Operatore Descrizione Associa a _. _ dot notation sinistra _ [ _ ] accesso elemento array _ ( _ ) invocazione di metodo _ ++ incremento postfisso _ -- decremento postfisso ++ _ incremento prefisso -- _ decremento prefisso! _ negazione booleana ~ _ negazione bit-a-bit + _ segno positivo (nessun effetto) - _ inversione di segno ( Tipo )_ cast esplicito new _ creazione di oggetto _ * _ moltiplicazione sinistra _ / _ divisione o divisione tra interi sinistra _ % _ resto della divisione intera sinistra _ + _ somma o concatenazione sinistra _ - _ sottrazione sinistra _ << _ shift aritmetico a sinistra sinistra _ >> _ shift aritmetico a destra sinistra _ >>> _ shift logico a destra sinistra _ < _ minore di sinistra _ <= _ minore o uguale a sinistra _ > _ maggiore di sinistra _ >= _ maggiore o uguale a sinistra _ == _ uguale a sinistra _!= _ diverso da sinistra instanceof appartenenza a un tipo sinistra _ & _ AND bit-a-bit sinistra _ ^ _ XOR bit-a-bit sinistra _ _ OR bit-a-bit sinistra _ && _ congiunzione lazy sinistra _ _ disgiunzione inclusiva lazy sinistra _? _ : _ espressione condizionale destra _ = _ assegnamento semplice destra _ op= _ assegnamento composto destra (op uno tra *, /, %, +, -, <<, >>, >>>, &, ^, ) destra 7

Operator Precedence and Associa?vity C s very fine grained precedence levels are of doubuul usefulness Pascal s flat precedence levels is a design mistake if A<B and C<D then is grouped as follows if A<(B and C)<D then Note: levels of operator precedence and associa?vity can be captured in a context- free grammar be imposed by instruc?ng the parser on how to resolve shil- reduce conflicts. 8

Evalua?on Order of Expressions Precedence and associa?vity state the rules for grouping operators in expressions, but do not determine the operand evalua>on order! Expression a-f(b)-b*c is structured as (a-f(b))-(b*c) but either (a-f(b)) or (b*c) can be evaluated first The evalua?on order of arguments in func?on and subrou?ne calls may differ, e.g. arguments evaluated from lel to right or right to lel Knowing the operand evalua?on order is important Side effects: suppose f(b) above modifies the value of b (that is, f(b) has a side effect) then the value will depend on the operand evalua?on order Code improvement: compilers rearrange expressions to maximize efficiency, e.g. a compiler can improve memory load efficiency by moving loads up in the instruc?on stream 9

Denota?onal seman?cs of expressions If expressions don t have side effects, the seman?c interpreta?on func?on is E: Exp à Env à Store à Eval Precedence and associa?vity rules determine the abstract syntax Seman?cs by structural induc?on with one rule for each operator, e.g. E{ e1 + e2 } r s = E{ e1 } r s E{ e2 } r s where is the seman?c counterpart of + If expression may have side effects, the func?on is E: Exp à Env à Store à (Eval x Store) Order of evalua?on of arguments may influence the result Seman?c rules must specify the order. Eg: E{ e1 + e2 } r s = let (v, s ) = E{ e1 } r s in let (v, s ) = E{ e2 } r s in (v v, s ) 10

Expression Operand Reordering Issues Rearranging expressions may lead to arithmetic overflow or different floating point results Assume b, d, and c are very large positive integers, then if b-c+d is rearranged into (b+d)-c arithmetic overflow occurs Floating point value of b-c+d may differ from b+d-c Most programming languages will not rearrange expressions when parenthesis are used, e.g. write (b-c)+d to avoid problems Design choices: Java: expressions evaluation is always left to right in the order operands are provided in the source text and overflow is always detected Pascal: expression evaluation is unspecified and overflows are always detected C and C++: expression evaluation is unspecified and overflow detection is implementation dependent Lisp: no limit on number representation 11

Short- Circuit Evalua?on Short-circuit evaluation of Boolean expressions: the result of an operator can be determined from the evaluation of just one operand Pascal does not use short-circuit evaluation The program fragment below has the problem that element a[11] is read resulting in a dynamic semantic error: var a:array [1..10] of integer;... i := 1; while (i<=10) and (a[i]<>0) do i := i+1 C, C++, and Java use short-circuit conditional and/or operators If a in a&&b evaluates to false, b is not evaluated If a in a b evaluates to true, b is not evaluated Avoids the Pascal problem, e.g. while (i <= 10 && a[i]!= 0)... Ada uses and then and or else, e.g. cond1 and then cond2 Ada, C, C++ and Java also have regular bit-wise Boolean operators 12