Home » 2019 » February » 15 » Recursive descent with tokens
2:29 PM
Recursive descent with tokens

With parens :

'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]
end sub

sub expr() as float
float v = term()
if token = "+": gettok() : v = v + term(): end if
if token = "-": gettok() : v = v - term(): end if
return v
end sub

sub term() as float
float v = factor()
if token = "*": gettok() : v = v * factor(): end if
if token = "/": gettok() : v = v / factor(): end if
return v
end sub

sub factor() as float
float v
if asc(token)>47  and asc(token)<58 'nums
v = val(token) : gettok()
end if
if asc(token)=40 and asc(token)<>41 'match (...)
gettok() : v = expr() : gettok()
end if
return v
end sub

'exec-----------------
gettok() 'start
float res = expr()
print str res

Views: 104 | Added by: Zlatko | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *: