My lecture infix-to-postfix
-
Upload
senthil-kumar -
Category
Education
-
view
1.735 -
download
2
description
Transcript of My lecture infix-to-postfix
![Page 1: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/1.jpg)
Infix to Postfix Conversion
![Page 2: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/2.jpg)
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.
![Page 3: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/3.jpg)
Precedence and PriorityToken Operator Precedence1 Associativity
( )[ ]-> .
function callarray elementstruct or union member
17 left-to-right
-- ++ increment, decrement2 16 left-to-right
-- ++!-- +& *sizeof
decrement, increment3
logical notone’s complementunary minus or plusaddress or indirectionsize (in bytes)
15 right-to-left
(type) type cast 14 right-to-left
* / % mutiplicative 13 Left-to-right
![Page 4: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/4.jpg)
+ - 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
![Page 5: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/5.jpg)
?: conditional 3 right-to-left
= += -=/= *= %=<<= >>=&= ^= =
assignment 2 right-to-left
, comma 1 left-to-right
![Page 6: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/6.jpg)
Examples
Infix Postfix
2+3*4a*b+5(1+2)*7a*b/c(a/(b-c+d))*(e-a)*ca/b-c+d*e-a*c
234*+ab*5+12+7*ab*c/abc-d+/ea-*c*ab/c-de*ac*-
![Page 7: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/7.jpg)
![Page 8: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/8.jpg)
![Page 9: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/9.jpg)
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..
![Page 10: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/10.jpg)
![Page 11: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/11.jpg)
![Page 12: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/12.jpg)
Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form,
So, the Postfix Expression is 23*21-/53*+
2 Empty 2
* * 2
3 * 23
/ / 23*
( /( 23*
2 /( 23*2
- /(- 23*2
1 /(- 23*21
) / 23*21-
+ + 23*21-/
5 + 23*21-/5
3 +* 23*21-/53
Expression Stack Output
* +* 23*21-/53
Empty 23*21-/53*+
![Page 13: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/13.jpg)
13
Postfix Demo: The EquationInfix: (1 + (2 * ((3 + (4 * 5)) * 6)))
Postfix: 1 2 3 4 5 * + 6 * * +
3+1 ( 2 (* ( (+ 4 5* ) *) 6 ) )( )
( 4 5 *)3( +)( *6 )( 2 *) +1( )
3 +1 2 *+4 5 * *6
4 * 5 = 2020 + 3 = 2323 * 6 = 138138 * 2 = 276276 + 1 = 277
= 277
= 277
![Page 14: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/14.jpg)
14
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.
3 +1 2 *+4 5 * *6
![Page 15: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/15.jpg)
15
Postfix Demo: The Evaluation3 +1 2 *+4 5 * *6
3
+
1
2
*
+
4
5
*
*4 5 = 2020
203 = 2323
6
623 = 138
138
1382 = 276
276
2761 = 277
277
The Stack
The Answer
![Page 16: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/16.jpg)
FPE Infix to Postfix
( ( ( A + B ) * ( C - E ) ) / ( F + G ) )
• stack: <empty>• output: []
![Page 17: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/17.jpg)
FPE Infix to Postfix
( ( A + B ) * ( C - E ) ) / ( F + G ) )
• stack: (• output: []
![Page 18: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/18.jpg)
FPE Infix to Postfix
( A + B ) * ( C - E ) ) / ( F + G ) )
• stack: ( (• output: []
![Page 19: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/19.jpg)
FPE Infix to Postfix
A + B ) * ( C - E ) ) / ( F + G ) )
• stack: ( ( (• output: []
![Page 20: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/20.jpg)
FPE Infix to Postfix
+ B ) * ( C - E ) ) / ( F + G ) )
• stack: ( ( (• output: [A]
![Page 21: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/21.jpg)
FPE Infix to Postfix
B ) * ( C - E ) ) / ( F + G ) )
• stack: ( ( ( +• output: [A]
![Page 22: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/22.jpg)
FPE Infix to Postfix
) * ( C - E ) ) / ( F + G ) )
• stack: ( ( ( +• output: [A B]
![Page 23: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/23.jpg)
FPE Infix to Postfix
* ( C - E ) ) / ( F + G ) )
• stack: ( ( • output: [A B + ]
![Page 24: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/24.jpg)
FPE Infix to Postfix
( C - E ) ) / ( F + G ) )
• stack: ( ( * • output: [A B + ]
![Page 25: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/25.jpg)
FPE Infix to Postfix
C - E ) ) / ( F + G ) )
• stack: ( ( * (• output: [A B + ]
![Page 26: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/26.jpg)
FPE Infix to Postfix
- E ) ) / ( F + G ) )
• stack: ( ( * (• output: [A B + C ]
![Page 27: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/27.jpg)
FPE Infix to Postfix
E ) ) / ( F + G ) )
• stack: ( ( * ( -• output: [A B + C ]
![Page 28: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/28.jpg)
FPE Infix to Postfix
) ) / ( F + G ) )
• stack: ( ( * ( -• output: [A B + C E ]
![Page 29: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/29.jpg)
FPE Infix to Postfix
) / ( F + G ) )
• stack: ( ( *• output: [A B + C E - ]
![Page 30: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/30.jpg)
FPE Infix to Postfix
/ ( F + G ) )
• stack: ( • output: [A B + C E - * ]
![Page 31: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/31.jpg)
FPE Infix to Postfix
( F + G ) )
• stack: ( /• output: [A B + C E - * ]
![Page 32: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/32.jpg)
FPE Infix to Postfix
F + G ) )
• stack: ( / (• output: [A B + C E - * ]
![Page 33: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/33.jpg)
FPE Infix to Postfix
+ G ) )
• stack: ( / (• output: [A B + C E - * F ]
![Page 34: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/34.jpg)
FPE Infix to Postfix
G ) )
• stack: ( / ( +• output: [A B + C E - * F ]
![Page 35: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/35.jpg)
FPE Infix to Postfix
) )
• stack: ( / ( +• output: [A B + C E - * F G ]
![Page 36: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/36.jpg)
FPE Infix to Postfix
)
• stack: ( /• output: [A B + C E - * F G + ]
![Page 37: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/37.jpg)
FPE Infix to Postfix
• stack: <empty>• output: [A B + C E - * F G + / ]
![Page 38: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/38.jpg)
• 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-program-to-convert-an-expression-from-infix-expression-to-prefix-form/#sthash.eCuEQFN6.dpuf
![Page 39: My lecture infix-to-postfix](https://reader033.fdocuments.net/reader033/viewer/2022061122/547097deb4af9fc80a8b47f1/html5/thumbnails/39.jpg)