LB Booster
Programming >> BASIC code examples >> precedence climbing parser - evaluator
http://lbb.conforums.com/index.cgi?board=code&action=display&num=1500109758

precedence climbing parser - evaluator
Post by aurel on Jul 15th, 2017, 09:09am

Hi
In first place to mr.Russel

Do you maybe have any example LBB or in BB4W
about this thematic?
thnks in advance


Aurel wink
Re: precedence climbing parser - evaluator
Post by Richard Russell on Jul 15th, 2017, 3:44pm

on Jul 15th, 2017, 09:09am, aurel wrote:
Do you maybe have any example LBB or in BB4W about this thematic?

I have some BBC BASIC code which may qualify as a "precedence climbing parser". As such it's not really appropriate to discuss it on this LBB support forum but, since you asked, I've listed it below. If you want to follow this up can I ask that you do so at a BBC BASIC forum.

BBC BASIC Code:
      REM. Test of expression evaluator

      *KEY 1 2^3*2^3+4|M
      *KEY 2 1-2-3-4-5-6|M
      *KEY 3 1/2/3/4/5/6|M
      *KEY 4 1+2-3*4/5^6/7*8-9+10|M
      *KEY 5 1^2/3*4-5+6-7*8/9^10|M
      *KEY 6 2^3*2+4|M
      *KEY 7 1+2^3*4|M
      *KEY 8 3.14159|M
      *KEY 9 -5^2|M

      COLOUR 5
      PRINT '"Use function keys for preset expressions"'
      COLOUR 0

      REPEAT
        INPUT "Enter a numeric expression (+,-,*,/,^ operators only): "E$
        ok% = TRUE
        ON ERROR LOCAL PRINT 'REPORT$ : ok% = FALSE
        IF ok% THEN
          result1 = EVAL(E$)
          PRINT "BASIC's evaluator gives: ";result1
          op% = 0 : pr% = 1
          result2 = FNeval(E$, op%, pr%, 99)
          PRINT "Test evaluator gives: ";result2
          IF result1 <> result2 COLOUR 1 : PRINT "ERROR!!!!" : COLOUR 0 : VDU 7
        ENDIF
        RESTORE ERROR
      UNTIL FALSE

      DEF FNeval(RETURN E$, RETURN O%, RETURN o%, ecxedx)
      LOCAL p%(), P%, p%, eaxebx

      DIM p%(5)
      p%() = 0,1,1,2,2,3

      REPEAT
        P% = O%
        p% = o%
        eaxebx = ecxedx
        ecxedx = VAL(E$)
        REPEAT
          E$ = MID$(E$,2)
          O% = INSTR("+-*/^", LEFT$(E$,1))
        UNTIL O%<>0 OR E$=""
        E$ = MID$(E$,2) : REM Skip past operator
        o% = p%(O%)
  
        WHILE o% > p% ecxedx = FNeval(E$, O%, o%, ecxedx) : ENDWHILE
  
        SWAP eaxebx,ecxedx
        CASE P% OF
          WHEN 0: ecxedx = eaxebx
          WHEN 1: ecxedx += eaxebx
          WHEN 2: ecxedx -= eaxebx
          WHEN 3: ecxedx *= eaxebx
          WHEN 4: ecxedx /= eaxebx
          WHEN 5: ecxedx = ecxedx^eaxebx
        ENDCASE
  
      UNTIL o% < p%
      = ecxedx 

Richard.
Re: precedence climbing parser - evaluator
Post by aurel on Jul 16th, 2017, 2:42pm

Thanks Richard...
i think that i have account on BBC forum but i am not
sure that ih have BB4W installed...
Ok i will try your example..

Is even possible to translate this to JB/LBB ?
Re: precedence climbing parser - evaluator
Post by Richard Russell on Jul 16th, 2017, 3:06pm

on Jul 16th, 2017, 2:42pm, aurel wrote:
Is even possible to translate this to JB/LBB ?

Here's a direct translation to LBB. If you delete the !*KEY commands it will run in LB 4 too (not JB because it doesn't have EVAL, although that's only used to check the result for correctness):

Code:
      ' Test of expression evaluator

      !*KEY 1 2^3*2^3+4|M
      !*KEY 2 1-2-3-4-5-6|M
      !*KEY 3 1/2/3/4/5/6|M
      !*KEY 4 1+2-3*4/5^6/7*8-9+10|M
      !*KEY 5 1^2/3*4-5+6-7*8/9^10|M
      !*KEY 6 2^3*2+4|M
      !*KEY 7 1+2^3*4|M
      !*KEY 8 3.14159|M
      !*KEY 9 -5^2|M

      print "Use function keys for preset expressions"
      print

      do
        input "Enter a numeric expression (+,-,*,/,^ operators only): "; E$
        result1 = eval(E$)
        print "BASIC's evaluator gives: ";result1
        op = 0 : pr = 1
        result2 = evaluate(E$, op, pr, 99)
        print "Test evaluator gives: ";result2
        if result1 <> result2 then print "ERROR!!!!"
      loop until 0

function evaluate(byref E$, byref O, byref o, ecxedx)
      dim p(5)
      p(1) = 1 : p(2) = 1 : p(3) = 2 : p(4) = 2 : p(5) = 3

      do
        P = O
        p = o
        eaxebx = ecxedx
        ecxedx = val(E$)
        do
          E$ = mid$(E$,2)
          O = instr("+-*/^", left$(E$,1))
        loop until O<>0 or E$=""
        E$ = mid$(E$,2) ' Skip past operator
        o = p(O)

        while o > p : ecxedx = evaluate(E$, O, o, ecxedx) : wend

        temp = eaxebx : eaxebx = ecxedx : ecxedx = temp
        select case P
          case 0: ecxedx = eaxebx
          case 1: ecxedx = ecxedx + eaxebx
          case 2: ecxedx = ecxedx - eaxebx
          case 3: ecxedx = ecxedx * eaxebx
          case 4: ecxedx = ecxedx / eaxebx
          case 5: ecxedx = ecxedx^eaxebx
        end select

      loop until o < p
      evaluate = ecxedx
end function 

Richard.
Re: precedence climbing parser - evaluator
Post by aurel on Jul 16th, 2017, 10:18pm

thank you
Richard i just commented lines with EVAL because i use
ončy LBB which i think is very good smiley
and work without problems for me .

I hope that i dont bothering you with this questions?

I see that in evaluator we cannot use parens ()
is it problematic to add () in program ?

thanks
Re: precedence climbing parser - evaluator
Post by Richard Russell on Jul 17th, 2017, 08:37am

on Jul 16th, 2017, 10:18pm, aurel wrote:
I see that in evaluator we cannot use parens ()
is it problematic to add () in program ?

I would not expect it to be "problematic" but I've not tried it myself. You can treat a parenthesised expression as a 'number' so I would expect that the correct place to handle it is where the program has the VAL function.

Richard.

Re: precedence climbing parser - evaluator
Post by Richard Russell on Jul 17th, 2017, 4:15pm

on Jul 17th, 2017, 08:37am, Richard Russell wrote:
I would not expect it to be "problematic" but I've not tried it myself.

For a version which supports parentheses see the Just BASIC forum.

Richard.
Re: precedence climbing parser - evaluator
Post by aurel on Jul 18th, 2017, 10:09pm

Hi Richard
It looks that someone remove linked topic
well nothing new those people don't like you
because you create better PL than Carl.
not worry i will try...
Re: precedence climbing parser - evaluator
Post by RNBW on Jul 20th, 2017, 08:23am

REPLY #6
Richard
Could you kindly reproduce the code to which your link was directed in JB because the whole topic has been deleted on JB and my requests on the JB forum have also been deleted. I can't remember if you produced the code or just referred to it (I only had the opportunity to glance at it when it was posted so I wasn't sure). If it's not your code then I understand that you won't be able to produce it.
Re: precedence climbing parser - evaluator
Post by Richard Russell on Jul 20th, 2017, 10:48am

on Jul 20th, 2017, 08:23am, RNBW wrote:
Could you kindly reproduce the code to which your link was directed in JB because the whole topic has been deleted

I don't think I kept a copy of the 'parentheses' version; I posted it to the JB forum thinking it would be a secure repository for it. I do know that it took me longer than I expected to get that version working so I'd be pretty upset if it was now 'lost'.

Why was the topic deleted? I realise that Just BASIC doesn't have EVAL, but surely it's legitimate to work around that by writing an equivalent user-defined function. Such a substitute is never going to be as fast as, and probably not as functional as, the native LB/LBB function (and indeed even LBB's version isn't fully compatible with LB4) so I can't see how it can be argued that it's a 'forbidden' means of working around JB's limitations.

As for Aurel's comment that "those people don't like you" I don't think that can be an issue on this occasion because it would surely only have been my posts that were deleted, not the entire thread (which wasn't started by me).

Richard.
Re: precedence climbing parser - evaluator
Post by RNBW on Jul 20th, 2017, 1:07pm

Richard
The strange thing is that it's not only the original total link that has been deleted, my subsequent posts have also been deleted (and very quickly). Someone doesn't want to see any reference to it for some reason.
I wonder if it's because they are talking of a Just Basic 2 and that this new version still doesn't include EVAL. That would be strange but no stranger than what has happened. In my posts, I requested a reason for the deletions and I didn't even receive the courtesy of a response, just deletion of my posts.
I must say I find LB and JB's aversion to LBB very strange. All the other "BASIC" forums seem to talk freely about other versions. Some even include sections where one can freely discuss other programming languages.

Re: precedence climbing parser - evaluator
Post by Richard Russell on Jul 20th, 2017, 2:30pm

on Jul 20th, 2017, 1:07pm, RNBW wrote:
I wonder if it's because they are talking of a Just Basic 2 and that this new version still doesn't include EVAL.

It seems ridiculous to me if users are not allowed to devise workarounds for limitations of the language. I've seen some cunning uses of the JB RUN statement which can go some way to compensate for the absence of CALLDLL, yet it's never been suggested that these are 'illegal'. 'Clever' programming should be encouraged rather than penalised!

Quote:
In my posts, I requested a reason for the deletions and I didn't even receive the courtesy of a response, just deletion of my posts.

That is disgraceful. You could try appealing to one of the other admins: Rod Bird (who posts here) is listed as an admin of the Just BASIC forum so he might be able to shed some light on what is going on. There has been some pretty inexcusable behaviour at the Liberty BASIC Community Forum in the past but the Just BASIC forum has (at least until now) seemed to be run quite responsibly.

Quote:
I must say I find LB and JB's aversion to LBB very strange. All the other "BASIC" forums seem to talk freely about other versions. Some even include sections where one can freely discuss other programming languages.

The rules of the Just BASIC forum specifically allow discussion of other languages so long as it is not "in depth" and is not "promoting" them. They state: "Other languages may be mentioned when discussing code to be converted or tools that can be adapted" (Rule 2, just in case they are retrospectively changed, as happened at the LB forum).

But that's not an issue in this case. The substitute EVAL function had no specific connection with LBB or any other BASIC dialect, it was perfectly ordinary Just BASIC code. There were a couple of references to LBB in the thread because, coincidentally, expression evaluation was being discussed here but that should have been covered under the "discussing code to be converted or tools that can be adapted" clause - and anyway those individual posts could have been removed if anybody felt that strongly.

Richard.

Re: precedence climbing parser - evaluator
Post by aurel on Jul 20th, 2017, 9:25pm

Hi guys
interesting discussion but what a worth of all this.
honestly i think that all those people arounded
around CG are servant or are very limited mind
probably they think that whole basic world start with
JB/LB but that times are gone.