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.
 
<br>The same reasoning was used for the ''operator associations'' &nbsp; (the left &nbsp; ◄ &nbsp; and right &nbsp; ► &nbsp; arrow symbols).
<lang rexx>/*REXX program converts Reverse Polish Notation (RPN) ──► ───► an infix notation. */
showAction = 1 /* 0 if no showActions wanted. */
showAction = 1 # = 0 /*initialize stack pointer0 to 0if no showActions wanted. */
oS # = '+0 - / * ^' /*operator symbols. /*initialize stack pointer to 0 (zero).*/
oPoS = '2+ 2- 3/ 3* 4^' /*the operator prioritiessymbols. */
oAoP = '2 2 3 3 4' /*the operator associationspriorities. */
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); ?=word(RPN,N) /*process each of the RPN tokens.*/
if pos( ?= word(RPN,oS N)==0 then call push '¥' ? /*whenobtain innext doubt,item addin athe Yenlist. to it.*/
if pos(?,oS)==0 then elsecall do;push '¥' ? g=pop(); /*when in gp=left(gdoubt, 1);add a Yen to g=substr(g, 2)it.*/
h else do; g= pop(); hpgp= left(hg, 1); hg= substr(hg, 2)
tp h=substr pop(oP,pos); hp= left(?h, oS1),; 1 h= substr(h, 2)
ta tp= substr(oAoP, pos(?, oS), 1)
if hp<tp | (hp==tp & ta=='►') then h ta=" substr("h"oA, pos(?, oS), 1)"
if gphp<tp | (gphp==tp & ta=='') then gh= "("gh")"
call push if gp<tp || h(gp==tp & ?ta=='◄') then g= "("g")"
end call push tp || h ? g
if showAction then say right(?,25) "──►" stack2str() end
if showAction then say right(?, 25) "──►" stack2str()
end /*N*/
 
return space( substr( pop(), 2) )</lang>
'''{{out|output'''|text=&nbsp; when using the default inputinputs: &nbsp; &nbsp; &nbsp; [Output is very similar to AWK's output.]}}
<pre>
<br>[Output is very similar to AWK's output.]
<pre style="height:55ex">
───────────────────────────────────────────────────────────────────────────────
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>