[antlr-interest] Grammar issue

Sterling Mason sterling.mason at hotmail.com
Sun Nov 7 08:50:12 PST 2010


I have a very simple grammar where I am attempting to parse some C++ code.  The input is very simple and I am having trouble figuring out how to parse (lex?) a line.  What I want to do is match a '#define" and then the rest of the line.  I don't care what is in the rest of the line (even if empty) but I do want it passed to a processing function where I can examine its contents.  The code snippet I have used is

definestatement
    : '#define' defineoption
    ;

defineoption
    : RESTOFLINE
    ;
...
COMMENT
    :   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;

LINE_COMMENT
    : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    ;
    
RESTOFLINE
 : ~('\n'|'\r')* '\r'? '\n'
 ;

This was taken out of the example grammar for 'C' and modified.

The problem is that when I attempt to use a RESTOFLINE in the grammar, the parser stops with an Unexpected Token at the terminal */ of the comment in the header.  It doesn't seem to make any difference if I modify LINE_COMMENT to contain the RESTOFLINE item or not.

Questions:
1.  How can I capture the rest of the line into a string that I can examine in the function handling that expression?
2.  Why doesn't the above construct work?

The grammar generates and compiles ok in Visual Studio 2008.

Thanks
Sterling
  		 	   		  


More information about the antlr-interest mailing list