[antlr-interest] Avoiding warnings without code bloat
David Piepgrass
qwertie256 at gmail.com
Wed Jun 27 10:05:21 PDT 2007
> >// Strings
> >SQ_STRING: '\''! ({true}? ESC_SEQ | ~'\'')* '\''!;
> >DQ_STRING: '"'! ({true}? ESC_SEQ | ~'"')* '"'!;
> >BQ_STRING: '`'! ({true}? ESC_SEQ | ~'`')* '`'!;
>
> Does ! work in v3 lexers now? (Maybe I need to pay closer
> attention.)
Whoops! It doesn't seem to work; the Text property returns the whole
matching string, and even modifying the text property ($text = ...)
doesn't seem to have any effect (But I'm using C#, and modifying $text
in a fragment rule).
Why doesn't ANTLR give a warning?
*** BONUS C# code for printing the output of a lexer ***
// Since ANTLRWorks can't debug a lexer, everyone needs code like this
string s = System.Console.ReadLine();
ANTLRStringStream input = new ANTLRStringStream(s);
// Create an ExprLexer that feeds from that stream
ExprLexer lexer = new ExprLexer(input);
// Create a stream of tokens fed by the lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
IToken t;
while ((t = tokens.LT(1)).Type != ExprLexer.EOF)
{
Debug.Assert(t.Type == tokens.LA(1));
System.Console.WriteLine("{0} <{1}>", ExprParser.tokenNames[t.Type], t.Text);
tokens.Consume();
}
More information about the antlr-interest
mailing list