Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursive Descent in BASIC
03-20-2019, 05:29 AM
Post: #1
Recursive Descent in BASIC
I was thinking that recursive descent in Basic as i use
in ruben interpreter work same standalone
but i found that is not Ed explained for one turn more:

so in oxygen basic i need two while loops like this:

Code:
'recursive descent token evaluator
#lookahead
int tc=0 : string token
string tokens[7]
tokens[1] = "2"
tokens[2] = "*"
tokens[3] = "("
tokens[4] = "3"
tokens[5] = "+"
tokens[6] = "4"
tokens[7] = ")"
'-----------------------------------------------------
sub gettok()
tc++
token = tokens[tc]
print "TOKEN: " + token + "  ,TC: " + str(tc)
if tokens[tc+1] <> "" then return
end sub
'----------------------------------------------------
sub expr() as float
float v
If token = "-"
v = -(term())
else
v = term()
end if

while token = "+" or token = "-"
if token = "+": gettok() : v = v + term(): end if
if token = "-": gettok() : v = v - term(): end if
wend

return v
end sub
'---------------------------------------------------
sub term() as float
float v
v = factor()

while token = "*" or token = "/"
if token = "*": gettok() : v = v * factor(): end if
if token = "/": gettok() : v = v / factor(): end if
wend

return v
end sub
'-------------------------------------------------------

sub factor() as float
float v
if asc(token)>47  and asc(token)<58 'nums
v = val(token)
print str(v)+ " factor"
gettok()
end if

if asc(token)=40 and asc(token)<>41 'match (...)
gettok() : v = expr() : gettok()
end if


return v
end sub

'execute-----------------------------------------------------
gettok()'start
float res = expr()
print "RESULT=" + str(res)
Find all posts by this user
Quote this message in a reply
03-21-2019, 03:24 AM
Post: #2
RE: Recursive Descent in BASIC
In this version i remove WHILE loops
Added var look which check one token lookahead.
and add INSTR () function to check last token if is not operator.
And looks that work in o2..
In qb64 no matter what i do without dummy load recursion
simply not work .

Code:
'recursive descent token evaluator
#lookahead
int tc=0 : string token,look
string tokens[8]
tokens[1] = "2"
tokens[2] = "*"
tokens[3] = "("
tokens[4] = "10"
tokens[5] = "-"
tokens[6] = "2"
tokens[7] = ")"
'tokens[8] = "+"
'-----------------------------------------------------
sub gettok()
tc++
token = tokens[tc] : look = tokens[tc+1]
print "TOKEN: " + token + "  ,TC: " + str(tc)
if tokens[tc+1] <> "" then return
end sub
'----------------------------------------------------
sub expr() as float
float v=0.0
If token = "-"
v = -(term())
else
v = term()
end if

'while token = "+" or token = "-"
'while look <> ""
if token = "+": gettok() : v = v + term(): end if
if token = "-": gettok() : v = v - term(): end if

'wend


return v
end sub
'---------------------------------------------------
sub term() as float
float v
v = factor()

'while token = "*" or token = "/"
if token = "*": gettok() : v = v * factor(): end if
if token = "/": gettok() : v = v / factor(): end if
'wend

return v
end sub
'-------------------------------------------------------

sub factor() as float
float v

if asc(token)>47  and asc(token)<58 'nums
v = val(token) : gettok()
if instr("+-*/",look) = 0
      return v
end if  
end if


if asc(token)=40 and asc(token)<>41 'match (...)
gettok() : v = expr() : gettok()
end if


return v
end sub

'execute-----------------------------------------------------
gettok()'start
float res = expr()
print "RESULT=" + str(res)
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: