Author Topic: The Recursive Descent Parser  (Read 12797 times)

0 Members and 1 Guest are viewing this topic.

Offline eoredson

  • Newbie
  • Posts: 55
  • Let the Farce be with you!
    • View Profile
    • Oredson QB45 Files At Filegate
The Recursive Descent Parser
« on: July 23, 2017, 10:45:01 pm »
Find attached What64 the recursive descent parser.

Erik.
* WHAT64.ZIP (Filesize: 17.47 KB, Downloads: 271)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: The Recursive Descent Parser
« Reply #1 on: July 24, 2017, 02:21:16 pm »
Hi Erik,

Did you use this in your Interpreter SICK64D1.zip that I just downloaded?

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: The Recursive Descent Parser
« Reply #2 on: July 24, 2017, 04:06:12 pm »
Hey eoredson,

I've always liked this project. I'm glad you still occasionally make noise about it! Just curious - is there any formal website or home for this? Does the project *.zip contain all documentation?

And a really off-the-wall question is, just how much of the code is from the underscore-ridden QB64 era, vs how much is mostly QB45 compatible?

(I could dig a bit and answer all this myself, but I figure the public questions make things easier.)

Thanks!
You're not done when it works, you're done when it's right.

Offline eoredson

  • Newbie
  • Posts: 55
  • Let the Farce be with you!
    • View Profile
    • Oredson QB45 Files At Filegate
Re: The Recursive Descent Parser
« Reply #3 on: July 24, 2017, 08:30:13 pm »
Thanks!

@bplus: Yes, it is the SICK engine.

STxAxTIC:
There is no web site I own that distributes it, and the .zip contains the entire project, including .doc

Interestingly enough, it was ported from 16-bit QB45 and that was not easy.

Unless https://goo.gl/pkQug8 qualifies as a web site..

Erik.
« Last Edit: July 24, 2017, 08:40:18 pm by eoredson »

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: The Recursive Descent Parser
« Reply #4 on: March 19, 2019, 06:55:54 am »
SICK engine is large and filled with lot of stuff but i have have something smaller
well not written in qb64 but is quite universal ..
i hope that Mark(B+) can convert this to qb64...

Code: QB64: [Select]
  1. 'recursive descent token evaluator
  2. '#lookahead
  3. int tc=0 : string token
  4. string tokens[7]
  5. tokens[1] = "2"
  6. tokens[2] = "*"
  7. tokens[3] = "("
  8. tokens[4] = "3"
  9. tokens[5] = "+"
  10. tokens[6] = "4"
  11. tokens[7] = ")"
  12.  
  13. sub gettok()
  14. tc++ : token = tokens[tc]
  15.  
  16. sub expr() as float
  17. float v = term()
  18. if token = "+": gettok() : v = v + term(): end if
  19. if token = "-": gettok() : v = v - term(): end if
  20.  
  21. sub term() as float
  22. float v = factor()
  23. if token = "*": gettok() : v = v * factor(): end if
  24. if token = "/": gettok() : v = v / factor(): end if
  25.  
  26. sub factor() as float
  27. float v
  28. if asc(token)>47  and asc(token)<58 'nums
  29. v = val(token) : gettok()
  30. if asc(token)=40 and asc(token)<>41 'match (...)
  31. gettok() : v = expr() : gettok()
  32.  
  33. 'execute---------------
  34. gettok() 'start
  35. float res = expr()
  36. print str res
« Last Edit: March 19, 2019, 08:53:30 am by Aurel »
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline odin

  • Administrator
  • Newbie
  • Posts: 92
  • I am.
    • View Profile
Re: The Recursive Descent Parser
« Reply #5 on: March 19, 2019, 08:09:04 am »
Posting code that's unusable in QB64 is not welcome. If the idea is awesome, adapt it to QB64 and post it or else just post it under off-topic.

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: The Recursive Descent Parser
« Reply #6 on: March 19, 2019, 08:52:51 am »
sorry i know that...
but i simply don't know how to translate this code to qb64.
I use Davs IDE .
main problem i have is with SUB routines
in another words i cannot figured proper shape..
i am looking into help of qb64 but i simply cannot found where is what?
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: The Recursive Descent Parser
« Reply #7 on: March 19, 2019, 09:02:19 am »
Here is what i transform but some things are wrong
Code: QB64: [Select]
  1. recursive descent token evaluator
  2. '
  3. dim tc as INTEGER : dim token as string : dim v as single
  4. DIM tokens() as string * 7
  5. tokens(1) = "2"
  6. tokens(2) = "*"
  7. tokens(3) = "("
  8. tokens(4) = "3"
  9. tokens(5) = "+"
  10. tokens(6) = "4"
  11. tokens(7) = ")"
  12.  
  13. sub gettok()
  14. tc=tc+1 : token = tokens(tc)
  15.  
  16. sub expr()
  17. v = term()
  18. if token = "+": gettok() : v = v + term(): end if
  19. if token = "-": gettok() : v = v - term(): end if
  20.  
  21. sub term()
  22. v = factor()
  23. if token = "*": gettok() : v = v * factor(): end if
  24. if token = "/": gettok() : v = v / factor(): end if
  25.  
  26. sub factor()
  27. if asc(token)>47  and asc(token)<58 'nums
  28. v = val(token) : gettok()
  29. if asc(token)=40 and asc(token)<>41 'match (...)
  30. gettok() : v = expr() : gettok()
  31.  
  32. 'execute---------------
  33. gettok() 'start
  34. float res = expr()
  35. print str(res)
« Last Edit: March 19, 2019, 09:05:47 am by Aurel »
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: The Recursive Descent Parser
« Reply #8 on: March 19, 2019, 09:17:06 am »
What language is it? It looks interesting

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: The Recursive Descent Parser
« Reply #9 on: March 19, 2019, 10:08:29 am »
hi vince
it is o2( Oxygen basic)
all basic dialects are very similar and that is good
but i simply stuck with such a simple code here in qb64 ..my fault
or i become rusty .. :)
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: The Recursive Descent Parser
« Reply #10 on: March 19, 2019, 10:23:56 am »
Hi Aurel
here's your code translated to QB64
Code: QB64: [Select]
  1. 'recursive descent token evaluator
  2. DIM SHARED tokens(8) AS STRING * 1
  3.  
  4. tc = 0
  5. tokens(1) = "2"
  6. tokens(2) = "*"
  7. tokens(3) = "("
  8. tokens(4) = "3"
  9. tokens(5) = "+"
  10. tokens(6) = "4"
  11. tokens(7) = ")"
  12.  
  13. 'execute---------------
  14. CALL gettok 'start
  15. res = expr#
  16. PRINT res
  17.  
  18. SUB gettok ()
  19.     tc = tc + 1
  20.     token = tokens(tc)
  21.  
  22. FUNCTION expr# ()
  23.     DIM v AS DOUBLE
  24.     v = term#
  25.     IF token = "+" THEN
  26.         gettok
  27.         v = v + term#
  28.     END IF
  29.     IF token = "-" THEN
  30.         gettok
  31.         v = v - term#
  32.     END IF
  33.     expr# = v
  34.  
  35. FUNCTION term# ()
  36.     DIM v AS DOUBLE
  37.     v = factor#
  38.     IF token = "*" THEN
  39.         gettok
  40.         v = v * factor#
  41.     END IF
  42.     IF token = "/" THEN
  43.         gettok
  44.         v = v / factor#
  45.     END IF
  46.     term# = v
  47.  
  48. FUNCTION factor# ()
  49.     DIM v AS DOUBLE
  50.     IF ASC(token) > 47 AND ASC(token) < 58 THEN 'nums
  51.         v = VAL(token)
  52.         gettok
  53.     END IF
  54.     IF ASC(token) = 40 AND ASC(token) <> 41 THEN 'match (...)
  55.         gettok
  56.         v = expr#
  57.         gettok
  58.     END IF
  59.     factor# = v
  60.  
output 14

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: The Recursive Descent Parser
« Reply #11 on: March 19, 2019, 10:31:21 am »
hi Jack and thank you!
now i see where is difference.
qb require # suffix for floating point number
globals need directive SHERED...
nice !

As we can see this few functions are really simple.
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: The Recursive Descent Parser
« Reply #12 on: March 19, 2019, 10:53:41 am »
hi Jack and thank you!
now i see where is difference.
qb require # suffix for floating point number
globals need directive SHERED...
nice !

As we can see this few functions are really simple.

Aurel something else about QB64 subs and functions you should know:
The arguments passed are by default By Reference, NOT By Value so if you change a value in sub it might go back to main program changed.

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: The Recursive Descent Parser
« Reply #13 on: March 19, 2019, 11:22:27 am »
Hey Mark
I see now that something is different ..
ok so default is byRef...mean pointered.
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: The Recursive Descent Parser
« Reply #14 on: March 19, 2019, 11:28:01 am »
@Aurel
in your function factor the second if clause makes no sense, you can replace it with else, '(' and ')' are simply ignored by the function expr
Code: QB64: [Select]
  1. FUNCTION factor# ()
  2.     DIM v AS DOUBLE
  3.     IF ASC(token) > 47 AND ASC(token) < 58 THEN 'nums
  4.         v = VAL(token)
  5.         gettok
  6.     ELSE
  7.         gettok
  8.         v = expr#
  9.         gettok
  10.     END IF
  11.     factor# = v
  12.  
« Last Edit: March 19, 2019, 11:30:08 am by jack »