[antlr-interest] EOF in Lexer- how to?
Alexey Demakov
demakov at ispras.ru
Mon Jan 16 02:02:14 PST 2006
Make NewLine at the end of single line comment optional:
SingleLineComment :"//" ( ~('\r' | '\n') )* ( NewLine )? ;
It will match NewLine everywhere except
// comment text <EOF>
Regards,
Alexey
-----
Alexey Demakov
TreeDL: Tree Description Language: http://treedl.sourceforge.net
RedVerst Group: http://www.unitesk.com
----- Original Message -----
From: Tomasz Jastrzebski
To: antlr-interest at antlr.org
Sent: Monday, January 16, 2006 12:03 PM
Subject: [antlr-interest] EOF in Lexer- how to?
Hi Everybody,
Is it possible to recognize EOF in the lexer?
Ok, why would someone wanted to do it in the first place?
Lets suppose I want my lexer to recognize a SingleLineComment, let's say Java "// com ment" style. My lexer rules should look more
or less like this:
NewLine :(options{greedy=true;}:"\r\n" | '\r' | '\n' ) ;
SingleLineComment :"//" ( ~('\r' | '\n') )* NewLine ;
But there is a problem here. What if my input stream consists of only single comment and no NewLine? E.g.
// comment text <EOF>
This lexer will not recognize such an input correctly.
That is why I w ant my lexer to be able to treat EOF as NewLine.
However it seems like I can not use or define EOF token within Lexer. An attempt to use '\uFFFF' within the NewLine rule seems to
block the lexer and lead to unpredictable results.
I would appreciate any help.
More information about the antlr-interest
mailing list