[antlr-interest] Comment Problem

Thomas Dill thomas.dill at teleport.ch
Mon May 4 02:04:26 PDT 2009


I tried your solution but wasn't successful. I understand the point with the
NLCHARS and NEWLINE but this doesn't really change something with my origin
problem.

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Idle
Sent: Donnerstag, 30. April 2009 17:40
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Comment Problem

 

Thomas Dill wrote: 

Hi there.

 

I got the following Problem:

 

In the syntax, I want to parse, a comment line starts with "-*" and ends at
the end of the line. It's possible that an asterisk can appear in a "text".

 

I can't find a solution. Below there's what I tried (and many other things).
Can somebody help me?

You need to eat the comment in your lexer and ignore it in the parser
(unless it is predictable and you want to preserve it).

COMMENT : '-*' (~(NLCHARS))* NEWLINE ;

fragment NLCHARS
: '\n' | '\r' | '\u2028'| '\u2029' ;

NEWLINE : NLCHARS+ ;

However, your grammar only defines these tokens so the set
~(COMMENT_START|NEWLINE) would not have anything to work with any way. Start
by eating the comments in the lexer though.

You should download the example grammar set and read through them, they will
help you a lot.

Jim




 

Example:

 

TABLE FILE CAR

PRINT * -* prtints all fields

BY MODEL

END

 

ANTLR Code:

grammar FexParser;

options {k=3; backtrack=true; memoize=true;}

 

report    :    (comment | text | NEWLINE)+ ;

 

comment   :    (COMMENT_START ~(NEWLINE))* ;

text      :    (~(COMMENT_START | NEWLINE))+;

 

fragment COMMENT_START

          :    '-*';

 

WS        :    ('\t' | '\v' | '\f' | ' ' | '\u00A0')     {$channel =
HIDDEN;};

NEWLINE   :    '\n' | '\r' | '\u2028'| '\u2029'    ;

 

Best regards,

Tom

 



  _____  



 
 
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address
  

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090504/50bb9a7c/attachment.html 


More information about the antlr-interest mailing list