Imperative Programming

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

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.
Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Programming Languages and Paradigms The C Programming Language.
1 Compiler Construction Intermediate Code Generation.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Names, Bindings, Type Checking, and Scopes
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Run time vs. Compile time
Chapter 9: Subprogram Control
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
CIS Computer Programming Logic
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
ISBN Chapter 7 Expressions and Assignment Statements.
Compiler Construction
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
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.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Programming Languages and Paradigms Imperative Programming.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
April 2, ICE 1341 – Programming Languages (Lecture #12) In-Young Ko Programming Languages (ICE 1341) Lecture #12 Programming Languages (ICE 1341)
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Slide 1 WEEK 8 Imperative Programming Original by Vitaly Shmatikov.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
By Mr. Muhammad Pervez Akhtar
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Names, Scope, and Bindings Programming Languages and Paradigms.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Review for Test 2 Chapters 5 (start at 5.4), 6.1, , 12, 13, 15.1, Python.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
Functional Programming
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Information and Computer Sciences University of Hawaii, Manoa
Names and Attributes Names are a key programming language feature
Information Science and Engineering
Chapter 6: Data Types Lectures # 10.
Chapter 8: Control Structures
Chap. 6 :: Control Flow Michael L. Scott.
Chapter 10 Programming Fundamentals with JavaScript
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Chap. 6 :: Control Flow Michael L. Scott.
UNIT V Run Time Environments.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages and Paradigms
Midterm Review In Text: Chapters 1-3, 5-11, 15.
Presentation transcript:

Imperative Programming

Imperative Programming Most widely used paradigm Oldest paradigm Largest number of languages E.g. C, FORTRAN, Java/C++ (without the objects) Features closely related to machine architecture Based on the von Neumann stored program model We will skip a number of topics in this chapter as you should already be familiar with them from previous courses

Imperative Programming Language Structure Declarations Associate variables with memory locations Expressions Evaluated in the current environment Commands Execution and flow of control Similar to the machine instructions Assignment Dynamically updates data store Conditionals Statements executed or skipped Branching Alters the flow of control Supports looping Basis for defining an effective programming language

Turing Completeness A programming language is said to be Turing Complete if it contains Integer variables, values and operations Assignments, branching, sequencing, and conditionals Other features make languages easier to program for complex applications Loops Procedures Object Orientation But they have the same power as any other programming language i.e. what is possible to compute in one is possible in the other Jay is Turing Complete

Other Features Typical Turing complete language today supports: Data types for reals, chars, strings, booleans For, while, switch Arrays Records I/O commands Pointers Procedures and functions

Imperative Programming Design Principles Structured Programming The structure of the program text should be the guide to understanding what it does. Can be more efficient Improved readability Easier to tune and modify Efficiency A language must allow an underlying assignment-oriented machine to be used directly and efficiently. Driving factor in the implementation of many imperative languages

Types in Typical Imperative Languages Emphasis on data structures with assignable components Based on values that can be manipulated directly by underlying machine Size and layout fixed at compile time Storage allocated and deallocated explicitly Strongly typed Storage efficiency is the key

Naming and Variables Skipping in Text Reserved words Variables unique Concept of scope, scoping rules

Types, Values and Expressions in Jay Let’s look at defining types, values, and expressions in Jay Jay supports only two types Boolean and Integer Boolean Values: {true,false} Operations: &&, ||, ! Integer Values: {…,-2,-1,0,1,2,…..} Operations Arithmetic : +,-,*,/ Relational: ==, !=, <=, <, >=, >

Expressions in Jay Expression has the forms Value Variable Result of a binary operation Result of a unary operation Type of an expression is defined by the types of its constituents Constituent Expression Type Value Type of the value Variable Type of the variable Arithmetic Binary operator int Relational Binary operator boolean Unary operator boolean

Formally Defining Types Back to the typemap format from chapter 3:

Types of Expressions Formally defining expression types in Jay:

Jay Expression Example Declaration: int x,y; Expressions: x+2*y x<2*y x<2*y && x>0 Typemap tm={<x,int>,<y,int>}

Validity of Expressions typeOf function determines the type of expressions typeOf function does NOT determine the validity of the expression Need to check separately

Validity of Expressions An expression is valid if it is: int or boolean Value Variable in the type map Unary with a UnaryOp operator and a valid boolean operand Binary with ArithmeticOp with two valid int operands Binary with RelationalOp with two valid int operands Binary with BooleanOp with two valid boolean operands

Formal Validity of Expressions More formally:

Validity of Expressions

Expression Example Validity of x+2*y Note recursive nature of V

Expression Example

Semantics of Jay Semantic domains Integers (I) Boolean (B) Meaning of a Jay Expression can be defined functionally: ApplyBinary : Computes a value given a binary operator and two operands ApplyUnary : Computes a value given a unary operator and one operand Uses the properties of the underlying semantic domains

ApplyBinary Function

ApplyBinary, Cont.

Semantics Example Meaning of expression x+2*y Note recursive definitions

Semantics ApplyBinary depends on the meaning of the semantic domain of I and B Sometimes it is possible to define the meanings directly without using domains, e.g. for the definitions of && and || without using  and  Using the Short Circuit evaluation method

Elementary Data Types Skipping from book Nibble, byte, word, quadword, etc. IEEE 754 number format Unicode Operator overloading, type conversion (e.g. int/float) See textbook for similarity in C++ and Java operators Book uses ?: for conditional E.g. x = (y>0) ? 1 : 2; If (y>0) x=1; else y=2;

Syntax and Semantics of Jay Recall the following concrete and abstract syntax for Jay statements We can define validity for statements:

Semantics of Skip Skip Skip statement can’t change the state

Semantics of Assignment Discussed in chapter 3 For x=a+b

Semantics of Conditional If (a>b) max=a; else max=b

Conditional, continued

Semantics of Block Block is just a sequence of statements Example for Block b: fact = fact * i; i = i – 1;

Block example b1 = fact = fact * i; b2 = i = i – 1; M(b,σ) = M(b2,M(b1,σ)) = M(i=i-1,M(fact=fact*i,σ)) = M(i=i-1,M(fact=fact*i,{<i,3>,<fact,1>})) =M(i=i-1,{<i,3>,<fact,3>}) ={<i,2>,<fact,3>} b

Semantics of Loop Loop = Expression test; Statement body Recursive definition

Loop Example Initial state σ={<N,3>} fact=1; i=N; while (i>1) { fact = fact * i; i = i -1; } After first two statements, σ = {<fact,1>,<N,3>,<i,3>}

Loop Example σ = {<fact,1>,<N,3>,<i,3>} M(while(i>1) {…}, σ) = M(while(i>1) {…}, M(fact=fact*i; i=i-1;, σ) = M(while(i>1) {…}, {<fact,3>,<N,3>,<i,2>}) = M(while(i>1) {…}, {<fact,6>,<N,3>,<i,1>}) = M(σ) ={<fact,6>,<N,3>,<i,1>}

Syntax and Semantics for Real Languages For Loop Concrete Syntax ForStatement  for (Assign1opt; Expropt; Assign2opt) Statement Abstract Syntax Identical to Abstract Syntax for While See text for details Do statements DoStatement  do Statement while (Expression) Different semantics, must make sure body is execute once

Syntax and Semantics of Real Languages Switch statement Multi-way IF statement See book for details, straightforward extension Break/Continue statement Break terminates switch or while Continue terminates current iteration of while and jumps back to the loop test Semantics must be specified in definitions for switch and while E.g. break statement in a switch: Meaning if no break is Meaning of first matching true statement and all subsequent statements Meaning if break is Meaning of first matching true statement, all subsequent statements until one contains a break

Scoping Skipping scope, visibility, and lifetime Should be familiar with this from Java Static scope Scope computed at compile time Dynamic scope Scope compute at run time

Extension to Methods Suppose we would like to extend Jay to include methods (functions) Must augment the concrete syntax:

Sample Program

Syntax for Methods Must modify BNF rules for statements to allow method calls as a statement or a factor within an expression:

Abstract Syntax for Methods Underlined rules indicate changes

Static Type Checking for Methods New validity rules now need to be added: Every method and global variable must have a unique ID All local variables and parameters within a method must be unique with valid types All statements and expressions within the body of each method must be valid with respect to the variables and parameters accessible to them A return appears in every method with a nonvoid Type, and the type of the Expression in the return must be identical with the method’s Type Every call has a name identical with the name of a method in the program and has the same number of arguments as the method Every argument in a call has the same type as the type given in the corresponding parameter in the method of that name See book for denotational semantics to implement these rules