Rosetta code: help needed

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
Ed Davis
Posts: 37
Joined: Jul 28, 2008 23:24

Rosetta code: help needed

Post by Ed Davis »

I've created some (fun!) tasks over at Rosettacode:

http://rosettacode.org/wiki/Compiler/lexical_analyzer
http://rosettacode.org/wiki/Compiler/syntax_analyzer
http://rosettacode.org/wiki/Compiler/code_generator
http://rosettacode.org/wiki/Compiler/vi ... nterpreter
http://rosettacode.org/wiki/Compiler/AST_interpreter

I've put up a FreeBasic solution for the lexical analyzer task (feel free to improve it!)

It would be great if folks here could write FreeBasic (or other) solutions for the other four tasks.

Additionally, if any of the specifications are ambiguous and/or not detailed enough, let me know and I'll improve them.

Thanks!
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Rosetta code: help needed

Post by Tourist Trap »

Very interesting. Far beyond my knowledge but nice reading.
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: Rosetta code: help needed

Post by frisian »

Ed Davis wrote: I've put up a FreeBasic solution for the lexical analyzer task (feel free to improve it!)
I added sleep because Windows and FBEdit(mod) closes the window when program ends making it impossible to see any error message(s).

I also changed "\" on a few places with chr(92). The code highlighter seems to get confused when it encounters \".

All alterations are marked with #added# and #changed#. I will clean up the listing in a few days.

Regards Frisian.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Rosetta code: help needed

Post by sancho2 »

Very interesting projects.
I looked at the first project, the lexical analyzer.
The two functions, is_digit and is_alnum, have unnecessary empty string checks. Look at is_digitx and is_alnumx in the following code:

Code: Select all

function is_digit(byval ch as string) as long
    is_digit = (ch <> "") and ch >= "0" and ch <= "9"
end function
 
function is_alnum(byval ch as string) as long
    is_alnum = (ch <> "") and ((ucase(ch) >= "A" and ucase(ch) <= "Z") or (is_digit(ch)))
end Function

function is_digitx(byval ch as string) as long
    is_digitx = ch >= "0" AndAlso ch <= "9"
end function
 
function is_alnumx(byval ch as string) as long
    is_alnumx = (ucase(ch) >= "A" AndAlso ucase(ch) <= "Z") OrElse (is_digit(ch))
end Function

Print is_digit("0")
Print is_digitx("0")
Print is_digit("")
Print is_digitx("")
Print is_alnum("1")
Print is_alnumx("1")
Print is_alnum("")
Print is_alnumx("")

sleep
The function install has a weird bit of code to init the array. It can be reduced to eliminate one of the ubound calls:

Code: Select all

function install(byval s_name as string, byval tok as Token_type) as integer
    dim n as integer
 
    n = ubound(symtab)
    redim preserve symtab(n + 1)
    n = ubound(symtab)
----------------------------------------------------------------------------
function install(byval s_name as string, byval tok as Token_type) as integer
	Dim n As Integer = ubound(symtab) + 1
	ReDim Preserve symtab(n)    
Until now I haven't seen anyone using 'system' to close files. Not sure I would do it that way.

On line 200 you declare a boolean type var and assign it the return value of is_digit. is_digit is a function that returns a long.
I scoured the code and can find no reason not to declare the two functions is_digit and is_alnum booleans as well.

I am going to look closer at this analyzer as it is in line with what I am playing around with. Thanks for sharing it.
Post Reply