[antlr-interest] Which approach for an Interpreter: Tree Grammar vs AST-Visitor
Loring Craymer
lgcraymer at yahoo.com
Thu Feb 3 00:58:37 PST 2011
The "best" way to implement an interpreter is to compile to byte code and
interpret the byte code--the interpreter is then a large case statement
surrounded by a loop which fetches the next byte to be interpreted and that
value is used as an index into the case statement. That provides the code
separation that you want and provides a good lesson in designing a virtual
machine. As to the tree grammar or visitor choice, consider doing
both--possibly by dividing the class. The tree grammar approach is superior in
the general case, but an important lesson for students to learn the value of
tools as opposed to automatically hand coding solutions--exposing students to
alternatives is good for them.
--Loring
----- Original Message ----
> From: bill punch <punch at cse.msu.edu>
> To: antlr-interest at antlr.org
> Sent: Wed, February 2, 2011 5:36:38 PM
> Subject: [antlr-interest] Which approach for an Interpreter: Tree Grammar vs
>AST-Visitor
>
> I'm designing a project for my compiler class, and we are at the stage
> of building an interpreter for our grammar. Before going farther, let me
> say first that ANTLR is great and makes the whole process a lot easier.
> However, I'm converting the course and, being new to ANTLR, have a few
> questions. Here is one.
>
> I was using Pattern 25, the Pie language, from LIP as a guideline, but
> I'm a little confused about the best approach. Pattern 25, Pie,
> constructs an AST then uses hand-written code to do the visiting. I like
> the approach, as more complex code can be embedded in the visitor code.
> However, instead of writing my own visitor, I could have the students
> write a tree grammar to visit the AST nodes. But it seems that I would
> still be better off putting the exec type code in a separate file.
>
> So would it be better to have an example like the Pie language use a
> tree grammar or is the hand-written visitor code a better approach? What
> are the pros and cons? Any help appreciated.
>
> --
> >>>bill<<<
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
>http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
More information about the antlr-interest
mailing list