Infix to postfix algorithm
By rhodium
- 1203 reads
This algorithm converts infix to postfix.
And evaluates it.
By Salim Meghani BSc(Hons).
For single digit positive integers only.
Use \% as the first and last character (terminators).
From Tanenbaum's 'Structured Computer Organization' (3rd ed).
Define: The switch, texas line, california line, newyork line.
Define the functions : Push texas, Pop texas, Deleteboth, Put
California,
Error.
Begin while.
Input the expression into the newyork data structure using
terminators.
Let the switch become the first character in this structure.
Push the terminator on the texas stack.
DO the following repeatedly.
If the switch is a number put it onto the California line and goto the
end of the loop.
If the switch is either \%+-) and the top of texas is either +*-/ pop
the character off
the texas line and put it onto the california line and goto the end of
the loop.
If the switch is either */ and the top of texas is either */ pop the
character off the
texas line and put it onto the california line and goto the end of the
loop.
If the switch is \% and top of texas is ( then call error and
stop.
If the switch is either +-*/( and top of texas is either (\% push the
switch onto texas
stack and goto end of loop.
If the switch is either */ and top of texas is either +- push the
switch onto texas
stack and goto end of loop.
If the switch is ( and texas is either +*-/( push the switch onto texas
stack
and goto end of loop.
If the switch is ) and texas is \% then call error and stop.
If the switch is ) and texas ( then delete both switch and pop texas
and goto
end of loop.
End of loop.
If switch is \% and texas is \% then end while loop.
When pushing texas, the next character from newyork is assigned to the
switch.
When popping texas, the california line is assigned the character at
texas.
When deleting both, then top of texas is deleted and next newyork
character
is assigned to the switch.
When putting to california, then the switch is assigned to california
and
switch then becomes next character from newyork line.
- Log in to post comments


