Anonymous user
Parsing/RPN to infix conversion: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments, used a template for the output section.
(Added Wren) |
m (→{{header|REXX}}: added/changed whitespace and comments, used a template for the output section.) |
||
Line 3,387:
{{trans|AWK}}
{{trans|Tcl}}
A Yen symbol '''¥''' was used instead of a '''9''' to make it apparenet that it's just a placeholder.
<lang rexx>/*REXX program converts Reverse Polish Notation (RPN)
showAction = 1
oA = '◄ ◄ ◄ ◄ ►' /*the operator associations. */
say "infix: " toInfix( "3 4 2 * 1 5 - 2 3 ^ ^ / +" )
say "infix: " toInfix( "1 2 + 3 4 + ^ 5 6 + ^" ) /* [↓] Sprechen Sie Deutsch
say "infix: " toInfix( "Mond Sterne Schlamm + * Feur Suppe * ^" )
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
pop: pop= #; #= # - 1; return @.pop
push: #= # + 1; @.#= arg(1); return
/*──────────────────────────────────────────────────────────────────────────────────────*/
stack2str: $=; do j=1 for #; _ = @.j; y= left(_, 1)
if pos(' ', _)==0 then _ = '{'strip( substr(_, 2) )"}"
else _ = substr(_, 2)
$=$ '{'strip(y _)"}"
end /*j*/
return space($)
/*──────────────────────────────────────────────────────────────────────────────────────*/
toInfix: parse arg rpn; say copies('─', 80 - 1); say 'RPN: ' space(rpn)
do N=1 for words(RPN)
if pos(?,oS)==0 then
if
if showAction then say right(?, 25) "──►" stack2str()
end /*N*/
return space( substr( pop(), 2) )</lang>
<pre>
───────────────────────────────────────────────────────────────────────────────
RPN: 3 4 2 * 1 5 - 2 3 ^ ^ / +
Line 3,469 ⟶ 3,472:
Suppe ──► {3 Mond * ( Sterne + Schlamm)} {¥ Feur} {¥ Suppe}
* ──► {3 Mond * ( Sterne + Schlamm)} {3 Feur * Suppe}
^ ──► {4 ( Mond * ( Sterne + Schlamm)) ^ ( Feur * Suppe)}
infix: ( Mond * ( Sterne + Schlamm)) ^ ( Feur * Suppe)
</pre>
|