11

When I compile my .y file with bison parser.y -d -t and then include the parser.tab.h file in my flex file, gcc says "error: syntax error before numeric constant." It's referencing line 32, which is the first line in the enum of yytokentype.

enum yytokentype {
   BREAK = 258,
   ... }

The error is about the line "BREAK = 258." I honestly don't know why this is happening--I would really like to use the generated yylval and I need it from this header file. Even if I declared yytokentype like this in my flex file, I would get the same error. Anything I could be doing wrong?

Lesmana
  • 25,663
  • 9
  • 82
  • 87
Kizaru
  • 2,443
  • 3
  • 24
  • 39
  • 2
    I'm guessing there is a redefinition of `BREAK` somewhere above the errored line. The `-E` flag to gcc is good for ferreting these problems out. – msw Aug 13 '10 at 02:25

2 Answers2

20

Is BREAK defined somewhere else in your code? I get a similar error from the following toy example:

#define BREAK 10
enum yytokentype {
    BREAK = 258
};

Build example:

$ cc -c file.c 
file.c:4: error: expected identifier before numeric constant
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Hmm? That example will fail with or without the `#define` because of the semicolon. –  Aug 13 '10 at 02:28
  • Yep... That's the issue. I checked all of my #includes and found it in one of them. Thanks for the help to both of you. – Kizaru Aug 13 '10 at 02:30
  • @Kinopiko, oops, should have copy/pasted instead of retyping. Fixed now. – Carl Norum Aug 13 '10 at 02:31
6

Presumably BREAK is already defined somewhere in the flex output file, so after the preprocessor runs you are getting a statement like 99 = 258 or something. Try looking at the output of cpp yy.lex.c or gcc -E yy.lex.c. I looked at a flex output file but did not find BREAK anywhere in it, only YY_BREAK.