[antlr-interest] Lexer Help, line-comment begins with a character that is also an operator
Anand.Vidyasagar at sungard.com
Anand.Vidyasagar at sungard.com
Mon Sep 1 02:14:52 PDT 2008
In our small language we have a notion that if a line begins with a '*'
character on Column 0 we treat it is a line comment.
However the '*' happens to be the multiplication operator in the
language at the same time.
Here is my Grammar so far,
script
:
(expression SEMICOLON)+
;
expression
:
lvalue operator (lvalue | expression)
{
System.out.println("Expression : "+ $expression.text);
}
;
lvalue
:
FLOAT | NUMERIC
;
FLOAT
:
DIGIT+ ('.' DIGIT+)
;
NUMERIC
:
| DIGIT+
;
fragment
DIGIT
: '0'..'9'
;
operator
options { k=2;}
:
'*'
| '/'
| '+'
| '-'
| '**'
| '<<'
| '>>'
;
WS : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
;
LINE_COMMENT
: '*' ~('\n'|'\r')* '\r'? ('\n')
{
if(getCharPositionInLine() == 0){
System.out.println("Found Line Comment :" + getLine());
$channel=HIDDEN;
}
}
;
Now for an Input as below I get an error because the lexer treats '*' as
a comment, what do I have to do to make sure the '*' is recognized as a
comment if it starts on Column 0 and as an operator elsewhere
*THIS IS A LINE COMMENT
1+2+3;
*ANOTHER LINE COMMENT, The Following gives me an Exception
1+4*3;
And here is the output when I run the parser
Found Line Comment :2
Found Line Comment :5
Found Line Comment :6
Expression : 2+3
Expression : 1+2+3
line 5:2 no viable alternative at input '4'
BR.recoverFromMismatchedToken
Regards,
Anand Vidyasagar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080901/cdbc7ab7/attachment.html
More information about the antlr-interest
mailing list