[antlr-interest] GCC lexer warnings with C target

Wincent Colaiuta win at wincent.com
Wed Jun 6 06:55:14 PDT 2007


Given the following grammar file, "Simple.g":

   grammar Simple;
   options {
     language = C;
   }
   FOO: 'foo';
   thing: .* EOF;

And generated as follows:

   java org.antlr.Tool Simple.g

Using ANTLR 3:

   ANTLR Parser Generator  Version 3.0 (May 17, 2007)  1989-2007

And then feeding the output to GCC:

   gcc main.c SimpleLexer.c SimpleParser.c -lantlr3c -o simple

GCC issues this warning while compiling the lexer:

   SimpleLexer.c:198: warning: ‘mFOO’ declared inline after being called
   SimpleLexer.c:198: warning: previous declaration of ‘mFOO’ was here

This is with GCC 4:

   i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc.  
build 5367)

Running on Mac OS X 10.4.9 (Intel):

   Darwin cuzco.local 8.9.1 Darwin Kernel Version 8.9.1: Thu Feb 22  
20:55:00 PST 2007; root:xnu-792.18.15~1/RELEASE_I386 i386 i386

The lines around the warning are:

   /* Forward declare the locally static matching functions we have  
generated and any predicate functions.
    */
   static void     mFOO    (pSimpleLexer ctx);
   static void     mTokens    (pSimpleLexer ctx);
   static void     SimpleLexerFree(pSimpleLexer ctx);

The actual function is implemented as follows:

   static ANTLR3_INLINE
   void mFOO(pSimpleLexer ctx)
   {
     ...

The warning can be eliminated in one of three ways:

- either dropping the ANTLR3_INLINE from the function implementation

- or adding ANTLR3_INLINE to the forward declaration

- or moving the mFOO function further up in the file so that it's not  
called prior to the implementation (in the case of the example  
grammar, it's called exactly once prior to the implementation, from  
the SimpleLexerNew function)

Of these, the cleanest solution looks to be adding ANTLR3_INLINE to  
the forward declaration, at least with GCC... I don't have access to  
any other compilers though so I don't know what the effect would be.  
I'm not aware of any switch that can be passed to GCC to suppress  
these warnings, but in a large lexer than tend to produce a fair bit  
of noise (two warnings for each lexer rule).

Cheers,
Wincent




More information about the antlr-interest mailing list