title: javacc C grammar and C "Bit fields" ; ParseException
body: <p>I'm trying to use this javacc grammar  <a href="https://java.net/downloads/javacc/contrib/grammars/C.jj" rel="nofollow">https://java.net/downloads/javacc/contrib/grammars/C.jj</a> to parse a C code containing <a href="http://en.wikipedia.org/wiki/Bit_field" rel="nofollow">bit fields</a></p>
  
  <pre><code>struct T{
      int w:2;
      };
  struct T a;
  </code></pre>
  
  <p>The generated parser cannot parse this code:</p>
  
  <pre><code>$ javacc -DEBUG_PARSER=true C.jj &amp;&amp; javac CParser.java &amp;&amp; gcc -E input.c | java CParser 
  Java Compiler Compiler Version 5.0 (Parser Generator)
  (type "javacc" with no arguments for help)
  Reading from file C.jj . . .
  File "TokenMgrError.java" is being rebuilt.
  File "ParseException.java" is being rebuilt.
  File "Token.java" is being rebuilt.
  File "SimpleCharStream.java" is being rebuilt.
  Parser generated successfully.
  Note: CParser.java uses unchecked or unsafe operations.
  Note: Recompile with -Xlint:unchecked for details.
  C Parser Version 0.1Alpha:  Reading from standard input . . .
  Call:   TranslationUnit
    Call:   ExternalDeclaration
      Call:   Declaration
        Call:   DeclarationSpecifiers
          Call:   TypeSpecifier
            Call:   StructOrUnionSpecifier
              Call:   StructOrUnion
                Consumed token: &lt;"struct" at line 5 column 1&gt;
              Return: StructOrUnion
              Consumed token: &lt;&lt;IDENTIFIER&gt;: "T" at line 5 column 8&gt;
              Consumed token: &lt;"{" at line 5 column 9&gt;
              Call:   StructDeclarationList
                Call:   StructDeclaration
                  Call:   SpecifierQualifierList
                    Call:   TypeSpecifier
                      Consumed token: &lt;"int" at line 6 column 2&gt;
                    Return: TypeSpecifier
                  Return: SpecifierQualifierList
                  Call:   StructDeclaratorList
                    Call:   StructDeclarator
                      Call:   Declarator
                        Call:   DirectDeclarator
                          Consumed token: &lt;&lt;IDENTIFIER&gt;: "w" at line 6 column 6&gt;
                        Return: DirectDeclarator
                      Return: Declarator
                    Return: StructDeclarator
                  Return: StructDeclaratorList
                Return: StructDeclaration
              Return: StructDeclarationList
            Return: StructOrUnionSpecifier
          Return: TypeSpecifier
        Return: DeclarationSpecifiers
      Return: Declaration
    Return: ExternalDeclaration
  Return: TranslationUnit
  C Parser Version 0.1Alpha:  Encountered errors during parse.
  ParseException: Encountered " ":" ": "" at line 6, column 7.
  Was expecting one of:
      ";" ...
      "," ...
      "(" ...
      "[" ...
      "[" ...
      "(" ...
      "(" ...
      "," ...
      ";" ...
      ";" ...
      ";" ...
      "[" ...
      "(" ...
      "(" ...
  
      at CParser.generateParseException(CParser.java:4279)
      at CParser.jj_consume_token(CParser.java:4154)
      at CParser.StructDeclaration(CParser.java:433)
      at CParser.StructDeclarationList(CParser.java:372)
      at CParser.StructOrUnionSpecifier(CParser.java:328)
      at CParser.TypeSpecifier(CParser.java:274)
      at CParser.DeclarationSpecifiers(CParser.java:182)
      at CParser.Declaration(CParser.java:129)
      at CParser.ExternalDeclaration(CParser.java:96)
      at CParser.TranslationUnit(CParser.java:77)
      at CParser.main(CParser.java:63)
  </code></pre>
  
  <p>I tried to change (line 245)</p>
  
  <pre><code>(...) LOOKAHEAD( { isType(getToken(1).image) } )TypedefName() )
  </code></pre>
  
  <p>to </p>
  
  <pre><code> LOOKAHEAD( { isType(getToken(1).image) } ) TypedefName2()
  
  (...)
  
  void TypedefName2() : {}
   {
      TypedefName()   (LOOKAHEAD(2) ":" &lt;INTEGER_LITERAL&gt; )?
    }
  </code></pre>
  
  <p>but it doesn't work (same error) .</p>
  
  <p>Is there a simple way  to fix the javaCC grammar to handle Bit Fields ?</p>