[antlr-interest] Comments / White-Space and All The Rest

Randall R Schulz rschulz at sonic.net
Fri Nov 24 08:21:59 PST 2006


Hi,

I would like to integrate comments into the stream of top-level 
constructs recognized by my ANTLR (v3) grammar.

Specifically, I'd like to treat comments in the input something like 
JavaDoc comments in that I accumulate them (merge their contents in a 
single StringBuilder instance) as they appear and then when the parser 
encounters a top-level construct (the analogy would be a method or 
field definition in Java source code) associate the accumulated 
comment, if any, with that construct.

I've had partial success, but there's one problem: It appears all the 
comments are processed before any of the parsing happens. Thus the 
comments don't properly interleave with the non-comment content and 
don't get associated with the proper constructs. Instead, they all 
appear at the beginning, merged into a single comment block.

How can I get the kind of behavior I want? Namely that comment action 
code is processed in sequence with the parsing process?


Thanks.

Randall Schulz


More information about the antlr-interest mailing list