kchadha
kchadha

Reputation: 61

ANTLR 3.5 generates "extraneous parentheses" warning with C++ target

I am using the C++ target in ANTLR v 3.5 and the generated Lexer.cpp file has a bunch of extraneous parentheses warnings. Specifically the warning is:

warning: equality comparison with extraneous parentheses [-Wparentheses-equality]

This happens in five places, the corresponding generated lexer code is:

void
QualifiedNameLexer::mTokens()
{
    {
        //  QualifiedName.g:1:8: ( T__11 | T__12 | T__13 | T__14 | T__15 | PN_CHARS_BASE | HELPER_RULE_FOR_OTHER_PUNCTUATION_CHARS | PN_CHARS_OTHERS | DIGIT )

        ANTLR_UINT32 alt2;

        alt2=9;

        {
            int LA2_0 = this->LA(1);
            if ( (LA2_0 == '-'))
            {
                alt2=1;
            }
            else if ( (LA2_0 == '.'))
            {
                alt2=2;
            }
            else if ( (LA2_0 == ':'))
            {
                alt2=3;
            }
            else if ( (LA2_0 == 0x00B7))
            {
                alt2=4;
            }
            else if ( (LA2_0 == '_'))
            {
                alt2=5;
            }
            ...
        }
    }
}

Here is the grammar that corresponds to the generated lexer with the warnings.

grammar QualifiedName;

options {
    language = Cpp;
    k = 2;
    backtrack = true;
    memoize = true;
}

@parser::includes {
    #include "QualifiedNameLexer.hpp"
}

@lexer::namespace { User }

@parser::namespace { User }

@lexer::traits {
     class QualifiedNameLexer; class QualifiedNameParser;
     typedef antlr3::Traits< QualifiedNameLexer, QualifiedNameParser > QualifiedNameLexerTraits;
     typedef QualifiedNameLexerTraits QualifiedNameParserTraits;
}


/******************** Parser Rules *****************************/

qualified_name
    :   ( pn_prefix ':' )? pn_local
      | pn_prefix ':'
    ;

pn_prefix
    :   PN_CHARS_BASE ((pn_chars | '.')* pn_chars)?
    ;

pn_local
    :   ( pn_chars_u | DIGIT | PN_CHARS_OTHERS )
        (
            ( pn_chars | '.' | PN_CHARS_OTHERS )*
            ( pn_chars | PN_CHARS_OTHERS )
        )?
    ;

pn_chars_u
    : PN_CHARS_BASE | '_'
    ;

pn_chars
    : pn_chars_u | '-' | DIGIT | '\u00B7' | HELPER_RULE_FOR_OTHER_PUNCTUATION_CHARS
    ;

/************************* Lexer Rules ******************************/

PN_CHARS_BASE
    : 'A'..'Z' | 'a'..'z'
    | '\u00C0'..'\u00D6'
    | '\u00D8'..'\u00F6'
    | '\u00F8'..'\u02FF'
    | '\u0370'..'\u037D'
    | '\u037F'..'\u1FFF'
    | '\u200C'..'\u200D'
    | '\u2070'..'\u218F'
    | '\u2C00'..'\u2FEF'
    | '\u3001'..'\uD7FF'
    | '\uF900'..'\uFDCF'
    | '\uFDF0'..'\uFFFD'
//  | '\uD800\uDC00'..'\uDB7F\uDFFF' (u+10000 to u+EFFFF) not supported by this grammar.
    ;

HELPER_RULE_FOR_OTHER_PUNCTUATION_CHARS
    : '\u0300'..'\u036F' | '\u203F'..'\u2040'
    ;

PN_CHARS_OTHERS
    : '/'
    | '@'
    | '~'
    | '&'
    | '+'
    | '*'
    | '?'
    | '#'
    | '$'
    | '!'
    | PERCENT
    | PN_CHARS_ESC
    ;

fragment
PN_CHARS_ESC
    :   '\\' ( '=' | '\'' | '(' | ')' | ',' | '-' | ':' | ';' | '[' | ']' | '.' )
    ;

fragment
PERCENT
    : '%' HEX HEX
    ;

fragment
HEX
    :   '0'..'9' | 'A'..'F' | 'a'..'f'
    ;

DIGIT
    : '0'..'9'
    ;

Is there something I can change in the grammar to get rid of these warnings when antlr generates a lexer? Or is this an ANTLR bug?

Upvotes: 0

Views: 390

Answers (1)

ibre5041
ibre5041

Reputation: 5288

I was about to fix it but I gave up. The code is generated by nested templates and it's hard to predict what you will get at the end.

Also g++ and Clang treats this code differently. AFAIK Clang generates different warnings and extraneous parentheses somehow treat warning generated.

I think best way would be to push-ignore this warning at the beginning of the header and pop-ignore at the and. I tried it but I got stuck somewhere.

Maybe because the warning is not triggered while reading the source but while instantiating the template. I do not remember.

PS: your grammar is quite simple, the generated conditions might be much complex.

Upvotes: 1

Related Questions