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 <[email protected]> > To: [email protected] > 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 > List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
