Patching ANTLR

As far as I can tell, there are two major flaws with ANTLR's default TreeParsers:

  1. Token information is not retained, so you can't keep track of which line number of the original source code a token came from, which makes it impossible to give good typechecker error messages
  2. ANTLR's default rules for TreeParsers will "shave off" any decorations you add to the tree
These flaws can be seen by examining the "old" source code, linked below.

Fortunately, ANTLR is OO, and OO principles tend to make defects in design and implementation easy to correct in a localized fashion. That's the case with this problem. I have written two new classes which you can graft onto your existing solutions with a minimum of hassle, and these classes solve both of the flaws described above.

Here is a summary of the changes you make for this to work. Two new classes are introduced into the hierarchy, as seen below (the new classes are in bold):

   Old Hierarchy                       New Hierarchy
   -------------                       -------------

   CommonAST                     CommonAST
      |                             |
      |                             |
      v                             v
    MyAST                     MyKludgeASTNode <======> MyASTNode
                                                          |
                                                          |
                                                          v
                                                        MyAST

An example

I have some sample code. I am too tired to give a lengthy description now. You can grab a directory full of code and run

   . ./pf
   . ./y
to compile and run the stuff and see how the old version fails and the new version works.

  • old code
  • new code
  • Here's a summary of the differences:

  • differences
  • Here's the C++ version:

  • new code

  • Last updated on Fri Oct 22 02:37:44 EDT 1999 by Brian McNamara