[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Tips on adding GOTO, GOSUB to MiniBasic



Arman Anwar wrote:
> 
> Etienne ,
> 
> There are several reasons why I want to implement the GOTO construct:
> 
> a. A lot of the CC tools out there support AST like constructs but I
> wanted to see how one would effectively implement a GOTO in such a
> framework. If one wants to implement a completely languages such as
> C/C++ the GOTO has to be taken care of (in you simple C it is part of
> the token list)

Be careful, here:  Handling "GOTO" in (1) a compiler or (2) in an
interpreter is quite different.  Handling "GOTO" using an AST is just
fine in a compiler.  In fact, I would strongly suggest to use an AST to
compile C/C++.

On the other hand, "recursive interpretation" of unstructured gotos on
an AST (which is structured by definition) is not a good match.  Linear
interpretation is quite possible though.  The problem is you don't gain
much from the AST structure "above" the Statement level.  But, within a
single statement, you can still use the all the advantages of the AST
structure.

Also, you get "upfront" syntax checking when you use a SableCC approach,
as opposed to the traditional "parse as you interpret" approach.

> b. There is a lot of legacy code centered aroung GOTOs that people
> would not like to port

I know, I know...  I just want to encourage people to consider "modern"
approaches, when possible. 

> But thanks for you valuable input .. I think I shall probably try the
> linear approach as one with ASTs seems too convoluted.

As I told you,  you can still benefit from the AST structure within each
statement,  so you should  seriously consider a hybrid linear/AST
approach.  Don't forget that you get lexing/parsing/"node classes" for
almost free using SableCC.  All you need is to do one initial pass over
the AST to "linearize" all the statements.

Have fun.

Etienne
-- 
----------------------------------------------------------------------
Etienne Gagnon, M.Sc.                        e-mail: egagnon@j-meg.com
Author of SableCC:                 http://www.sable.mcgill.ca/sablecc/
----------------------------------------------------------------------