Jump to content

Parsing/RPN to infix conversion: Difference between revisions

no edit summary
(→‎{{header|Perl}}: same logic, more readable)
No edit summary
Line 2,359:
Infix : ((1 + 2) ^ (3 + 4)) ^ (5 + 6)
</pre>
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Module Rpn_2_Infix {
Rem Form 80,60
function rpn_to_infix$(a$) {
def m=0
inventory precendence="-":=2,"+":=2,"*":=3,"/":=3,"^":=4
dim token$()
token$()=piece$(a$," ")
l=len(token$())
dim type(l)=0, right(l)=0, infix$(l)
infix=-1
for i=0 to l-1
c=val(token$(i),"",m)
if m=-1 then
type(i)=precendence(token$(i))
if type(i)=4 then right(i)=-1
end if
if type(i)=0 then
infix++
infix$(infix)=token$(i)
type(infix)=100
else
if right(i) then
if type(infix)<type(i) then infix$(infix)="("+infix$(infix)+")"
if type(infix-1)<100 then infix$(infix-1)="("+infix$(infix-1)+")"
infix$(infix-1)=infix$(infix-1)+token$(i)+infix$(infix)
else
if type(infix)<type(i) then infix$(infix)="("+infix$(infix)+")"
if type(infix-1)<type(i) then
infix$(infix-1)="("+infix$(infix-1)+")"+token$(i)+infix$(infix)
else
infix$(infix-1)=infix$(infix-1)+token$(i)+infix$(infix)
end if
end if
type(infix-1)=type(i)
infix--
end if
inf=each(infix$(),1, infix+1)
while inf
export$<=token$(i)+" ["+str$(inf^,"")+"] "+ array$(inf)+{
}
token$(i)=" "
end while
next i
=infix$(0)
}
Global export$
document export$
example1=rpn_to_infix$("3 4 2 * 1 5 - 2 3 ^ ^ / +")="3+4*2/(1-5)^2^3"
example2=rpn_to_infix$("1 2 + 3 4 + ^ 5 6 + ^")="((1+2)^(3+4))^(5+6)"
Print example1, example2
Rem Print #-2, Export$
ClipBoard Export$
}
Rpn_2_Infix
</lang>
 
{{out}}
<pre style="height:30ex;overflow:scroll">
3 [0] 3
4 [0] 3
[1] 4
2 [0] 3
[1] 4
[2] 2
* [0] 3
[1] 4*2
1 [0] 3
[1] 4*2
[2] 1
5 [0] 3
[1] 4*2
[2] 1
[3] 5
- [0] 3
[1] 4*2
[2] 1-5
2 [0] 3
[1] 4*2
[2] 1-5
[3] 2
3 [0] 3
[1] 4*2
[2] 1-5
[3] 2
[4] 3
^ [0] 3
[1] 4*2
[2] 1-5
[3] 2^3
^ [0] 3
[1] 4*2
[2] (1-5)^2^3
/ [0] 3
[1] 4*2/(1-5)^2^3
+ [0] 3+4*2/(1-5)^2^3
1 [0] 1
2 [0] 1
[1] 2
+ [0] 1+2
3 [0] 1+2
[1] 3
4 [0] 1+2
[1] 3
[2] 4
+ [0] 1+2
[1] 3+4
^ [0] (1+2)^(3+4)
5 [0] (1+2)^(3+4)
[1] 5
6 [0] (1+2)^(3+4)
[1] 5
[2] 6
+ [0] (1+2)^(3+4)
[1] 5+6
^ [0] ((1+2)^(3+4))^(5+6)
</pre >
 
 
 
=={{header|Nim}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.