Evaluation of postfix

Similar documents
Postscript Intro. References. What is Postscript? Hello World! in Ghostscript. CSE 413, Autumn 2005 Programming Languages. Page description language

Hello world. 2. Stack-based Programming. Oscar Nierstrasz

Package epsdice a scalable dice font

Infix to Postfix Conversion

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Informatique Graphique. Traitement de l'information et automatisation. Traitement de l'information et automatisation

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

Expressions and Assignment

Text and Graphics. Postcript is a Page Description Language! Paths

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

References. Topic #15: Postscript Intro. What is Postscript? Hello World! in Ghostscript. Page Description Language. Hello World!

Prefix/Infix/Postfix Notation

Code Generation: Introduction

Postscript Control Flow

A Short Introduction to PostScript

Joseph E. Coulson, Jr. Director. ~~/JVt~ (advisor's signature) Ball State University. Muncie, Indiana. February 29, Date of Graduation:

Operators. Java operators are classified into three categories:

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

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

Emulation of the execform Operator

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Lecture P5: Abstract Data Types

Timing Techniques. Adobe Developer Support. Technical Note # March Adobe Systems Incorporated

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

SOFTWARE ARCHITECTURE 4. TEXT FORMATTING SYSTEM

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Review of the C Programming Language for Principles of Operating Systems

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

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

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

Department of Computer Science

Drawing curves automatically: procedures as arguments

WARNING for Autumn 2004:

Expressions and Precedence. Last updated 12/10/18

Review of the C Programming Language

Languages and Compilers (SProg og Oversættere)

Stacks and Queues. !stacks!dynamic resizing!queues!generics!applications. Stacks and Queues

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

Chapter 12 Variables and Operators

CS 206 Introduction to Computer Science II

CHAPTER 8: Central Processing Unit (CPU)

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

A flow chart is a graphical or symbolic representation of a process.

LECTURE 17. Expressions and Assignment

Color Separation Conventions for PostScript Language Programs

Fall, 2015 Prof. Jungkeun Park

CDA 3103 Computer Organization Homework #7 Solution Set

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Chapter 04: Instruction Sets and the Processor organizations. Lesson 18: Stack-based processor Organisation

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

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

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

JAVA OPERATORS GENERAL

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

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

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

The Arithmetic Operators

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Expression Evaluation and Control Flow. Outline

Computers Programming Course 6. Iulian Năstac

Drawing polygons: loops and arrays

Operators & Expressions

Drawing polygons: loops and arrays

The New C Standard (Excerpted material)

CD and DVD labels TIMTOWTDI. Introduction

Chapter 3: Operators, Expressions and Type Conversion

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

Exercises. Computer Tech Tom Browder Northwest Florida Linux User Group [nwflug.org]

Operators in C. Staff Incharge: S.Sasirekha

BBM 201 DATA STRUCTURES

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

BBM 201 DATA STRUCTURES

UNIT- 3 Introduction to C++

Information Science 1

Chapter 6 Control Flow. June 9, 2015

An Introduction to Trees

This book is licensed under a Creative Commons Attribution 3.0 License

Arithmetic Operators. Portability: Printing Numbers

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Control Flow February 9, Lecture 7

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS 206 Introduction to Computer Science II

Unit 3. Operators. School of Science and Technology INTRODUCTION

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stars around I PostScript straightaway

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Fall 2018 Instructor: Bills

15-122: Principles of Imperative Computation, Fall 2015

Learning Forth. Developer Technical Support DTS. for Macintosh Open Firmware (Part I) 2/1/01 version 0.9 (part I) 1

Linear Data Structure

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Chapter 7. Expressions and Assignment Statements ISBN

Data Structures Week #3. Stacks

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University

CS W3134: Data Structures in Java

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

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Transcription:

Postfix Infix notation: Operator appears between operands: 2 + 3 5 3 + 6 9 Implied precedence: 2 + 3 * 4 2 + (3 * 4 ), not (2 + 3 ) * 4 Prefix notation: Operator precedes operands: + 2 3 5 + 2 * 3 5 (+ 2 ( * 3 5 ) ) + 2 15 17 Postfix notation: Operator follows operands: 2 3 + 5 2 3 * 5 + (( 2 3 * 5 +) 6 5 + 11 Called Polish postfix since few could pronounce Polish mathematician Lukasiewicz, who invented it. An interesting, but unimportant mathematical curiosity when presented in 1920s. Only became important in 1950s when Burroughs rediscovered it for their ALGOL compiler. 1

Evaluation of postfix 1. If argument is an operand, stack it. 2. If argument is an n-ary operator, then the n arguments are already on the stack. Pop the n arguments from the stack and replace by the value of the operator applied to the arguments. Example: 2 3 4 + 5 * + 1. 2 - stack 2. 3 - stack 3. 4 - stack 4. + - replace 3 and 4 on stack by 7 5. 5 - stack 6. * - replace 5 and 7 on stack by 35 7. + - replace 35 and 2 on stack by 37 2

Importance of Postfix to Compilers Code generation same as expression evaluation. To generate code for 2 3 4 + 5 * +, do: 1. 2 - stack L-value of 2 2. 3 - stack L-value of 3 3. 4 - stack L-value of 4 4. + - generate code to take R-value of top stack element (L-value of 4) and add to R-value of next stack element (L-value of 3) and place L-value of result on stack 5. 5 - stack L-value of 5 6. * - generate code to take R-value of top stack element (L-value of 5) and multiply to R-value of next stack element (L-value of 7) and place L-value of result on stack 7. + - generate code to take R-value of top stack element (L-value of 35) and add to R-value of next stack element (L-value of 2) and place L-value of result (37) on stack 3

Forth - A language based on postfix Postfix source language leads to an efficient execution model, even though generally interpreted. System runs on two stacks - a subroutine return stack and an expression evaluation stack. Run-time model very small making it useful on small embedded computers. Forth was developed by Charles Moore around 1970. The name was a contraction of Fourth Generation Programming Language with the program name limited to five characters. The language was a replacement for FORTRAN on small minicomputers in the 1970s where space was at a premium and the only input-output device was often a very slow and cumbersome paper tape. Having a resident translator/interpreter made for easy program development on the target system. 4

Example Forth program Program to compute: 1 2 +2 2 +... +9 2 +10 2 $ [Notation: a,b,c is expression stack. c is stack(top)] : SQR DUP * ; (Defines square by: n n,n (n*n)) : DOSUM SWAP 1 + SWAP OVER SQR + ; Execution: ( N,S N+1,S+(N+1) 2 ) ( N,S S,N S,(N+1) (N+1),S (N+1),S, N+1) (N+1),S, N+1) 2 (N+1),S+(N+1) 2 ) 3 6 DOSUM.. 22 4 ok (Period (.) prints stack(top). Output is 22 = 4 2 +6) 0 0 10 0 DO DOSUM LOOP. 385 ok (Apply DOSUM from 0 to 9 (Stop at 10)) }} 5

Postscript Postscript is Forth with painting commands added. 1: %Same as Forth program 2: /Helvetica findfont 3: 20 scalefont 4: setfont 5: 200 400 moveto 6: /formatit {10 10 string cvrs show} def 7: /sqr {dup mul} def 8: /dosum {exch 1 add exch 1 index sqr add} def 9: 3 6 dosum 2 copy formatit ( ) show formatit 10: clear 11: 200 375 moveto 12: 0 0 0 1 9 {pop dosum} for formatit 13: showpage 6

Postscript painting commands 14: % Lets draw a truck 15: /box {newpath 0 0 moveto 0 1 lineto 3 1 lineto 3 0 lineto 16: closepath} def 17:.1 setlinewidth 0 setgray 18: gsave 19: 72 72 scale 20: 2 5 translate box stroke 21: 3.2 0 translate.5.5 scale box fill 22: 0 1 translate.6.6 scale box fill 23: grestore 24: /tire {newpath 1 0 moveto 0 0 1 0 360 arc closepath} def 25:.5 setlinewidth 10 10 scale 26: 16 34 translate tire stroke 27: 3 0 translate tire stroke 28: 17 0 translate tire stroke 29: 3 0 translate tire stroke 30: 8 0 translate tire stroke 13: showpage 7

Precedence of operators Assumed order of evaluation for arithmetic expressions: 2*3+4*5 assumed to be 26 since assumed ordering is (2*3)+(4*5). This is specified by precedence of operators. In any expression, the highest precedence operations are evaluated first, and so on. Most languages have an implied precedence. APL and Smalltalk do not. Neither language emphasizes arithmetic data, so it is not clear what precedence means in this case. C has 17 levels of precedence (given next) 8

C precedence levels Precedence Operators Operator names 17 tokens, a[k], f()literals, subscripting, function call.,-> Selection 16 ++, -- Postfix increment/decrement 15* ++, -- Prefix inc/dec, -, sizeof Unary operators, storage!,&,* Logical negation, indirection 14 typename Casts 13 *, /, % Multiplicative operators 12 +,- Additive operators 11 <<, >> Shift 10 <,>,<=, >= Relational 9 ==,!= Equality 8 & Bitwise and 7 Bitwise xor 6 Bitwise or 5 && Logical and 4 Logical or 3?: Conditional 2 =, +=, -=, *=, Assignment /=, %=, <<=, >>=, &=, =, = 1, Sequential evaluation 9