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: [email protected]
[mailto:[email protected]] Im Auftrag von ???
Gesendet: Freitag, 10. Dezember 2010 03:07
An: [email protected]
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



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

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to