[antlr-interest] useing antlr to get key information from commentline

Stefan Misch stefan.misch at gmx.de
Fri Dec 10 02:20:54 PST 2010


Hi Joel Li,

I'm also new to ANTLR and had a look at your problem to learn more...

This modified grammar seems to work for me (at least AntlrWorks Interpreter
shows the correct tree):

=================
grammar TestComment;

all     :   (txt)*;

txt     :	comment {System.out.println("found comment");}
        |	content {System.out.println("found content");}
        |	NEWLINE
        ;
        
comment :   COMMENT_TAG STRING NEWLINE
        ;

content :	STRING NEWLINE
        ;

COMMENT_TAG
        :   '#';

NEWLINE :	'\r'? '\n';

STRING  :   ~COMMENT_TAG ~('\n'|'\r')*
        ;
========================

The problem is, that STRING is not allowed to start with a COMMENT_TAG,
because otherwise there would be no difference between a comment and
content. Please also note the addition of just a NEWLINE to txt, which
allows for empty lines, too.

HTH,
Stefan


-----Ursprüngliche Nachricht-----
Von: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] Im Auftrag von ???
Gesendet: Freitag, 10. Dezember 2010 03:07
An: antlr-interest at antlr.org
Betreff: [antlr-interest] useing antlr to get key information from
commentline

i am new to antlr. i put key infomation in the comment and try to use antlr
to retrive comment in text file.

here is the grammar:


grammar T;
options {backtrack=true;}
all     :       (txt)*;
txt     :comment {System.out.println("found comment");}
        |content {System.out.println("found content");}
        ;
comment
        :       COMMENT_TAG content
        ;
content
        :       STRING NEWLINE
        ;

      
COMMENT_TAG
        :        '#'
       
        ;
      
NEWLINE: ('\r'? '\n')+;

STRING
        :       ('\u0010'..'\uffff')*
        ;





the input text like this :


#command parameter parameter
hello,this is text;

it dose not work,the output is


found content
found content


my expectation is follow :


found comment
found content
.

how to modify the grammar?
Regards
Joel li


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




More information about the antlr-interest mailing list