Parsing/RPN to infix conversion: Difference between revisions

m
(→‎{{header|Haskell}}: Fixed typo which broke compilation. Added test in main(). Applied Hlint, Ormolu.)
 
(7 intermediate revisions by 6 users not shown)
Line 43:
{{trans|Java}}
 
<langsyntaxhighlight lang="11l">-V ops = ‘-+/*^’
 
F postfix_to_infix(postfix)
Line 89:
print(‘Postfix : ’e)
print(‘Infix : ’postfix_to_infix(e))
print()</langsyntaxhighlight>
 
{{out}}
Line 126:
=={{header|Ada}}==
Using the solution of the task [[stack]]:
<syntaxhighlight lang="ada">
<lang Ada>
type Priority is range 1..4;
type Infix is record
Line 209:
end;
end Convert;
</syntaxhighlight>
</lang>
The test program:
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
Line 224:
Put_Line (Convert ("1 2 + 3 4 + ^ 5 6 + ^"));
end RPN_to_Infix;
</syntaxhighlight>
</lang>
should produce the following output
<pre>
Line 236:
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
Recursively parses the RPN string backwards to build a parse tree which is then printed.
<langsyntaxhighlight lang="algol68">
# rpn to infix - parses an RPN expression and generates the equivalent #
# infix expression #
Line 468:
 
)
</syntaxhighlight>
</lang>
{{out}}<pre>
parsing expression from: 3 4 2 * 1 5 - 2 3 ^ ^ / +
Line 525:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
<langsyntaxhighlight AHKlang="ahk">expr := "3 4 2 * 1 5 - 2 3 ^ ^ / +"
 
stack := {push: func("ObjInsert"), pop: func("ObjRemove")}
Line 570:
Space(n){
return n>0 ? A_Space Space(n-1) : ""
}</langsyntaxhighlight>
;Output
<pre style="height:30ex;overflow:scroll;">TOKEN ACTION STACK (comma separated)
Line 595:
The kludge is prepending the precedence on the front of the expressions stored on the stack. This shows up when the tail() function is used, and when 'x' is prepended as a placeholder when adding parenthesis.
 
<langsyntaxhighlight lang="awk">#!/usr/bin/awk -f
 
BEGIN {
Line 697:
return s
}
</syntaxhighlight>
</lang>
 
Output:
Line 745:
=={{header|C}}==
Takes RPN string from command line, string must be enclosed in double quotes. This is necessary since / and ^ are control characters for the command line. The second string, which can be any valid string, is optional and if supplied, the expression tree is printed out as it is built. The final expression is printed out in both cases.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<string.h>
Line 871:
return 0;
}
</syntaxhighlight>
</lang>
Output, both final and traced outputs are shown:
<pre>
Line 914:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 1,014:
}
}
}</langsyntaxhighlight>
<pre>3 -> [3]
4 -> [3, 4]
Line 1,046:
=={{header|C++}}==
Very primitive implementation, doesn't use any parsing libraries which would shorten this greatly.
<syntaxhighlight lang="cpp">
<lang Cpp>
#include <iostream>
#include <stack>
Line 1,125:
}
}
</syntaxhighlight>
</lang>
Output :
<pre>
Line 1,134:
=={{header|Common Lisp}}==
Tested on ABCL.
<langsyntaxhighlight lang="lisp">
;;;; Parsing/RPN to infix conversion
(defstruct (node (:print-function print-node)) opr infix)
Line 1,194:
(format t "~%Parsing:\"~A\"~%" expr)
(format t "RPN:\"~A\" INFIX:\"~A\"~%" expr (parse expr)))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,293:
=={{header|D}}==
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.array;
 
void parseRPN(in string e) {
Line 1,335:
"1 2 + 3 4 + ^ 5 6 + ^"])
parseRPN(test);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,427:
===Alternative Version===
{{trans|Raku}}
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.array, std.algorithm;
 
void rpmToInfix(in string str) @safe {
Line 1,461:
"3 4 2 * 1 5 - 2 3 ^ ^ / +".rpmToInfix;
"1 2 + 3 4 + ^ 5 6 + ^".rpmToInfix;
}</langsyntaxhighlight>
{{out}}
<pre>=================
Line 1,499:
For convenience, modularity, reusability, and the fun of it, we split the task into two parts. '''rpn->infix''' checks the rpn expression and builds an infix - lisp - tree (which can be the input of an infix calculator). '''infix->string''' takes a tree in input and builds the required string.
 
<langsyntaxhighlight lang="scheme">
(require 'hash)
(string-delimiter "")
Line 1,560:
(when (right-par? node) (set! rhs (string-append "(" rhs ")")))
(string-append lhs " " node.op " " rhs))]))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,601:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">type ast =
| Num of int
| Add of ast * ast | Sub of ast * ast
Line 1,649:
Seq.iter (printf " %s") (infix 0 tree); printfn ""
0
</syntaxhighlight>
</lang>
Input is given via the command line.
Output includes the abstract syntax tree generated for the input.
Line 1,661:
=={{header|Go}}==
No error checking.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,723:
}
fmt.Println("infix:", stack[0].expr)
}</langsyntaxhighlight>
Output:
<pre>
Line 1,816:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class PostfixToInfix {
static class Expression {
final static String ops = "-+/*^"
Line 1,874:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Postfix : 3 4 2 * 1 5 - 2 3 ^ ^ / +
Line 1,911:
This solution is a rough translation of the solution provided on RubyQuiz, as linked above.
 
<syntaxhighlight lang="haskell">import Debug.Trace
<lang Haskell>data Expression = Const String | Exp Expression String Expression
 
data Expression = Const String | Exp Expression String Expression
precedence :: Expression -> Int
precedence (Const _) = 5
precedence (Exp _ op _)
| op == "^" = 4
| op `elem` ["*", "/"] = 3
| op `elem` ["+", "-"] = 2
| otherwise = 0
 
------------- INFIX EXPRESSION FROM RPN STRING -----------
leftAssoc :: Expression -> Bool
leftAssoc (Const _) = False
leftAssoc (Exp _ op _) = op `notElem` ["^", "*", "+"]
 
infixFromRPN :: String -> Expression
rightAssoc :: Expression -> Bool
infixFromRPN = head . foldl buildExp [] . words
rightAssoc (Const _) = False
rightAssoc (Exp _ op _) = op == "^"
 
buildExp :: [Expression] -> String -> [Expression]
buildExp stack x
| (not . isOp) x =
let v = Const x : stack
in trace (show v) v
| otherwise =
let v = Exp l x r : rest
in trace (show v) v
where
r : l : rest = stack
isOp = (`elem` ["^", "*", "/", "+", "-"])
 
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_
( \s ->
putStr (s <> "\n-->\n")
>> (print . infixFromRPN)
s
>> putStrLn []
)
[ "3 4 2 * 1 5 - 2 3 ^ ^ / +",
"1 2 + 3 4 + ^ 5 6 + ^",
"1 4 + 5 3 + 2 3 * * *",
"1 2 * 3 4 * *",
"1 2 + 3 4 + +"
]
 
---------------------- SHOW INSTANCE ---------------------
instance Show Expression where
show (Const x) = x
Line 1,949 ⟶ 1,971:
opPrec = precedence exp
 
buildExpleftAssoc :: [Expression] -> String -> [Expression]Bool
leftAssoc (Const _) = False
buildExp stack x
leftAssoc (Exp _ op _) = op `notElem` ["^", "*", "+"]
| not . isOp $ x = Const x : stack
| otherwise = Exp l x r : rest
where
r : l : rest = stack
isOp = (`elem` ["^", "*", "/", "+", "-"])
 
rightAssoc :: Expression -> Bool
main :: IO ()
rightAssoc (Const _) = False
main =
rightAssoc (Exp _ op _) = op == "^"
mapM_
 
(print . head . foldl buildExp [] . words)
precedence :: Expression -> Int
[ "3 4 2 * 1 5 - 2 3 ^ ^ / +",
precedence (Const _) = 5
"1 2 + 3 4 + ^ 5 6 + ^",
precedence (Exp _ op _)
"1 4 + 5 3 + 2 3 * * *",
| op == "1^" 2 * 3= 4 * *",
| op `elem` ["*", "1/"] 2 += 3 4 + +"
| op `elem` ["+", "-"] = 2
]</lang>
| otherwise = 0</syntaxhighlight>
{{Out}}
<pre>3 + 4 * 2 / (* 1 5 - 52 )3 ^ 2 ^ 3/ +
-->
[3]
[4,3]
[2,4,3]
[4 * 2,3]
[1,4 * 2,3]
[5,1,4 * 2,3]
[1 - 5,4 * 2,3]
[2,1 - 5,4 * 2,3]
[3,2,1 - 5,4 * 2,3]
[2 ^ 3,1 - 5,4 * 2,3]
[( 1 - 5 ) ^ 2 ^ 3,4 * 2,3]
[4 * 2 / ( 1 - 5 ) ^ 2 ^ 3,3]
[3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3]
3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3
 
1 2 + 3 4 + ^ 5 6 + ^
-->
[1]
[2,1]
[1 + 2]
[3,1 + 2]
[4,3,1 + 2]
[3 + 4,1 + 2]
[( 1 + 2 ) ^ ( 3 + 4 )]
[5,( 1 + 2 ) ^ ( 3 + 4 )]
[6,5,( 1 + 2 ) ^ ( 3 + 4 )]
[5 + 6,( 1 + 2 ) ^ ( 3 + 4 )]
[( ( 1 + 2 ) ^ ( 3 + 4 ) ) ^ ( 5 + 6 )]
( ( 1 + 2 ) ^ ( 3 + 4 ) ) ^ ( 5 + 6 )
 
1 4 + 5 3 + 2 3 * * *
-->
[1]
[4,1]
[1 + 4]
[5,1 + 4]
[3,5,1 + 4]
[5 + 3,1 + 4]
[2,5 + 3,1 + 4]
[3,2,5 + 3,1 + 4]
[2 * 3,5 + 3,1 + 4]
[( 5 + 3 ) * 2 * 3,1 + 4]
[( 1 + 4 ) * ( 5 + 3 ) * 2 * 3]
( 1 + 4 ) * ( 5 + 3 ) * 2 * 3
 
1 2 * 3 4 * *
-->
[1]
[2,1]
[1 * 2]
[3,1 * 2]
[4,3,1 * 2]
[3 * 4,1 * 2]
[1 * 2 * 3 * 4]
1 * 2 * 3 * 4
 
1 2 + 3 4 + +
-->
[1]
[2,1]
[1 + 2]
[3,1 + 2]
[4,3,1 + 2]
[3 + 4,1 + 2]
1 + 2 + 3 + 4</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
every rpn := ![
"3 4 2 * 1 5 - 2 3 ^ ^ / +", # reqd
Line 2,032 ⟶ 2,113:
else x) || " "
return s || "]"
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 2,067 ⟶ 2,148:
Implementation:
 
<langsyntaxhighlight lang="j">tokenize=: ' ' <;._1@, deb
 
ops=: ;:'+ - * / ^'
Line 2,102 ⟶ 2,183:
stack=: stack,_;y
smoutput stack
)</langsyntaxhighlight>
 
The stack structure has two elements for each stack entry: expression precedence and expression characters.
Line 2,108 ⟶ 2,189:
Required example:
 
<langsyntaxhighlight lang="j"> parse '3 4 2 * 1 5 - 2 3 ^ ^ / +'
pushing: 3
+-+-+
Line 2,209 ⟶ 2,290:
|2|3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3|
+-+-----------------------------+
3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|7}}
<langsyntaxhighlight lang="java">import java.util.Stack;
 
public class PostfixToInfix {
Line 2,276 ⟶ 2,357:
return expr.peek().ex;
}
}</langsyntaxhighlight>
 
Output:
Line 2,313 ⟶ 2,394:
Needs EcmaScript 6 support (e.g. Chrome).
 
<langsyntaxhighlight lang="javascript">const Associativity = {
/** a / b / c = (a / b) / c */
left: 0,
Line 2,371 ⟶ 2,452:
const realOup = rpnToTree(inp).toString();
console.log(realOup === oup ? "Correct!" : "Incorrect!");
}</langsyntaxhighlight>
 
Output:
Line 2,410 ⟶ 2,491:
Correct!</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
This entry is based on the observation that the Parsing Expression Grammar (PEG)
for the reversed sequence of tokens of an RPN expression is essentially:
<pre>
E = operator operand operand
operand = number / E
operator = '+' | '-' | '*' | '^'
</pre>
 
it being understood that a subsequence [operator, operand1, operand2]
must finally be rendered as "(operand2 operator operand1)" because of
the reversal.
 
This PEG is so simple that only one of the functions from
[[:Category:Jq/peg.jq]], the jq library supporting PEG parsing, is
needed here, namely: `box/1`. It is therefore included in the
following listing, so there is no need to include peg.jq.
 
Notice also that the task requirements imply that the RPN string can
be readily tokenized using `splits/1`, as is done by `tokens/0`.
<syntaxhighlight lang=jq>
### PEG infrastructure
def box(E):
((.result = null) | E) as $e
| .remainder = $e.remainder
| .result += [$e.result] # the magic sauce
;
 
### Tokenize the RPN string.
# Input: a string representing an expression using RPN.
# Output: an array of corresponding tokens.
def tokens:
[splits("[ \n\r\t]+")]
| map(select(. != "")
| . as $in
| try tonumber catch $in);
 
### Parse the reversed array of tokens as produced by `tokens`.
# On entry, .remainder should be the reversed array.
# Output: {remainder, result}
def rrpn:
def found: .result += [.remainder[0]] | (.remainder |= .[1:]);
def nonempty: select(.remainder|length>0);
def check(predicate):
nonempty | select(.remainder[0] | predicate);
 
def recognize(predicate): check(predicate) | found;
 
def number : recognize(type=="number");
def operator: recognize(type=="string");
def operand : number // rrpn;
box(operator | operand | operand);
 
# Input: an array of tokens as produced by `tokens`
# Output: the infix equivalent expressed as a string.
def tokens2infix:
def infix:
if type != "array" then .
elif length == 1 then .[0] | infix
elif length == 3 then "(\(.[2] | infix) \(.[0]) \(.[1] | infix))"
else error
end;
 
{remainder: reverse} | rrpn | .result | infix;
 
# Input: an RPN string
# Output: the equivalent expression as a string using infix notation
def rpn2infix: tokens | tokens2infix;
 
def tests:
"3 4 2 * 1 5 - 2 3 ^ ^ / +",
"1 2 + 3 4 + ^ 5 6 + ^"
;
 
tests | "\"\(.)\" =>", rpn2infix, ""
</syntaxhighlight>
{{output}}
"3 4 2 * 1 5 - 2 3 ^ ^ / +" =>
(3 + ((4 * 2) / ((1 - 5) ^ (2 ^ 3))))
 
"1 2 + 3 4 + ^ 5 6 + ^" =>
(((1 + 2) ^ (3 + 4)) ^ (5 + 6))
</pre>
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
function parseRPNstring(rpns)
infix = []
Line 2,432 ⟶ 2,599:
println("The final infix result: ", parseRPNstring("3 4 2 * 1 5 - 2 3 ^ ^ / +") |> unany, "\n")
println("The final infix result: ", parseRPNstring("1 2 + 3 4 + ^ 5 6 + ^") |> unany)
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 2,453 ⟶ 2,620:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.2.0
 
import java.util.Stack
Line 2,504 ⟶ 2,671:
println("Infix : ${postfixToInfix(e)}\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,541 ⟶ 2,708:
=={{header|Lua}}==
The ouput contains more parenthesis then in strictly nessicary, but otherwise seems to read correctly
<langsyntaxhighlight lang="lua">function tokenize(rpn)
local out = {}
local cnt = 0
Line 2,625 ⟶ 2,792:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>3 + ((4 * 2) / ((1 - 5) ^ (2 ^ 3)))
Line 2,631 ⟶ 2,798:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Rpn_2_Infix {
Rem Form 80,60
Line 2,688 ⟶ 2,855:
}
Rpn_2_Infix
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,770 ⟶ 2,937:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight lang="nim">import tables, strutils
 
const nPrec = 9
Line 2,814 ⟶ 2,981:
 
for test in ["3 4 2 * 1 5 - 2 3 ^ ^ / +", "1 2 + 3 4 + ^ 5 6 + ^"]:
test.parseRPN</langsyntaxhighlight>
Output:
<pre>postfix: 3 4 2 * 1 5 - 2 3 ^ ^ / +
Line 2,903 ⟶ 3,070:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,932 ⟶ 3,099:
) { say } # track progress
say '=>' . substr($_,2,-2)."\n";
}</langsyntaxhighlight>
{{out}}
<pre> ($2^$3)
Line 2,950 ⟶ 3,117:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer show_workings = 1
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #004080;">bool</span> <span style="color: #000000;">show_workings</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
constant operators = {"^","*","/","+","-"},
precedence = { 4, 3, 3, 2, 2 },
<span style="color: #008080;">constant</span> <span style="color: #000000;">operators</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"/"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">},</span>
rassoc = {'r', 0 ,'l', 0 ,'l'}
<span style="color: #000000;">precedence</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span> <span style="color: #0000FF;">},</span>
 
<span style="color: #000000;">rassoc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">'r'</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span> <span style="color: #0000FF;">,</span><span style="color: #008000;">'l'</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span> <span style="color: #0000FF;">,</span><span style="color: #008000;">'l'</span><span style="color: #0000FF;">}</span>
procedure parseRPN(string expr, string expected)
sequence stack = {}
<span style="color: #008080;">procedure</span> <span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">expr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span>
sequence ops = split(expr)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">stack</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
string lhs, rhs
<span style="color: #000000;">ops</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">expr</span><span style="color: #0000FF;">)</span>
integer lprec,rprec
<span style="color: #004080;">string</span> <span style="color: #000000;">lhs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rhs</span>
printf(1,"Postfix input: %-30s%s", {expr,iff(show_workings?'\n':'\t')})
<span style="color: #004080;">integer</span> <span style="color: #000000;">lprec</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rprec</span>
if length(ops)=0 then ?"error" return end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Postfix input: %-32s%s"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">expr</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">show_workings</span><span style="color: #0000FF;">?</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">:</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">)})</span>
for i=1 to length(ops) do
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ops</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"error"</span> <span style="color: #008080;">return</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
string op = ops[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ops</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer k = find(op,operators)
<span style="color: #004080;">string</span> <span style="color: #000000;">op</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ops</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
if k=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">op</span><span style="color: #0000FF;">,</span><span style="color: #000000;">operators</span><span style="color: #0000FF;">)</span>
stack = append(stack,{9,op})
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">stack</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">stack</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">op</span><span style="color: #0000FF;">})</span>
if length(stack)<2 then ?"error" return end if
<span style="color: #008080;">else</span>
{rprec,rhs} = stack[$]; stack = stack[1..$-1]
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">stack</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"error"</span> <span style="color: #008080;">return</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
{lprec,lhs} = stack[$]
<span style="color: #0000FF;">{</span><span style="color: #000000;">rprec</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rhs</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">stack</span><span style="color: #0000FF;">[$];</span> <span style="color: #000000;">stack</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">stack</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
integer prec = precedence[k]
<span style="color: #0000FF;">{</span><span style="color: #000000;">lprec</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lhs</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">stack</span><span style="color: #0000FF;">[$]</span>
integer assoc = rassoc[k]
<span style="color: #004080;">integer</span> <span style="color: #000000;">prec</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">precedence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
if lprec<prec or (lprec=prec and assoc='r') then
<span style="color: #004080;">integer</span> <span style="color: #000000;">assoc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rassoc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
lhs = "("&lhs&")"
<span style="color: #008080;">if</span> <span style="color: #000000;">lprec</span><span style="color: #0000FF;"><</span><span style="color: #000000;">prec</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">lprec</span><span style="color: #0000FF;">=</span><span style="color: #000000;">prec</span> <span style="color: #008080;">and</span> <span style="color: #000000;">assoc</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'r'</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">lhs</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"("</span><span style="color: #0000FF;">&</span><span style="color: #000000;">lhs</span><span style="color: #0000FF;">&</span><span style="color: #008000;">")"</span>
if rprec<prec or (rprec=prec and assoc='l') then
<span style="color: #008080;">end</span> rhs<span style= "("&rhs&")color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rprec</span><span style="color: #0000FF;"><</span><span style="color: #000000;">prec</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">rprec</span><span style="color: #0000FF;">=</span><span style="color: #000000;">prec</span> <span style="color: #008080;">and</span> <span style="color: #000000;">assoc</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'l'</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">rhs</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"("</span><span style="color: #0000FF;">&</span><span style="color: #000000;">rhs</span><span style="color: #0000FF;">&</span><span style="color: #008000;">")"</span>
stack[$] = {prec,lhs&" "&op&" "&rhs}
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #000000;">stack</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">prec</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lhs</span><span style="color: #0000FF;">&</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">op</span><span style="color: #0000FF;">&</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">rhs</span><span style="color: #0000FF;">}</span>
if show_workings then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
?{op,stack}
<span style="color: #008080;">if</span> <span style="color: #000000;">show_workings</span> <span style="color: #008080;">then</span>
end if
<span style="color: #0000FF;">?{</span><span style="color: #000000;">op</span><span style="color: #0000FF;">,</span><span style="color: #000000;">stack</span><span style="color: #0000FF;">}</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
string res = stack[1][2]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"Infix result: %s [%s]\n", {res,iff(res=expected?"ok","**ERROR**")})
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">stack</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
end procedure
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Infix result: %s [%s]\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"ok"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"**ERROR**"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
parseRPN("3 4 2 * 1 5 - 2 3 ^ ^ / +","3 + 4 * 2 / (1 - 5) ^ 2 ^ 3")
show_workings = 0
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3 4 2 * 1 5 - 2 3 ^ ^ / +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3 + 4 * 2 / (1 - 5) ^ 2 ^ 3"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 + 3 4 + ^ 5 6 + ^","((1 + 2) ^ (3 + 4)) ^ (5 + 6)")
<span style="color: #000000;">show_workings</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
parseRPN("1 2 + 3 4 + 5 6 + ^ ^","(1 + 2) ^ (3 + 4) ^ (5 + 6)")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 + 3 4 + ^ 5 6 + ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"((1 + 2) ^ (3 + 4)) ^ (5 + 6)"</span><span style="color: #0000FF;">)</span>
parseRPN("moon stars mud + * fire soup * ^","(moon * (stars + mud)) ^ (fire * soup)")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 + 3 4 + 5 6 + ^ ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(1 + 2) ^ (3 + 4) ^ (5 + 6)"</span><span style="color: #0000FF;">)</span>
parseRPN("3 4 ^ 2 9 ^ ^ 2 5 ^ ^","((3 ^ 4) ^ 2 ^ 9) ^ 2 ^ 5")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"moon stars mud + * fire soup * ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(moon * (stars + mud)) ^ (fire * soup)"</span><span style="color: #0000FF;">)</span>
parseRPN("5 6 * * + +","error")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3 4 ^ 2 9 ^ ^ 2 5 ^ ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"((3 ^ 4) ^ 2 ^ 9) ^ 2 ^ 5"</span><span style="color: #0000FF;">)</span>
parseRPN("","error")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"5 6 * * + +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"error"</span><span style="color: #0000FF;">)</span>
parseRPN("1 4 + 5 3 + 2 3 * * *","(1 + 4) * (5 + 3) * 2 * 3")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"error"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 * 3 4 * *","1 * 2 * 3 * 4")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 4 + 5 3 + 2 3 * * *"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(1 + 4) * (5 + 3) * 2 * 3"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 + 3 4 + +","1 + 2 + 3 + 4")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 * 3 4 * *"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1 * 2 * 3 * 4"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 + 3 4 + ^","(1 + 2) ^ (3 + 4)")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 + 3 4 + +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1 + 2 + 3 + 4"</span><span style="color: #0000FF;">)</span>
parseRPN("5 6 ^ 7 ^","(5 ^ 6) ^ 7")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 + 3 4 + ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(1 + 2) ^ (3 + 4)"</span><span style="color: #0000FF;">)</span>
parseRPN("5 4 3 2 ^ ^ ^","5 ^ 4 ^ 3 ^ 2")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"5 6 ^ 7 ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(5 ^ 6) ^ 7"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 3 + +","1 + 2 + 3")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"5 4 3 2 ^ ^ ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"5 ^ 4 ^ 3 ^ 2"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 + 3 +","1 + 2 + 3")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 3 + +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1 + 2 + 3"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 3 ^ ^","1 ^ 2 ^ 3")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 + 3 +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1 + 2 + 3"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 ^ 3 ^","(1 ^ 2) ^ 3")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 3 ^ ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1 ^ 2 ^ 3"</span><span style="color: #0000FF;">)</span>
parseRPN("1 1 - 3 +","1 - 1 + 3")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 ^ 3 ^"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(1 ^ 2) ^ 3"</span><span style="color: #0000FF;">)</span>
parseRPN("3 1 1 - +","3 + 1 - 1") -- [txr says 3 + (1 - 1)]
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 1 - 3 +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1 - 1 + 3"</span><span style="color: #0000FF;">)</span>
parseRPN("1 2 3 + -","1 - (2 + 3)")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3 1 1 - +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3 + 1 - 1"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [txr says 3 + (1 - 1)]</span>
parseRPN("4 3 2 + +","4 + 3 + 2")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1 2 3 + -"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"1 - (2 + 3)"</span><span style="color: #0000FF;">)</span>
parseRPN("5 4 3 2 + + +","5 + 4 + 3 + 2")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"4 3 2 + +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"4 + 3 + 2"</span><span style="color: #0000FF;">)</span>
parseRPN("5 4 3 2 * * *","5 * 4 * 3 * 2")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"5 4 3 2 + + +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"5 + 4 + 3 + 2"</span><span style="color: #0000FF;">)</span>
parseRPN("5 4 3 2 + - +","5 + 4 - (3 + 2)") -- [python says 5 + (4 - (3 + 2))]
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"5 4 3 2 * * *"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"5 * 4 * 3 * 2"</span><span style="color: #0000FF;">)</span>
parseRPN("3 4 5 * -","3 - 4 * 5")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"5 4 3 2 + - +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"5 + 4 - (3 + 2)"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [python says 5 + (4 - (3 + 2))]</span>
parseRPN("3 4 5 - *","3 * (4 - 5)") -- [python says (3 - 4) * 5] [!!flagged!!]
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3 4 5 * -"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3 - 4 * 5"</span><span style="color: #0000FF;">)</span>
parseRPN("3 4 - 5 *","(3 - 4) * 5")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3 4 5 - *"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3 * (4 - 5)"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [python says (3 - 4) * 5] [!!flagged!!]</span>
parseRPN("4 2 * 1 5 - +","4 * 2 + 1 - 5") -- [python says 4 * 2 + (1 - 5)]
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3 4 - 5 *"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(3 - 4) * 5"</span><span style="color: #0000FF;">)</span>
parseRPN("4 2 * 1 5 - 2 ^ /","4 * 2 / (1 - 5) ^ 2")
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"4 2 * 1 5 - +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"4 * 2 + 1 - 5"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [python says 4 * 2 + (1 - 5)]</span>
parseRPN("3 4 2 * 1 5 - 2 3 ^ ^ / +","3 + 4 * 2 / (1 - 5) ^ 2 ^ 3")</lang>
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"4 2 * 1 5 - 2 ^ /"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"4 * 2 / (1 - 5) ^ 2"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">parseRPN</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3 4 2 * 1 5 - 2 3 ^ ^ / +"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3 + 4 * 2 / (1 - 5) ^ 2 ^ 3"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,071 ⟶ 3,241:
=={{header|PicoLisp}}==
We maintain a stack of cons pairs, consisting of precedences and partial expressions. Numbers get a "highest" precedence of '9'.
<langsyntaxhighlight PicoLisplang="picolisp">(de leftAssoc (Op)
(member Op '("*" "/" "+" "-")) )
 
Line 3,103 ⟶ 3,273:
(println Stack) )
(prog1 (cdr (pop 'Stack))
(and Stack (quit "Garbage remained on stack")) ) ) )</langsyntaxhighlight>
Test (note that the top-of-stack is in the left-most position):
<langsyntaxhighlight PicoLisplang="picolisp">: (rpnToInfix "3 4 2 * 1 5 - 2 3 \^ \^ / +")
Token Stack
3 ((9 . 3))
Line 3,135 ⟶ 3,305:
+ ((2 . "5 + 6") (4 . "(1 + 2) \^ (3 + 4)"))
^ ((4 . "((1 + 2) \^ (3 + 4)) \^ (5 + 6)"))
-> "((1 + 2) \^ (3 + 4)) \^ (5 + 6)"</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* Uses a push-down pop-up stack for the stack (instead of array) */
cvt: procedure options (main); /* 10 Sept. 2012 */
Line 3,193 ⟶ 3,363:
 
end cvt;
</syntaxhighlight>
</lang>
Outputs:
<pre>
Line 3,238 ⟶ 3,408:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
"""
Line 3,357 ⟶ 3,527:
print ("Input: ",strTest)
print ("Output:",strResult)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,369 ⟶ 3,539:
=={{header|Racket}}==
{{trans|AWK}}
<langsyntaxhighlight lang="racket">
#lang racket
(require racket/dict)
Line 3,402 ⟶ 3,572:
(if (eq? +inf.0 p) (printf "[~a] " s) (printf "[~a {~a}] " s p)))
(newline))
</syntaxhighlight>
</lang>
 
Output:
Line 3,447 ⟶ 3,617:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub p ($pair, $prec) { $pair.key < $prec ?? "( {$pair.value} )" !! $pair.value }
 
sub rpm-to-infix($string) {
Line 3,463 ⟶ 3,633:
say rpm-to-infix $_ for
'3 4 2 * 1 5 - 2 3 ^ ^ / +',
'1 2 + 3 4 + ^ 5 6 + ^';</langsyntaxhighlight>
{{out}}
<pre>3 4 2 * 1 5 - 2 3 ^ ^ / +
Line 3,479 ⟶ 3,649:
 
The same reasoning was used for the ''operator associations'' &nbsp; (the left &nbsp; ◄ &nbsp; and right &nbsp; ► &nbsp; arrow symbols).
<langsyntaxhighlight lang="rexx">/*REXX program converts Reverse Polish Notation (RPN) ───► an infix notation. */
showAction = 1 /* 0 if no showActions wanted. */
# = 0 /*initialize stack pointer to 0 (zero).*/
Line 3,516 ⟶ 3,686:
end /*N*/
 
return space( substr( pop(), 2) )</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs: &nbsp; &nbsp; &nbsp; [Output is very similar to AWK's output.]}}
<pre>
Line 3,561 ⟶ 3,731:
^ ──► {4 ( Mond * ( Sterne + Schlamm)) ^ ( Feur * Suppe)}
infix: ( Mond * ( Sterne + Schlamm)) ^ ( Feur * Suppe)
</pre>
 
=={{header|RPL}}==
It adds more parentheses than required, thus avoiding any ambiguity.
« '''IF''' DUP " " POS '''THEN''' " )" + "( " SWAP + '''END'''
» '<span style="color:blue">ADDPAR</span>' STO
« "}" + "{" SWAP + STR→ <span style="color:grey">@ tokenize</span>
→ postfix
« 1 postfix SIZE '''FOR''' j
postfix j GET →STR
'''IF''' "+-*/^" OVER POS '''THEN'''
ROT <span style="color:blue">ADDPAR</span> " " + SWAP + " " +
SWAP <span style="color:blue">ADDPAR</span> +
'''END'''
'''NEXT'''
» » '<span style="color:blue">→INFIX</span>' STO
 
"3 4 2 * 1 5 - 2 3 ^ ^ / +" <span style="color:blue">→INFIX</span>
"1 2 + 3 4 + ^ 5 6 + ^" <span style="color:blue">→INFIX</span>
{{out}}
<pre>
2: "3 + ( ( 4 * 2 ) / ( ( 1 - 5 ) ^ ( 2 ^ 3 ) ) )"
1: "( ( 1 + 2 ) ^ ( 3 + 4 ) ) ^ ( 5 + 6 )"
</pre>
 
Line 3,567 ⟶ 3,761:
See [[Parsing/RPN/Ruby]]
 
<langsyntaxhighlight lang="ruby">rpn = RPNExpression.new("3 4 2 * 1 5 - 2 3 ^ ^ / +")
infix = rpn.to_infix
ruby = rpn.to_ruby</langsyntaxhighlight>
outputs
<pre>for RPN expression: 3 4 2 * 1 5 - 2 3 ^ ^ / +
Line 3,591 ⟶ 3,785:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func p(pair, prec) {
pair[0] < prec ? "( #{pair[1]} )" : pair[1]
}
Line 3,622 ⟶ 3,816:
]
 
tests.each { say rpm_to_infix(_).join(' ') }</langsyntaxhighlight>
{{out}}
<pre>
Line 3,647 ⟶ 3,841:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
# Helpers
Line 3,685 ⟶ 3,879:
 
puts [rpn2infix {3 4 2 * 1 5 - 2 3 ^ ^ / +}]
puts [rpn2infix {1 2 + 3 4 + ^ 5 6 + ^}]</langsyntaxhighlight>
Output:
<pre>
Line 3,724 ⟶ 3,918:
The <code>lisp-to-infix</code> filter then takes advantage of this non-associativity in minimizing the parentheses.
 
<langsyntaxhighlight lang="txrlisp">;; alias for circumflex, which is reserved syntax
(defvar exp (intern "^"))
 
Line 3,786 ⟶ 3,980:
((a b . c) "*excess args*")
((a) (lisp-to-infix (rpn-to-lisp (string-to-rpn a))))
(else "*arg needed*"))))</langsyntaxhighlight>
 
{{out}}
Line 3,878 ⟶ 4,072:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Option Strict On
 
Imports System.Text.RegularExpressions
Line 3,998 ⟶ 4,192:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Postfix : 3 4 2 * 1 5 - 2 3 ^ ^ / +
Line 4,034 ⟶ 4,228:
{{libheader|Wren-seq}}
{{libheader|Wren-pattern}}
<langsyntaxhighlight ecmascriptlang="wren">import "./seq" for Stack
import "./pattern" for Pattern
 
class Expression {
Line 4,087 ⟶ 4,281:
System.print("Postfix : %(e)")
System.print("Infix : %(postfixToInfix.call(e))\n")
}</langsyntaxhighlight>
 
{{out}}
Line 4,124 ⟶ 4,318:
=={{header|zkl}}==
{{trans|Go}}
<langsyntaxhighlight lang="zkl">tests:=T("3 4 2 * 1 5 - 2 3 ^ ^ / +","1 2 + 3 4 + ^ 5 6 + ^");
var opa=D(
Line 4,160 ⟶ 4,354:
}
println("infix:", stack[0][1])
}</langsyntaxhighlight>
{{out}}
<pre>
1,150

edits