SlideShare a Scribd company logo
1 of 39
Download to read offline
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 Precedence1
Associativity
( )
[ ]
-> .
function call
array element
struct or union member
17 left-to-right
-- ++ increment, decrement2
16 left-to-right
-- ++
!
-
- +
& *
sizeof
decrement, increment3
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 Postfix
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
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,
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*+
13
Postfix Demo: The Equation
Infix: (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 = 20
20 + 3 = 23
23 * 6 = 138
138 * 2 = 276
276 + 1 = 277
= 277
= 277
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
15
Postfix Demo: The Evaluation
3 +
1 2 *
+
4 5 * *
6
3
+
1
2
*
+
4
5
*
*
4 5 = 20
20
20
3 = 23
23
6
6
23 = 138
138
138
2 = 276
276
276
1 = 277
277
The Stack
The Answer
FPE Infix to Postfix
( ( ( A + B ) * ( C - E ) ) / ( F + G ) )
• stack: <empty>
• output: []
FPE Infix to Postfix
( ( A + B ) * ( C - E ) ) / ( F + G ) )
• stack: (
• output: []
FPE Infix to Postfix
( A + B ) * ( C - E ) ) / ( F + G ) )
• stack: ( (
• output: []
FPE Infix to Postfix
A + B ) * ( C - E ) ) / ( F + G ) )
• stack: ( ( (
• output: []
FPE Infix to Postfix
+ B ) * ( C - E ) ) / ( F + G ) )
• stack: ( ( (
• output: [A]
FPE Infix to Postfix
B ) * ( C - E ) ) / ( F + G ) )
• stack: ( ( ( +
• output: [A]
FPE Infix to Postfix
) * ( C - E ) ) / ( F + G ) )
• stack: ( ( ( +
• output: [A B]
FPE Infix to Postfix
* ( C - E ) ) / ( F + G ) )
• stack: ( (
• output: [A B + ]
FPE Infix to Postfix
( C - E ) ) / ( F + G ) )
• stack: ( ( *
• output: [A B + ]
FPE Infix to Postfix
C - E ) ) / ( F + G ) )
• stack: ( ( * (
• output: [A B + ]
FPE Infix to Postfix
- E ) ) / ( F + G ) )
• stack: ( ( * (
• output: [A B + C ]
FPE Infix to Postfix
E ) ) / ( F + G ) )
• stack: ( ( * ( -
• output: [A B + C ]
FPE Infix to Postfix
) ) / ( F + G ) )
• stack: ( ( * ( -
• output: [A B + C E ]
FPE Infix to Postfix
) / ( F + G ) )
• stack: ( ( *
• output: [A B + C E - ]
FPE Infix to Postfix
/ ( F + G ) )
• stack: (
• output: [A B + C E - * ]
FPE Infix to Postfix
( F + G ) )
• stack: ( /
• output: [A B + C E - * ]
FPE Infix to Postfix
F + G ) )
• stack: ( / (
• output: [A B + C E - * ]
FPE Infix to Postfix
+ G ) )
• stack: ( / (
• output: [A B + C E - * F ]
FPE Infix to Postfix
G ) )
• stack: ( / ( +
• output: [A B + C E - * F ]
FPE Infix to Postfix
) )
• stack: ( / ( +
• output: [A B + C E - * F G ]
FPE Infix to Postfix
)
• stack: ( /
• output: [A B + C E - * F G + ]
FPE Infix to Postfix
• 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-program-
to-convert-an-expression-from-infix-expression-to-prefix-form/#sthash.eCuEQFN6.dpuf
1.3- infix-ti-postfix.pdf

More Related Content

Similar to 1.3- infix-ti-postfix.pdf

Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monadskenbot
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfixSelf-Employed
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Ahmed Khateeb
 
Postfix Evaluations using Stack
Postfix Evaluations using StackPostfix Evaluations using Stack
Postfix Evaluations using StackSoumen Santra
 
data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3jateno3396
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfixecomputernotes
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7ecomputernotes
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)IoT Code Lab
 

Similar to 1.3- infix-ti-postfix.pdf (20)

Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
 
C applications
C applicationsC applications
C applications
 
Stack application
Stack applicationStack application
Stack application
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stack
StackStack
Stack
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Postfix Evaluations using Stack
Postfix Evaluations using StackPostfix Evaluations using Stack
Postfix Evaluations using Stack
 
data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3
 
Stack
StackStack
Stack
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfix
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7
 
Team 4
Team 4Team 4
Team 4
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 

Recently uploaded

꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 

Recently uploaded (20)

꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 

1.3- infix-ti-postfix.pdf

  • 1. Infix to Postfix Conversion
  • 2. 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.
  • 3. Precedence and Priority Token Operator Precedence1 Associativity ( ) [ ] -> . function call array element struct or union member 17 left-to-right -- ++ increment, decrement2 16 left-to-right -- ++ ! - - + & * sizeof decrement, increment3 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
  • 4. + - 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
  • 5. ?: conditional 3 right-to-left = += -= /= *= %= <<= >>= &= ^=  = assignment 2 right-to-left , comma 1 left-to-right
  • 7.
  • 8.
  • 9. 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..
  • 10.
  • 11.
  • 12. 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*+
  • 13. 13 Postfix Demo: The Equation Infix: (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 = 20 20 + 3 = 23 23 * 6 = 138 138 * 2 = 276 276 + 1 = 277 = 277 = 277
  • 14. 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
  • 15. 15 Postfix Demo: The Evaluation 3 + 1 2 * + 4 5 * * 6 3 + 1 2 * + 4 5 * * 4 5 = 20 20 20 3 = 23 23 6 6 23 = 138 138 138 2 = 276 276 276 1 = 277 277 The Stack The Answer
  • 16. FPE Infix to Postfix ( ( ( A + B ) * ( C - E ) ) / ( F + G ) ) • stack: <empty> • output: []
  • 17. FPE Infix to Postfix ( ( A + B ) * ( C - E ) ) / ( F + G ) ) • stack: ( • output: []
  • 18. FPE Infix to Postfix ( A + B ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( • output: []
  • 19. FPE Infix to Postfix A + B ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( ( • output: []
  • 20. FPE Infix to Postfix + B ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( ( • output: [A]
  • 21. FPE Infix to Postfix B ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( ( + • output: [A]
  • 22. FPE Infix to Postfix ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( ( + • output: [A B]
  • 23. FPE Infix to Postfix * ( C - E ) ) / ( F + G ) ) • stack: ( ( • output: [A B + ]
  • 24. FPE Infix to Postfix ( C - E ) ) / ( F + G ) ) • stack: ( ( * • output: [A B + ]
  • 25. FPE Infix to Postfix C - E ) ) / ( F + G ) ) • stack: ( ( * ( • output: [A B + ]
  • 26. FPE Infix to Postfix - E ) ) / ( F + G ) ) • stack: ( ( * ( • output: [A B + C ]
  • 27. FPE Infix to Postfix E ) ) / ( F + G ) ) • stack: ( ( * ( - • output: [A B + C ]
  • 28. FPE Infix to Postfix ) ) / ( F + G ) ) • stack: ( ( * ( - • output: [A B + C E ]
  • 29. FPE Infix to Postfix ) / ( F + G ) ) • stack: ( ( * • output: [A B + C E - ]
  • 30. FPE Infix to Postfix / ( F + G ) ) • stack: ( • output: [A B + C E - * ]
  • 31. FPE Infix to Postfix ( F + G ) ) • stack: ( / • output: [A B + C E - * ]
  • 32. FPE Infix to Postfix F + G ) ) • stack: ( / ( • output: [A B + C E - * ]
  • 33. FPE Infix to Postfix + G ) ) • stack: ( / ( • output: [A B + C E - * F ]
  • 34. FPE Infix to Postfix G ) ) • stack: ( / ( + • output: [A B + C E - * F ]
  • 35. FPE Infix to Postfix ) ) • stack: ( / ( + • output: [A B + C E - * F G ]
  • 36. FPE Infix to Postfix ) • stack: ( / • output: [A B + C E - * F G + ]
  • 37. FPE Infix to Postfix • stack: <empty> • output: [A B + C E - * F G + / ]
  • 38. • 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