[antlr-interest] Overloaded Lexemes!

Mark Lentczner markl at glyphic.com
Wed Apr 28 08:59:38 PDT 2004


Your examples and grammar are unclear - is this a legal comment?
     COMMENT(xxx)yyy)\r\n
     COMMENT(compute x*(y+z) and return)\r\n
     COMMENT(now set x) x = 2*(y+z)\r\n
     COMMENT(there are spaces at the end)    \r\n
     COMMENT(on line one\r\none line two)\r\n

Specifically, does a comment end with:
     1) any close parenthesis?
     2) a matching close parenthesis?
     3) only a close parenthesis at the end of the line
     4) only a close parenthesis followed by white space followed by end 
of line.
And:
     ?) Can a comment contain embedded newlines?

I'm not trying to be pedantic - it is just that when one writes 
grammars, you are forced to really answer these sorts of questions.

If you are defining the language (not just implementing someone else's 
spec), then do you user's a favor and choose option 2 or perhaps option 
1.  However, given your grammar, I'm going to assume you are attempting 
option 3.

> COMMENT	: TEXT! LPAREN! (~('\r'|'\n'))* RPAREN! '\r''\n'
> 	{newline();};
> I've tried non-greedy options for the subrule (as discussed in the
> LEXER section of the ANTLR documentation) without success.
The problem comes from the fact that "~('\r'|'\n'))* will match any 
closing RPAREN.

Try either:

COMMENT: TEXT! LPAREN! ( options{greedy=false;}: .)* RPAREN! '\r' '\n'
     {newline();};

-or-

COMMENT: TEXT! LPAREN! ~('\r'|'\n'|')'))* RPAREN! '\r' '\n'
     {newline();};

Though be ware, these parse different grammars for comments (both are 
option 3 above, but answer the embedded newline question differently)



 
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