Come for the quick hacks, stay for the epiphanies. PerlMonks

### Re^7: Generic RPN Translator available?

by BrowserUk (Patriarch)
 on Jan 22, 2005 at 04:55 UTC Need Help??

in reply to Re^6: Generic RPN Translator available?
in thread Generic RPN Translator available?

Update: It took all of about 25 minutes to implement 4 levels of presedence:

** | */% | +- | ,func()

```P:\test>423305
1+2*3
2, 3, *, 1, +
((2*3)+1)

1+2*3**4
3, 4, **, 2, *, 1, +
(((3**4)*2)+1)

It's not complete, but I thought you were looking for a starting point--not finished code.

Have fun!

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Replies are listed 'Best First'.
Re^8: Generic RPN Translator available?
by BrowserUk (Patriarch) on Jan 23, 2005 at 18:34 UTC

Here's a somewhat cleaned up and somewhat better tested version that handles infinite nesting, four levels of presecence etc.

It also has a rpn2exp() routine that reverses the process that adds full parenthesis to show the effect of the precedence.

Tests

```P:\test>423305
Func_1(1,Func_2(Func3(1+2*3)*aFunc(3))+FuNc(4,5,6),-2,e,-10,2e10)+1
1, 1, 2, 3, *, +, 1, Func3(), 3, 1, aFunc(), *, 1, Func_2(), 4, 5, 6,
+3, FuNc(), +, -2, e, -10, 2e10, 6, Func_1(), 1, +
( Func_1( 1, ( Func_2( ( Func3( ( 1 + ( 2 * 3 ) ) ) * aFunc( 3 ) ) ) +
+ FuNc( 4, 5, 6 ) ), -2, e, -10, 2e10 ) + 1 )

func(1,2+3*4/5**6,7/8)
1, 2, 3, 4, *, 5, 6, **, /, +, 7, 8, /, 3, func()
func( 1, ( 2 + ( ( 3 * 4 ) / ( 5 ** 6 ) ) ), ( 7 / 8 ) )

a+b*c/2
a, b, c, *, 2, /, +
( a + ( ( b * c ) / 2 ) )

abc+def+Efg_hij
abc, def, +, Efg_hij, +
( ( abc + def ) + Efg_hij )

2.0+1e-2/0.01E-21
2.0, 1e-2, 0.01E-21, /, +
( 2.0 + ( 1e-2 / 0.01E-21 ) )

max(a,b,c,d)*atan(pi*4,-1)
a, b, c, d, 4, max(), pi, 4, *, -1, 2, atan(), *
( max( a, b, c, d ) * atan( ( pi * 4 ), -1 ) )

sin(cos(x)-tan(y))+f(g(z))
x, 1, cos(), y, 1, tan(), -, 1, sin(), z, 1, g(), 1, f(), +
( sin( ( cos( x ) - tan( y ) ) ) + f( g( z ) ) )

2*(somevar+other)+max(this,that)
2, somevar, other, +, *, this, that, 2, max(), +
( ( 2 * ( somevar + other ) ) + max( this, that ) )

A+(B*C-D)/E
A, B, C, *, D, -, E, /, +
( A + ( ( ( B * C ) - D ) / E ) )

(a*(b)-c**(3.4e-2))
a, b, *, c, 3.4e-2, **, -
( ( a * b ) - ( c ** 3.4e-2 ) )

5**((-2e-3+x)*sin(p+4.0)/fred)
5, -2e-3, x, +, p, 4.0, +, 1, sin(), *, fred, /, **
( 5 ** ( ( ( -2e-3 + x ) * sin( ( p + 4.0 ) ) ) / fred ) )

sin(a)+sin(ab)+sin(a,b)
a, 1, sin(), ab, 1, sin(), +, a, b, 2, sin(), +
( ( sin( a ) + sin( ab ) ) + sin( a, b ) )

Func_2(Func3(1*2*3)*aFunc(3))+FuNc(4,5,6)
1, 2, *, 3, *, 1, Func3(), 3, 1, aFunc(), *, 1, Func_2(), 4, 5, 6, 3,
+FuNc(), +
( Func_2( ( Func3( ( ( 1 * 2 ) * 3 ) ) * aFunc( 3 ) ) ) + FuNc( 4, 5,
+6 ) )

func(1,2+3*4/5**6,7/8)
1, 2, 3, 4, *, 5, 6, **, /, +, 7, 8, /, 3, func()
func( 1, ( 2 + ( ( 3 * 4 ) / ( 5 ** 6 ) ) ), ( 7 / 8 ) )

Func_1(1,xx,-2,e,-10,-2e-10)+1
1, xx, -2, e, -10, -2e-10, 6, Func_1(), 1, +
( Func_1( 1, xx, -2, e, -10, -2e-10 ) + 1 )

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.
We're getting there :). However, nesteOk() doesn't seem to catch this case:

"sin((a)"

and exp2rpn then loops infinitely.

Strange?

```sin((a)
Unbalanced parens: 'sin((a)
' at P:\test\423305.pl line 79, <STDIN> line 1.
sin((a)
1
1

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://424186]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-06-13 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.