Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^6: Generic RPN Translator available?

by saintmike (Vicar)
on Jan 22, 2005 at 04:37 UTC ( [id://424183]=note: print w/replies, xml ) Need Help??


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

Hmm, looks like precedence of * and / over + and - isn't handled correctly:
a+b*c/2
should yield
a,b,c,*,2,/,+
but results in
a,b,+,c,*,2,/
instead. I guess we really need grammar, especially if you into consideration that an operator like '**' or '^' needs even higher precedence.

Replies are listed 'Best First'.
Re^7: Generic RPN Translator available?
by BrowserUk (Patriarch) on Jan 22, 2005 at 04:55 UTC

    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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://424183]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found