CS2104 Prog. Lang. Concepts

Similar documents
An Annotated Language

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Introduction to Axiomatic Semantics

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers.

A CRASH COURSE IN SEMANTICS

Chapter 3. Describing Syntax and Semantics

Abstract Interpretation

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

Chapter 3. Describing Syntax and Semantics ISBN

2 Introduction to operational semantics

Chapter 3. Describing Syntax and Semantics

Chapter 3 (part 3) Describing Syntax and Semantics

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

1 Lexical Considerations

Programming Languages Third Edition

3.7 Denotational Semantics

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

Introduction to Axiomatic Semantics (1/2)

Lecture 5 - Axiomatic semantics

Lexical Considerations

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

02157 Functional Programming Interpreters for two simple languages including exercises

Lexical Considerations

Information Science 1

Introduction to Structural Operational Semantics

Defining syntax using CFGs

Formal Semantics of Programming Languages

Formal Methods. CITS5501 Software Testing and Quality Assurance

Introduction to Axiomatic Semantics (1/2)

3. DESCRIBING SYNTAX AND SEMANTICS

Proving Properties on Programs From the Coq Tutorial at ITP 2015

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

CMSC 330: Organization of Programming Languages

Theorem proving. PVS theorem prover. Hoare style verification PVS. More on embeddings. What if. Abhik Roychoudhury CS 6214

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Introduction to Parsing Ambiguity and Syntax Errors

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

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

Programming Languages and Compilers (CS 421)

Foundations: Syntax, Semantics, and Graphs

Introduction to Parsing Ambiguity and Syntax Errors

n <exp>::= 0 1 b<exp> <exp>a n <exp>m<exp> 11/7/ /7/17 4 n Read tokens left to right (L) n Create a rightmost derivation (R)

Lectures 20, 21: Axiomatic Semantics

The PCAT Programming Language Reference Manual

CSCI312 Principles of Programming Languages!

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

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

Formal Semantics of Programming Languages

Contents. Chapter 1 SPECIFYING SYNTAX 1

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Chapter 3. Describing Syntax and Semantics ISBN

Hoare Logic: Proving Programs Correct

In Our Last Exciting Episode

CSC 7101: Programming Language Structures 1. Operational Semantics. Winskel, Ch. 2 Slonneger and Kurtz Ch 8.4, 8.5, 8.6. Operational vs.

CPS 506 Comparative Programming Languages. Syntax Specification

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

CSE 582 Autumn 2002 Exam 11/26/02

Project 2 Interpreter for Snail. 2 The Snail Programming Language

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

6. Hoare Logic and Weakest Preconditions

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

COMP 507: Computer-Aided Program Design

Denotational Semantics

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

From Hoare Logic to Matching Logic Reachability. Grigore Rosu and Andrei Stefanescu University of Illinois, USA

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

YOLOP Language Reference Manual

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Semantics of programming languages

Application: Programming Language Semantics

MIDTERM EXAMINATION - CS130 - Spring 2003

CS 242. Fundamentals. Reading: See last slide

Recursion and Structural Induction

STUDENT LESSON A12 Iterations

Reasoning About Imperative Programs. COS 441 Slides 10

CS 381: Programming Language Fundamentals

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

The Rule of Constancy(Derived Frame Rule)

The Formal Semantics of Programming Languages An Introduction. Glynn Winskel. The MIT Press Cambridge, Massachusetts London, England

CSE P 501 Exam 8/5/04

Semantic Analysis. Role of Semantic Analysis

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

LECTURE 18. Control Flow

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 1. Introduction

No model may be available. Software Abstractions. Recap on Model Checking. Model Checking for SW Verif. More on the big picture. Abst -> MC -> Refine

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

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Syntax. A. Bellaachia Page: 1

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

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Semantics. A. Demers Jan This material is primarily from Ch. 2 of the text. We present an imperative

Transcription:

CS2104 Prog. Lang. Concepts Operational Semantics Abhik Roychoudhury Department of Computer Science National University of Singapore

Organization An imperative language IMP Formalizing the syntax of IMP Meaning of arithmetic expressions Meaning of Boolean Expressions Meaning of statements/commands Discussion on Axiomatic Semantics

IMP : a toy imperative language IMP is an imperative language in the style of PASCAL or C (even though some of the syntax may be different) The language contains arithmetic and boolean expressions as well as if-then-else, while statements. The syntax of the program will be described by BNF grammars.

IMP : a toy imperative language During execution of IMP program, the state of execution will be captured by the values of program variables. Operational semantics will be described by rules which specify how Expressions in IMP pgm. are evaluated Statements in IMP pgm. change the state

Syntax of IMP non-negative integers N truth values T = {true, false} variables V arithmetic expressions A boolean expressions B statements/commands C

Syntax of N Regular Grammar: Regular Expression N DN D D 0 1 2 3 4 5 6 7 8 9 (0 1 2 3 4 5 6 7 8 9)(0 1 2 3 4 5 6 7 8 9) or (0 1 2 3 4 5 6 7 8 9) +

Syntax of T Regular Grammar T true false Regular Expression (true f alse)

Syntax of V Regular Grammar: V LV DV L D 0 1 2 3 4 5 6 7 8 9 L a b... z Construct the equivalent regular expression.

Syntax of Arithmetic expressions A A N A V A A + A A A A This grammar is ambiguous but an equivalent unambiguous grammar of arithmetic expressions can be easily constructed. Try it?

Syntax of Boolean expressions B B T B A = A B A A B B B B B This grammar is ambiguous but an equivalent unambiguous grammar of boolean expressions can be easily constructed. Try it?

Syntax of Commands C C skip C V := A C C; C C if B then C else C C while B do C Is there ambiguity in the above grammar?

Execution model Operational semantics of IMP describes how programs in that language are excuted. To describe this, it needs to assume an underlying execution model. The execution model could be thought as a state machine although not necessarily a finite state machine.

States of Execution Model Each state of the execution model is a unique assignment of values to variables. Thus, if {a, b} are the only variables in an IMP program, then each of the following are states in the execution model a = 0, b = 0 a = 0, b = 1 a = 0, b = 2... a = 1, b = 0...

Operational Semantics Operational Semantics for the IMP language will give rules to describe the following: Given a state s how to evaluate arithmetic expressions how to evaluate boolean expressions how the commands can alter s to a new state s

Meaning of Arith. Expressions A - (1) Numbers: n, s n Number n in any state s evaluates to n e.g. 0, s 0, 1, s 1 Variables: X, s s(x) Variable X in state s evaluates to value of X in s. e.g. a, (a = 5, b = 20) 5 b, (a = 5, b = 20) 27

Meaning of Arith. Expressions A - (2) Sums: a 0, s n 0 a 1, s n 1 a 0 + a 1, s n where n is the sum of n 0 and n 1 e.g. a + b, (a = 5, b = 20) 25 Products: a 0, s n 0 a 1, s n 1 a 0 a 1, s n where n is the product of n 0 and n 1 e.g. a b, (a = 5, b = 20) 100

Example arith. expr. evaluation Evaluating meaning of a complicated arith. expr. will require several application of the above rules operator precedence a, (a = 5, b = 20) 5 b, (a = 5, b = 20) 20 a b, (a = 5, b = 20) 100 a b + b, (a = 5, b = 20) 120 b, (a = 5, b = 20) 20

Syntax of Boolean expressions B - Recap B T B A = A B A A B B B B B

Meaning of Boolean Expressions B - (1) true, s true false, s false Equality Check a 0, s n 0 a 1, s n 1 a 0 = a 1, s true a 0, s n 0 a 1, s n 1 a 0 = a 1, s false where n 0 and n 1 are equal where n 0 and n 1 are unequal

LEQ check Meaning of Boolean Expressions B - (2) a 0, s n 0 a 1, s n 1 a 0 a 1, s true where n 0 is l.e.q. to n 1 a 0, s n 0 a 1, s n 1 a 0 a 1, s false where n 0 is greater than n 1

Negation Meaning of Boolean Expressions B - (3) b, s true b, s false b, s false b, s true Conjunction b 0, s t 0 b 1, s t 1 b 0 b 1, s t where t is logical conjunction of t 0, t 1

Example of Boolean Expr. meaning a, (a = 5, b = 6) 5 b, (a = 5, b = 6) 6 a = b, (a = 5, b = 6) false a = b, (a = 5, b = 6) true a, s 5 b, s 6 a b, s true a b a = b, s false a, s 5 b, s 6 a = b, s false where s is the state (a = 5, b = 6)

Meaning of Expressions Expressions evaluate to values in a given state. Therefore, the meaning of expressions are given by values. boolean values for boolean expressions numbers for arithmetic expressions Using the meaning of expressions, we can assign meaning to commands.

Meaning of Commands Execution of commands leads to a change of program state. Therefore the meaning of a command c is: If c is executed in some state s, how does it change s to s. c, s s

Syntax of Commands C - recap C skip C V := A C C; C C if B then C else C C while B do C

Rules for commands - (1) Skip skip, s s Sequencing c 0, s s int c 1, s int s c 0 ; c 1, s s

Rules for commands - (2) Assignment a, s n X := a, s s[x = n] where s[x = n] is a state which is same as state s, except that the value of varible X in s[x = n] is n. Thus: (a = 5, b = 20, c = 2)[a = 7] is the state (a = 7, b = 20, c = 2) (a = 5, b = 20, c = 2)[a = 5] is the state (a = 5, b = 20, c = 2)

Rules for commands - (2) If-then-else b, s true c 0, s s if b then c 0 else c 1, s s b, s false c 1, s s if b then c 0 else c 1, s s

Rules for commands - (3) While b, s false while b do c, s s b, s true c, s s int while b do c, s int s while b do c, s s

Summary of rules The meaning of each commands specifies how an execution of the command chnages state. Roughly speaking, this is done by simulating the execution of the commands. For example, the rule for while essentially unfolds the iterations of the while loop.

What we did consider We studied the syntax and semantics of a toy language which models many of the language features we are comfortable with. Our language IMP contains: program variables arithmetic and boolean expressions choice (if-then-else) loops (while) Studying the formal semantics of the language features allows: pinpointing the precise meaning of a program reasoning about programs

... and what we did not IMP does not contain many language features we are comfortable with. For example: different data types (not just integers) arrays of data types pointers objects procedures/functions recursion Formally reasoning about programs with all these features is still an active topic of research.

AXIOMATIC SEMANTICS Operational semantics lets us understand the meaning of a program. Axiomatic semantics allows us to also prove correctness of programs. The central idea in axiomatic semantics is that of an assertion: some property of program state at a particular control location

Assertions: Example {true } if (a >= b) m := a ; else m := b { m = max(a,b) } If we can prove the assertions above, we have proved correctness of the above code fragment!!

Hoare Triple A tool for reasoning about code fragments. Of the form {Pre} C {Post} Pre and Post are assertions. C is a program. Proving such a Hoare triple amounts to proving if we start in a state where Pre holds then execution of C in such a state produces a state where Post holds provided the program C terminates Previous slide contained a triple for the max program.

Proving Hoare Triples Proving correctness of a program amounts to setting appropriate pre- and post-conditions and then proving the corresponding Hoare Triple. To prove a Hoare Triple, we will use certain rules, based on the structure of the program under question. These rules are stated in the same manner as operational semantics rules.

Rule for if-then-else {P B}c 0 {Q} {P B}c 1 {Q} {P } if B then c 0 else c 1 {Q} {a >= b}m := a{m = max(a, b)} {a < b}m := b{m = max(a, b)} {true} if a >= b then m := a else m := b {m = max(a, b)} Now prove {a >= b}m := a{m = max(a, b)} and {a < b}m := b{m = max(a, b)}

Rule for assignment true {Q[x\e]}x := e{q} We need to prove {a >= b}m := a{m = max(a, b)} Using the above rule we can only prove: {a = max(a, b)}m := a{m = max(a, b)}

Rule of Consequence P P {P } C {Q } Q Q {P} C {Q} Set P = (a >= b), P = (a = max(a, b)) Q = Q = (m = max(a, b)) Then P P Therefore {a >= b}m := a{m = max(a, b)} holds.

Other rules Rule of sequences {P} C1 {R} {R} C2 {Q} {P} C1 ; C2 {Q} Rule of loop invariants {Cond P} Body {P} {P} while Cond do Body{ Cond P} P is a loop invariant : true after every iteration of the loop.

Exercise Reading : Chapter 3.3 of textbook (for axiomatic semantics) Study the correctness of the factorial function to get yourself familiar with reasoning about while loops. More about this to follow in the tutorial.