[antlr-interest] comment must be preceded by whitespace
mzukowski at yci.com
mzukowski at yci.com
Wed Mar 17 16:22:12 PST 2004
You are running into linear approximation lookahead issues.
I would recommend setting a flag when you hit a newline and unsetting it
after the first non-whitespace character. If the flag is set and you see
'/' then it is a comment. If not it's just a '/'.
The easiest way to deal with the flag is to set in in the newline() function
and unset it in match() when match is called on a non-WS character.
Monty
-----Original Message-----
From: charlesskelton [mailto:c.skelton at skelton.de]
Sent: Wednesday, March 17, 2004 3:46 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] comment must be preceded by whitespace
I'm new to antlr.
I'm trying to write lexer for a language, k, in which single line
comments begin with a '/'. If there is no whitespace before the '/',
or the '/' is not in column 1, then it is not a comment.
e.g
/this is a comment
/this is also a comment
a:+/!10 /this text after the second forward slash is a comment
In this language, +/!10 actually means sum over numbers 0 thru 9,
but that is not very relevant here, except it shows the other use
of '/'.
I did try to define the comment as
SL_COMMENT
:
((" /") (~('\n'|'\r'))* ('\n'|'\r'('\n')?))
| (("\t/") (~('\n'|'\r'))* ('\n'|'\r'('\n')?))
| (("\f/") (~('\n'|'\r'))* ('\n'|'\r'('\n')?))
| {getColumn()==1}? (('/') (~('\n'|'\r'))*
('\n'|'\r'('\n')?))
;
but it complains when the first char is not a '/'. I would define it
as
(("\n/") (~('\n'|'\r'))* ('\n'|'\r'('\n')?))
but I need \n to match WS type.
Any help would be greatly appreciated.
Many thanks,
Charlie
Yahoo! Groups Links
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