Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.

Slides:



Advertisements
Similar presentations
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
Advertisements

Intermediate Code Generation
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Principles of programming languages 3: Answers for exercises Isao Sasano Department of Information Science and Engineering.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
Imperative Programming
Principles of programming languages 2: Answers for exercises
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Unit 3 Lesson 2: Rational Expressions
Principle of Programming Lanugages 2: Imperative languages Isao Sasano Department of Information Science and Engineering ( structured programming, control.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
CPS 506 Comparative Programming Languages Syntax Specification.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Principles of programming languages 12: Logic programming Supplemental material Definition of prefix and suffix Isao Sasano Department of Information Science.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Sum of Arithmetic Sequences. Definitions Sequence Series.
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Chapter 02 (Part II) Introduction to C++ Programming.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
ARITHMETIC IN C Operators.
Principle of Programming Lanugages 2: Imperative languages
Principles of programming languages 8: Types
Information Science and Engineering
Chapter 2 - Introduction to C Programming
Addition/ Subtraction
Principles of programming languages 4: Parameter passing, Scope rules
Arithmetic operations & assignment statement
Algebra 1 Section 2.3 Subtract real numbers
Chapter 2 - Introduction to C Programming
Chapter 8: Control Structures
Objective: To Divide Integers
OPERATORS (1) CSC 111.
Chapter 2 - Introduction to C Programming
Programming Misconceptions
Chapter 2 - Introduction to C Programming
Laptop Instrument Meeting 4 January 29, 2018.
Programming Funamental slides
Lecture 18 Arrays and Pointer Arithmetic
Number Patterns.
Chapter 2 - Introduction to C Programming
What comes next in this sequence
9.2 Arithmetic Sequences and Series
Chapter 2 - Introduction to C Programming
Introduction to C Programming
COS 151 Bootcamp – Week 4 Department of Computer Science
Presentation transcript:

Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano

Today’s topic Give an operational semantics to a tiny subset of the language C. – We use an operational semantics called natural semantics or structural operational semantics.

Arithmetic expressions ::= | | ( + ) | ( - ) | ( * ) ::= X | Y | Z ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 We will give an operational semantics for the arithmetic expressions given below. We use only three variables X, Y, and Z. (ex.) ( ), (3 * (45 – X)) etc.

States A variable in C is a name for a location. The semantics of a variable in an arithmetic expression is a value stored in the corresponding location. A state is a function from locations to integers. We let the state with all the values being 0 as the initial state. For example, under the declarations int X = 3; int Y = 4; the state is { (X, 3), (Y, 4), (Z, 0) }.

Meta variables We use meta variables for representing expressions, sequences, integers, variables, and states as in the following. expressions : a, a 1, a 2, … sequences of numbers : n, n 1, n 2, … integers : m, m 1, m 2, … variables : x, y, … states : σ, σ 1, σ 2, …

Evaluation of arithmetic expressions We represent the relation that we get an integer m by evaluating an arithmetic expression a in a state  as follows.  m (ex.) Suppose  = { (X, 3), (Y, 20), (Z, 13) }. Then the following relations hold.  120  20

Evaluation of arithmetic expressions An arithmetic expression (( ) * 4) is evaluated o 120 by evaluating ( ), obtaining 30, and evaluating (30 * 4). All the arithmetic expressions are evaluated according to some rules.

Evaluation rules for arithmetic expressions Sequences of numbers  m (m is an integer represented by the sequence of numbers n.) Variables   (x) Addition  m 1  m 2  m (m is the sum of m 1 and m 2.)

Evaluation rules for arithmetic expressions (Cont.) Subtraction  m 1  m 2  m (m is the difference between m 1 and m 2 ) Multiplication  m 1  m 2  m (m is the product of m 1 and m 2 )

Example 1 Evaluate an arithmetic expression (( ) * 4) under the state  = { (X, 3), (Y, 20), (Z, 13) }.  120  30  4  10  20

Example 2 Evaluate an arithmetic expression (5 * (X + 1)) under the state  = { (X, 3), (Y, 20), (Z, 13) }.  20  5  3  1  4

Exercise 1 Evaluate an arithmetic expression ((4 + Y) * (5 + Z)) in the state  = { (X, 3), (Y, 20), (Z, 13) }.

Statements We have given semantics to the arithmetic expressions. We get values by executing (evaluating) expressions. (In the full set of C, evaluating an expression may change the state.) Executing a statement changes the state. (ex.) X = 2; By executing this statement, the value of X is changed (if the original value is not 2.) Let the state before executing the statement to be . After executing the statement, the value of X in the state is changed to 2.

Notation concerning states We write  [ m / x ] for the state after assigning an integer m to a variable x in the state . (ex.) X = 2; By executing the statement in the state , the state becomes  [ 2 / X ]. (ex.) X = (X + 2); By executing the statement in the state , the state becomes  [  (X) + 2 / X ]. m if y = x,  (y) if y  x (  [ m / x ]) (y) =

Exercise 2 Let  = { (X, 10), (Y, 20), (Z, 30) }. Write down all the elements of  [ 40 / X ] in the set notation.

Syntax of statements We use the statements defined below. :: = = ; | | while ( ) { } We use meta variables c, c 1, c 2, etc. for representing statements. (Note) Although the C language does not require the body of while statements is surrounded by curly braces, we does require because in our definition a sequence of statements is a statement.

Execution of statements We write   2 for representing that executing statement c in state  1 terminates in state  2. (ex.) By executing the statement Y = 40; in the state { (X, 10), (Y, 20), (Z, 30) }, the state becomes { (X, 10), (Y, 40), (Z, 30) }. We write this relation as follows.  { (X, 10), (Y, 40), (Z, 30) }

Rules for executing statements  m   [ m / x ] Assignments Sequences of statements   1   2   2

Example 1  40   [ 40 / Y ] Derive the state after executing the statement Y=40; in the state  = { (X, 10), (Y, 20), (Z, 30) }. { (X, 10), (Y, 40), (Z, 30) }

Example 2  40  (  [3 / X]) [ 40 / Y ] Derive the state after executing the statement X = 3; Y=40; in the state  = { (X, 10), (Y, 20), (Z, 30) }. { (X, 3), (Y, 40), (Z, 30) }  3   [3/X]  (  [3 / X]) [ 40 / Y ]

Exercise 3 Derive the state after executing the statement X = (Y + 2); Y = (Y + 3); in the state  = { (X, 10), (Y, 20), (Z, 30) }.

Rules for while statements  0    m   1   2   2 if m  0

Example 3  0   [0/Y] Derive the state after executing the statement while ( Y ) { Y = (Y – 20); } in the state  = { (X, 10), (Y, 20), (Z, 30) }. { (X, 10), (Y, 0), (Z, 30) }  20   [0 / Y]  0   [0/Y]  20  20

Exercise 4 Derive the state after executing the statement while ( Y ) { Y = (Y – 20); } in the state  = { (X, 10), (Y, 40), (Z, 30) }.