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

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

MIDTERM EXAMINATION - CS130 - Spring 2003

Parsing. Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Syntax Directed Translation

This book is licensed under a Creative Commons Attribution 3.0 License

More Assigned Reading and Exercises on Syntax (for Exam 2)

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

Introduction to Programming Using Java (98-388)

G Programming Languages - Fall 2012

CPS 506 Comparative Programming Languages. Syntax Specification

4. Semantic Processing and Attributed Grammars

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Program Representations

1 Lexical Considerations

CS2104 Prog. Lang. Concepts

Iteration. Side effects

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

CSCI312 Principles of Programming Languages!

MIDTERM EXAMINATION. CSE 130: Principles of Programming Languages. Professor Goguen. February 16, points total

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

2.2 Syntax Definition

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSE P 501 Exam 12/1/11

A programming language requires two major definitions A simple one pass compiler

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

A Simple Syntax-Directed Translator

Intermediate Code Generation

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

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Questions Bank. 14) State any four advantages of using flow-chart

Compiler Code Generation COMP360

CDA 3103 Computer Organization Homework #7 Solution Set

LECTURE 17. Expressions and Assignment

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Compiler Construction

Programming for Engineers Iteration


R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

SECTION II: LANGUAGE BASICS

Compiler Construction

LECTURE 18. Control Flow

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

Lecture Chapter 6 Recursion as a Problem Solving Technique

SECOND PUBLIC EXAMINATION. Compilers

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

Lexical Considerations

CSE P 501 Exam 11/17/05 Sample Solution

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

Decaf Language Reference

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Class Information ANNOUCEMENTS

Intermediate Formats. for object oriented languages

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

CS 314 Principles of Programming Languages. Lecture 9

SAURASHTRA UNIVERSITY

CSc 372 Comparative Programming Languages

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

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

C-LANGUAGE CURRICULAM

Intermediate Code Generation

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

MODEL ANSWERS COMP36512, May 2016

Lexical Considerations

Intermediate Code Generation

Computer Components. Software{ User Programs. Operating System. Hardware

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

CS 314 Principles of Programming Languages

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

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Introduction to Compilers and Language Design Copyright (C) 2017 Douglas Thain. All rights reserved.

Code No: R Set No. 1

ECE251 Midterm practice questions, Fall 2010

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

TDDD55 - Compilers and Interpreters Lesson 3

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Virtual Machine Where we are at: Part I: Stack Arithmetic. Motivation. Compilation models. direct compilation:... 2-tier compilation:

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CSE 401 Midterm Exam 11/5/10

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

CSE P 501 Exam 8/5/04

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Expressions and Statements. Department of CSE, MIT, Manipal

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Intermediate Formats. for object oriented languages

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Transcription:

Imperative Paradigm Syntax Rupesh Nasre. CS3100 Paradigms of Programming IIT Madras Jul 2014 Symbols int, cs12b063, function Expressions 10 * x, abs(x y), ++x Constructs if-else, while {, functionbeginend Grammar Role of Programming Languages Expressions 1: M[0] := 0 2: read(m[1]) 3: if M[1] >= 0 then goto 5 4: goto 7 5: M[3] := M[0] M[1] 6: if M[3] >= 0 then goto 16 7: writeln(m[1]) 8: read(m[2]) 9: M[3] := M[2] M[1] 10: if M[3] >= 0 then goto 12 11: goto 14 12: M[3] := M[1] M[2] 13: if M[3] >= 0 then goto 8 14: M[1] := M[2] + M[0] 15: goto 3 16: halt 1: read(x); 2: if x == 0 then goto 8; 3: writeln(x); 4: read(next); 5: if next == x then goto 4; 6: x = next; 7: goto 2; 8: ; 1: read(x); 2: while x!= 0 do begin 3: writeln(x); 4: repeat 5: read(next); 6: until next!= x; 7: x = next; 8: end; Assembly With goto With structured statements Input: 1 1 2 2 2 3 1 4 4 0 Output: 1 2 3 1 4 Infix 2 + 3 * 4 + 5 e.g., +, *, -, ==, <, p.x Postfix 2 3 4 * + 5 + e.g., a[i], f(), x++ Prefix + + 2 * 3 4 5 e.g., int i; *p, ++x Role of Programming Languages Expressions 1: read(x); 2: while x!= 0 do begin 3: writeln(x); 4: repeat 5: read(next); 6: until next!= x; 7: x = next; 8: end; 1: readentry(x); 2: while not endmarker(x) begin 3: writeentry(x); 4: repeat 5: readentry(next); 6: until not equalentry(x, next); 7: copyentry(x, next); 8: end; 1: readentry(x); 2: while not endmarker(x) { 3: writeentry(x); 4: next = removedup(x); 5: copyentry(x, next); 6: Infix 2 + 3 * 4 + 5 Postfix 2 3 4 * + 5 + Prefix + + 2 * 3 4 5 Infix 2 + -3 ** y + x++ Postfix t x = 2 3 u- y ** + t + x ++ Prefix Homework With structured statements With procedures Further abstraction

Precedence Ambiguous / Unambiguous Grammars x # y @ z How does a compiler know whether to execute # first or @ first? Think about x+y*z vs. x/y-z A similar situation arises in if-if-else. Humans and compilers may see different parse trees. #define MULT(x) x*x int main() { What if if both both the the operators are are the the same? printf( %d, MULT(3 + 1)); ## y @ ## @ yy Grammar for simple arithmetic expressions E E + E E * E E E E / E (E) number name E E + E E E T T T * T T / T F F (E) number name E E + T E T T T T * F T / F F F (E) number name Precedence not encoded Associativity not encoded Unambiguous grammar Check a b c Same Precedence Peculiar Constructs If-else nesting versus elseif or elsif. # x @ yy @ # z yy x + y + z x - y - z Order of evaluation doesn't matter. Order of evaluation matters. yy y Switch statement. Case ordering. Conversion of repeat-until into if + while. for i := 1 to 10 versus for (i = 1; i <= 10; ++i) for i := 10 downto 1: Why downto? Pascal: If expr then if expr then S else S Modula-2: if expr then if expr then S else S end end Modula-2: if expr then if expr then S end else S end Associativity C++ File I/O Associativity decides the order in which multiple instances of an operation are executed. Binary minus is left associative, hence x-y-z is equal to (x-y)-z. Homework: Write a C program to find out that assignment operator = is right associative. yy y #include <fstream> void SLL::read(std::string file) { std::ifstream in; std::string str; in.open(file.c_str()); in >> str; while (!in.eof()) { insert(str); in >> str; in.close(); New construct #include <fstream> void SLL::read(std::string file) { std::ifstream in; std::string str; in.open(file.c_str()); do { in >> str; while (!in.eof()); insert(str); in.close(); The do {while construct enables avoidance of code duplication. Homework: What are the constructs in your favorite language that allow you to avoid code duplication directly?

Duals break and continue while and until << and >> in C++ {x > 0 x++; {x > 1 {x > 0 x++; {?? {x > 0 {x > 0 x++; {x > 1 {x > 0 {x > 5 {x == 4 {x > 0 x == 4

{x > 0 {x > 5 {x == 4 {x > 0 {x > 0 {x > 5 {x == 4 else { {x <= 5 && x > 0 {x <= -5 && x > -10 {x > 0 {x > 5 {x == 4 else { {x > 0 {x > 5 {x == 4 else { {x <= 5 && x > 0 {x <= -5 && x > -10 {x == 4 x є (-10..-5] {x > 0 {x > 5 {x == 4 else { {x <= 5 {x > 0 if () { {x > 5 {x == 4 else { {x <= 5 && x > 0 {x <= -5 && x > -10 {x == 4 x є (-10..-5] Thought exercise: Given a set of pre and post-conditions, can we automatically generate a program?

{x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 We want to check if x becomes negative at the end of the loop. {x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x >= 0 && y >= 0 {x >= y {x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x < y && y >= 0

{x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x < y && y >= 0 Post-condition indicates that x may be negative at the end of the loop. Type of types Primitive Derived User-defined Type conversion Coersion Data Types Static versus dynamic typing Type hierarchy Static versus dynamic allocation Recursive Data Types {x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x < y && y >= 0 Post-condition indicates that x may be negative at the end of the loop; but the program doesn't say so. Procedures Abstraction: What versus How Procedures versus Functions Connection with flow-charts Declaration, Definition, Activation Parameter passing Call by value, reference, name, value-result {x >= 0 && y >= 0 {x >= y && y >= 0 {x >= 0 && y >= 0 {x < y && y >= 0 && x >= 0 x cannot be negative at the end of the loop. Multiple activations Recursion Imagining with recursion: list, tree, sort, search, factorial (more in functional paradigm) Drawing Hands, M C Escher

Static versus Dynamic Block structure Name resolution Scope