Infix to postfix conversion - PowerPoint PPT Presentation

About This Presentation
Title:

Infix to postfix conversion

Description:

Infix to postfix conversion. Read the tokens from a vector infixVect of tokens (strings) of an infix expression ... Infix to postfix conversion. stackVect ... – PowerPoint PPT presentation

Number of Views:1301
Avg rating:3.0/5.0
Slides: 21
Provided by: CSE138
Learn more at: http://csci.biola.edu
Category:

less

Transcript and Presenter's Notes

Title: Infix to postfix conversion


1
Infix to postfix conversion
  • Use a loop to read the tokens one by one from a
    vector infixVect of tokens (strings) representing
    an infix expression.
  • For each token do the following in the loop
  • When the token is an operand
  • Add it to the end of the vector postfixVect of
    token (strings) that is used to store the
    corresponding postfix expression
  • When the token is a left parenthesis (
  • Push_back the token x to the end of the vector
    stackVect of token (strings) that simulates a
    stack.
  • When the token is a right parenthesis )
  • Repeatedly pop_back a token y from stackVect and
    push_back that token y to postfixVect until (
    is encountered in stackVect. Then pop_back (
    from stackVect.
  • If stackVect is already empty before finding a
    (, that expression is not a valid expression.
  • When the token is an operator, see next slide.

2
Infix to postfix conversion
  • When the token x is an operator
  • Write a loop that checks the following three
    conditions
  • The stack stackVect is not empty
  • The token y currently in the end of stackVect is
    an operator. In other words, it is not not a lef
    parenthesis ( .
  • y is an operator of higer or equal precedence
    than that of x,
  • As long as the three conditions above are all
    true, in the loop above do the following in the
    body of the loop
  • push_back the token y to postfixVect
  • pop_back the token y from stackVect
  • The loop above will stops as soon as any of the
    three conditions is not true.
  • After the loop, push_back the token x into
    stackVect.

3
Infix to postfix conversion
  • After the loop in slide 1 has processes all the
    tokens in infixVect and stop,
  • use another loop to repeatedly do the following
    as long as the stack vector stackVect is not
    empty yet
  • push_back the token on the top of the stack
    vector stackVect into postfixVect.
  • pop_back the stack vector to remove the top token
    y the stack.

4
Infix to postfix conversion
infixVect
( a b - c ) d ( e f )
postfixVect
5
Infix to postfix conversion
stackVect
infixVect
a b - c ) d ( e f )
postfixVect
(
6
Infix to postfix conversion
stackVect
infixVect
b - c ) d ( e f )
postfixVect
a
(
7
Infix to postfix conversion
stackVect
infixVect
b - c ) d ( e f )
postfixVect
a

(
8
Infix to postfix conversion
stackVect
infixVect
- c ) d ( e f )
postfixVect
a b

(
9
Infix to postfix conversion
stackVect
infixVect
c ) d ( e f )
postfixVect
a b
-
(
10
Infix to postfix conversion
stackVect
infixVect
) d ( e f )
postfixVect
a b c
-
(
11
Infix to postfix conversion
stackVect
infixVect
d ( e f )
postfixVect
a b c -
12
Infix to postfix conversion
stackVect
infixVect
d ( e f )
postfixVect
a b c -

13
Infix to postfix conversion
stackVect
infixVect
( e f )
postfixVect
a b c - d

14
Infix to postfix conversion
stackVect
infixVect
( e f )
postfixVect
a b c d
-
15
Infix to postfix conversion
stackVect
infixVect
e f )
postfixVect
a b c d
(
-
16
Infix to postfix conversion
stackVect
infixVect
f )
postfixVect
a b c d e
(
-
17
Infix to postfix conversion
stackVect
infixVect
f )
postfixVect
a b c d e

(
-
18
Infix to postfix conversion
stackVect
infixVect
)
postfixVect
a b c d e f

(
-
19
Infix to postfix conversion
stackVect
infixVect
postfixVect
a b c d e f
-
20
Infix to postfix conversion
stackVect
infixVect
postfixVect
a b c d e f -
Write a Comment
User Comments (0)
About PowerShow.com