[antlr-interest] Parsing comments

Indhu Bharathi indhu.b at s7software.com
Thu Sep 17 08:33:03 PDT 2009


Try using semantic predicates to look-ahead and decide whether to emit
SLCOMMENT or TEMPLATE

 

Something like:

 

fragment SLCOMMENT

                :               ;

 

fragment TEMPLATE

                :               ;

 

SLCOMMENT_OR_TEMPLATE

                :               '//' (

                                                { input.LA(1)=='@' }?=> (
options {greedy=false;} : . )* EOL    { $type=TEMPLATE; }

                                |              ( options {greedy=false;} : .
)* EOL    { $type=SLCOMMENT; }

                                )

                ;

 

Cheers, Indhu

 

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Soren Holstebroe
Sent: Thursday, September 17, 2009 8:25 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Parsing comments

 

Hi,

I have this project where C++ templates are being unfolded and wrapped in
.NET. This wrapping is performed by some perl-scripts which over time have
become quite messy, so I wanted to rewrite the wrapper using ANTLR.

The way the current template wrapper works is by parsing simple template
expressions defined in the comments preceding a function definition. This is
similar to documentation frameworks like doxygen, where the documentation is
embedded in the comments above functions, classes, etc.
The simple syntax contains elements like this:
// @TEMPLATE <int, float>
which tells the wrapper to unfold the template using an int for the first
template type and a float for the second.

My problem is that I am already stuck in defining the lexer. 
Since all wrapper expressions start with '@' I tried to define a token like:
WRAPPERSTART : SLASHSLASH SPACE* '@'
and some grammar rules based on that token. 
However that doesn't work since the lexer will always choose the longest
matching rule, which would always be the normal single line comment rule.

I can imagine that there are many approaches to tackle this problem and some
hints would be greatly appreciated.

Soren

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090917/0b3b0cec/attachment.html 


More information about the antlr-interest mailing list