Infix to Postfix Conversion

Similar documents
Fall, 2015 Prof. Jungkeun Park

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

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

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

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

CHAPTER 3 STACKS AND QUEUES

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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

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

Department of Computer Science

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

JAVA OPERATORS GENERAL

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

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

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Operators & Expressions

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3. Stacks And Queues

Operators in C. Staff Incharge: S.Sasirekha

Operators in java Operator operands.

Expressions and Precedence. Last updated 12/10/18

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

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

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

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

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

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Unit-2 (Operators) ANAND KR.SRIVASTAVA

STACKS AND QUEUES. Problem Solving with Computers-II

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

Operators. Java operators are classified into three categories:

Operators and Expressions:

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

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

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

CDA 3103 Computer Organization Homework #7 Solution Set

Information Science 1

Chapter 3: Operators, Expressions and Type Conversion

UNIT 3 OPERATORS. [Marks- 12]

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

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

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

Computers Programming Course 6. Iulian Năstac

Linear Data Structure

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

Engineering Computing I

DEPARTMENT OF MATHS, MJ COLLEGE

Review of the C Programming Language for Principles of Operating Systems

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

UNIT- 3 Introduction to C++

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

Arithmetic Operators. Portability: Printing Numbers

Programming in C++ 5. Integral data types

Types of Data Structures

Content: Learning Objectives

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

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

The Arithmetic Operators

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CHAPTER 8: Central Processing Unit (CPU)

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

Operators And Expressions

Part I Part 1 Expressions

Data Types and Variables in C language

Prepared by: Shraddha Modi

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

An Introduction to Trees

Constants and Variables

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

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

Review of the C Programming Language

Lecture 02 C FUNDAMENTALS

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

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

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

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

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

Data Structure - Stack and Queue-

Principles of Programming Languages COMP251: Syntax and Grammars

3. Java - Language Constructs I

SECTION II: LANGUAGE BASICS

Data Structures Week #3. Stacks

CSC 1214: Object-Oriented Programming

Stacks II. Adventures in Notation. stacks2 1

Stack and Its Implementation

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

OBJECT ORIENTED PROGRAMMING

More Programming Constructs -- Introduction

Stacks and their Applications

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Transcription:

Infix to Postfix Conversion

Infix to Postfix Conversion Stacks are widely used in the design and implementation of compilers. For example, they are used to convert arithmetic expressions from infix notation to postfix notation. An infix expression is one in which operators are located between their operands. In postfix notation, the operator immediately follows its operands.

Precedence and Priority Token Operator Precedence 1 Associativity ( ) [ ] ->. function call array element struct or union member 17 left-to-right -- ++ increment, decrement 2 16 left-to-right -- ++! - - + & * sizeof decrement, increment 3 logical not one s complement unary minus or plus address or indirection size (in bytes) 15 right-to-left (type) type cast 14 right-to-left * / % mutiplicative 13 Left-to-right

+ - binary add or subtract 12 left-to-right << >> shift 11 left-to-right > >= relational 10 left-to-right < <= ==!= equality 9 left-to-right & bitwise and 8 left-to-right ^ bitwise exclusive or 7 left-to-right bitwise or 6 left-to-right && logical and 5 left-to-right logical or 4 left-to-right

?: conditional 3 right-to-left = += -= /= *= %= <<= >>= &= ^= = assignment 2 right-to-left, comma 1 left-to-right

Examples Infix 2+3*4 a*b+5 (1+2)*7 a*b/c (a/(b-c+d))*(e-a)*c a/b-c+d*e-a*c Postfix 234*+ ab*5+ 12+7* ab*c/ abc-d+/ea-*c* ab/c-de*ac*-

Algorithm 1. Scan the expression from left to right. 2. If any operands comes print it simply 3. If any operator comes compare the incoming operator with stack operator. If the incoming operator priority is higher than stack operator priority push the incoming operator. 4. If the incoming operator has less priority than the operator inside the stack then go on popping the operator from top of the stack and print them till this condition is true and then push the incoming operator on top of the stack.. 5. If both incoming and stack operator priority are equal then pop the stack operator till this condition is true. 6. If the operator is ) then go on popping the operators from top of the stack and print them till a matching ( operator is found. Delete ( from top of the stack..

Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form, Expression Stack Output 2 Empty 2 * * 2 3 * 23 / / 23* ( /( 23* 2 /( 23*2 - /(- 23*2 1 /(- 23*21 ) / 23*21- + + 23*21-/ 5 + 23*21-/5 * +* 23*21-/53 3 +* 23*21-/53 Empty 23*21-/53*+ So, the Postfix Expression is 23*21-/53*+

Postfix Demo: The Equation Infix: (1 + (2 * ((3 + (4 * 5)) * 6))) = 277 Postfix: 1 2 3 4 5 * + 6 * * + = 277 ( 1 + ( 2 * ( ( 3 + ( 4 * 5 ) ) * 6 ) ) ) ( 1 ( 2 ( ( 3 ( 4 5 ) * ) + 6 ) * ) * ) + 420 23 138 276 * + * 5 * + 36 = 21 = 20 = 23 138 276 277 1 2 3 4 5 * + 6 * * + 13

Postfix Demo: The Stack What is a STACK? At the grocery store, on the canned goods aisle, the cans are STACKED on top of each other. Which one do we take to make sure the stack doesn t fall over? How did the store worker put the cans into the stack? Where did he or she place the new can? We take the top item and we place new items on the top. So does the computer. To evaluate the problem (1 + (2 * ((3 + (4 * 5)) * 6))), the computer uses a stack and postfix notation. 1 2 3 4 5 * + 6 * * + 14

Postfix Demo: The Evaluation 1 2 3 4 5 * + 6 * * + The Stack 5 20 64 138 23 276 2 277 1 4 * 5 = 20 3 + 20 = 23 23 * 6 = 138 The Answer 2 * 138 = 276 1 + 276 = 277 15

( ( ( A + B ) * ( C - E ) ) / ( F + G ) ) stack: <empty> output: []

( ( A + B ) * ( C - E ) ) / ( F + G ) ) stack: ( output: []

( A + B ) * ( C - E ) ) / ( F + G ) ) stack: ( ( output: []

A + B ) * ( C - E ) ) / ( F + G ) ) stack: ( ( ( output: []

+ B ) * ( C - E ) ) / ( F + G ) ) stack: ( ( ( output: [A]

B ) * ( C - E ) ) / ( F + G ) ) stack: ( ( ( + output: [A]

) * ( C - E ) ) / ( F + G ) ) stack: ( ( ( + output: [A B]

* ( C - E ) ) / ( F + G ) ) stack: ( ( output: [A B + ]

( C - E ) ) / ( F + G ) ) stack: ( ( * output: [A B + ]

C - E ) ) / ( F + G ) ) stack: ( ( * ( output: [A B + ]

- E ) ) / ( F + G ) ) stack: ( ( * ( output: [A B + C ]

E ) ) / ( F + G ) ) stack: ( ( * ( - output: [A B + C ]

) ) / ( F + G ) ) stack: ( ( * ( - output: [A B + C E ]

) / ( F + G ) ) stack: ( ( * output: [A B + C E - ]

/ ( F + G ) ) stack: ( output: [A B + C E - * ]

( F + G ) ) stack: ( / output: [A B + C E - * ]

F + G ) ) stack: ( / ( output: [A B + C E - * ]

+ G ) ) stack: ( / ( output: [A B + C E - * F ]

G ) ) stack: ( / ( + output: [A B + C E - * F ]

) ) stack: ( / ( + output: [A B + C E - * F G ]

) stack: ( / output: [A B + C E - * F G + ]

stack: <empty> output: [A B + C E - * F G + / ]

void infix :: convert( ) { char opr ; while ( *s ) { if ( *s == ' ' *s == '\t' ) { s++ ; continue ; } if ( isdigit ( *s ) isalpha ( *s ) ) { while ( isdigit ( *s ) isalpha ( *s ) ) { *t = *s ; s++ ; t-- ; } } if ( *s == ')' ) { push ( *s ) ; s++ ; } if ( *s == '*' *s == '+' *s == '/' *s == '%' *s == '-' *s == '$' ) { if ( top!= -1 ) { opr = pop( ) ; while ( priority ( opr ) > priority ( *s ) ) { *t = opr ; t-- ; opr = pop( ) ; } push ( opr ) ; push ( *s ) ; } else push ( *s ) ; s++ ; } if ( *s == '(' ) { opr = pop( ) ; while ( ( opr )!= ')' ) { *t = opr ; t-- ; opr = pop ( ) ; } s++ ; } } while ( top!= -1 ) { opr = pop( ) ; *t = opr ; t-- ; } t++ ; } - See more at: http://electrofriends.com/source-codes/software-programs/cpp-programs/cpp-data-structure/c-programto-convert-an-expression-from-infix-expression-to-prefix-form/#sthash.ecueqfn6.dpuf