[antlr-interest] Re: C++ Comment like parsing

lgcraymer lgc at mail1.jpl.nasa.gov
Fri Jan 9 17:49:23 PST 2004


Werner--

You need to include
      charVocabulary='\003' .. '\377';
in your lexer options.  (Other ranges are possible, but this is the usual choice.)
The ~( * | '\n' | '\r' )* won't match " x " without it.

--Loring


--- In antlr-interest at yahoogroups.com, "wbsza" <bloudraak at h...> wrote:
> Hi, 
> 
> I'm aiming at writing a JSP/ASP like parser. For starters, I wrote a 
> grammar to parse a standard C++ comment. However, my grammar doesn't 
> work. If I pass /* x */ to the parser, I get an error. Actually, I 
> want to get just "x".  I'm not sure what to do to get the parser to 
> read the text between /* and */.
> 
> Below is my grammar. Note that the comment part was taken from the 
> Java grammar.
> 
> Thanks
> Werner
> 
> 
> 
> options
> {
>   language="Cpp";
> }
> 
> {
> #include <iostream>
> #include <antlr/Token.hpp>
> 
> }
> class MyParser extends Parser;
> 
> startRule
>     :   n:ML_COMMENT
>         {std::cout << "Hi there, " /*<< n->getText()*/ << std::endl;}
>     ;
> 
> class MyLexer extends Lexer;
> options
> {
>   k=2;
> }
> 	
> 	// multiple-line comments
> ML_COMMENT
> :	"/*"
> 
> 			options {
> 				generateAmbigWarnings=false;
> 			}
> 		:
> 			{ LA(2)!='/' }? '*'
> 		|	'\r' '\n'		{newline();}
> 		|	'\r'			{newline();}
> 		|	'\n'			{newline();}
> 		|	~('*'|'\n'|'\r')
> 		)*
> 		"*/"	
> 	;


 

Yahoo! Groups Links

To visit your group on the web, go to:
 http://groups.yahoo.com/group/antlr-interest/

To unsubscribe from this group, send an email to:
 antlr-interest-unsubscribe at yahoogroups.com

Your use of Yahoo! Groups is subject to:
 http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list