Sunday, July 01, 2007

ANTLR Warning suppression workaround

On p.285 of the ANTLR book it implies that you cannot suppress warnings in ANTLR v3 like you could in v2.

However, it appears that a semantic predicate works nicely as a workaround:
// Strings
SQ_STRING: '\''! ({true}? ESC_SEQ | ~'\'')* '\''!;
DQ_STRING: '"'! ({true}? ESC_SEQ | ~'"')* '"'!;
BQ_STRING: '`'! ({true}? ESC_SEQ | ~'`')* '`'!;
fragment ESC_SEQ: '\\' ~('\n'|'\r');
The generated code now contains LL(2) lookahead for no reason, and some redundant code. For example, code that originally read
   if ( (LA19_0 == '\"') )
{
alt19 = 1;
}
Now says
   if ( (LA19_0 == '\"') )
{
int LA19_1 = input.LA(2);
if ( (true) )
{
alt19 = 1;
}
}
However, its behavior appears to be the same.

The compiler will emit some "unreachable code" warnings. In C# you can disable them like this:
grammar Expr;
options {
language=CSharp;
}
@lexer::members {
#pragma warning disable 0162
}
@parser::members {
#pragma warning disable 0162
}

0 Comments:

Post a Comment

<< Home