SlideShare ist ein Scribd-Unternehmen logo
1 von 39
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
17
array element
struct or union member
increment, decrement2 16

left-to-right

-- ++
!
-+
&*
sizeof
(type)

decrement, increment3
logical not
one’s complement
unary minus or plus
address or indirection
size (in bytes)
type cast

15

right-to-left

14

right-to-left

*/%

mutiplicative

13

Left-to-right

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

,

1

left-to-right

comma
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,
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

)

*

)

138 6 138
23 532 23
20 ** 120276
4 *++=== 277
276
1

13

2

3

4

5

*

+

6

*

*

+

)

*

)

)

)

+
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
14

2

3

4

5

*

+

6

*

*

+
Postfix Demo: The Evaluation
1

2

3

4

5

*

+

6

*

*

The Stack
5
6
4
20
3
138
23
2
276
1
277

4

*

5

=

20

3

+

20

=

23

23

*

6

=

138

*

138

=

276

+

276

=

277

The Answer
2
1

15

+
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-programto-convert-an-expression-from-infix-expression-to-prefix-form/#sthash.eCuEQFN6.dpuf
My lecture infix-to-postfix

Weitere ähnliche Inhalte

Was ist angesagt?

18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, MysuruNithin Kumar,VVCE, Mysuru
 
Network lab manual
Network lab manualNetwork lab manual
Network lab manualPrabhu D
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite AutomataRatnakar Mikkili
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Self-Employed
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
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
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File Harjinder Singh
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
Project of data structure
Project of data structureProject of data structure
Project of data structureUmme habiba
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in pythonKarin Lagesen
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms sathish sak
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stackHaqnawaz Ch
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 

Was ist angesagt? (20)

18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
 
Network lab manual
Network lab manualNetwork lab manual
Network lab manual
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Project of data structure
Project of data structureProject of data structure
Project of data structure
 
single linked list
single linked listsingle linked list
single linked list
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Stack
StackStack
Stack
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 

Andere mochten auch

Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversionRashmiranja625
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversionSyed Mustafa
 

Andere mochten auch (6)

Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Infix to postfix
Infix to postfixInfix to postfix
Infix to postfix
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 

Ähnlich wie My lecture infix-to-postfix

Ähnlich wie My lecture infix-to-postfix (20)

1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
5.stack
5.stack5.stack
5.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
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stack application
Stack applicationStack application
Stack application
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Applications of Stack
Applications of StackApplications of Stack
Applications of Stack
 
Stack
StackStack
Stack
 
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
 
C applications
C applicationsC applications
C applications
 
Stack
StackStack
Stack
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
 
Postfix Evaluations using Stack
Postfix Evaluations using StackPostfix Evaluations using Stack
Postfix Evaluations using Stack
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 

Kürzlich hochgeladen

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Kürzlich hochgeladen (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

My lecture infix-to-postfix

  • 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 17 array element struct or union member increment, decrement2 16 left-to-right -- ++ ! -+ &* sizeof (type) decrement, increment3 logical not one’s complement unary minus or plus address or indirection size (in bytes) type cast 15 right-to-left 14 right-to-left */% mutiplicative 13 Left-to-right 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 , 1 left-to-right comma
  • 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, 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*+
  • 13. 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 ) * ) 138 6 138 23 532 23 20 ** 120276 4 *++=== 277 276 1 13 2 3 4 5 * + 6 * * + ) * ) ) ) +
  • 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. 1 14 2 3 4 5 * + 6 * * +
  • 15. Postfix Demo: The Evaluation 1 2 3 4 5 * + 6 * * The Stack 5 6 4 20 3 138 23 2 276 1 277 4 * 5 = 20 3 + 20 = 23 23 * 6 = 138 * 138 = 276 + 276 = 277 The Answer 2 1 15 +
  • 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-programto-convert-an-expression-from-infix-expression-to-prefix-form/#sthash.eCuEQFN6.dpuf