[antlr-interest] Javadoc style parsing

Andy Tripp antlr at jazillian.com
Wed Jan 2 16:35:19 PST 2008


Adam Connelly wrote:
> Hi,
>
> Just wanted to ask for some ideas.  I'm designing a language and have 
> most of the grammar done (I've still to add a few bits in and put in 
> the template generation).  The language is a modeling language for 
> describing some database objects and I'd like to allow some meta-data 
> to hold a human friendly description of these objects.
>
> I had an idea of having a special type of comment (like in javadoc) 
> that I would send to a different channel than the standard parser.  I 
> could then parse these comments separately (although this might not 
> end up being desirable).  This would mean that I wouldn't have to put 
> in references to an optional comment rule wherever comments were allowed.
>
> The downside to this that I can see at the moment is that I'm not sure 
> how I would then get the information about the object that each 
> comment was attached to.
>
> Any ideas?
>
> Adam
The hard part is associating each comment with a particular node in the 
AST. Peter Van Der Ahe at Sun wrote a
good paper about how hard that is, and even that paper only scratched 
the surface.

Consider this code:
----------------------------------------
// here is a comment that appears to be at the top of the file, but only 
because there are a few blank lines after it.

// a comment that seems to "go with" the following line, and not the 
previous comment, but again, only because of blank lines
int i;

for (int i=0; i<10 /* what does any inline comment go with? */; i++)
   // a comment that "goes with" the block inside the for loop
  {
      // another comment that "goes with" the block inside the for loop
  }  // does this comment "go with" the for loop, or the following code?
----------------------------------------

If you have control of the language (sounds like you do), I'd seriously 
consider making comments a real part of the grammar,
perhaps having a "comment statement", as opposed to the C, C++, Java, 
and many other languages approach of
allowing statements anywhere and having a preprocessor throw them out.






More information about the antlr-interest mailing list