问题
I have the following problem while compiling the files. I have overwritten the definition of YYLTYPE as follows(though it is the same as default but I will extend it
typedef struct YYLTYPE
{
int first_line;
int first_column;
int last_line;
int last_column;
} YYLTYPE;
and when I add the following in the lex file I get the "yylloc undefined in this scope" error.
#define YY_USER_INIT yylloc.first_line = yylloc.first_column = 1;
Pastebin:
- flex file
- bison file
- makefile
回答1:
You need to put the definition of YYLTYPE
and YYLTYPE_IS_DECLARED
into a header file that you #include
in both your .y
and .l
files, and you need to #include
the .tab.h
file in your .l
file AFTER the #include
of the file that defines YYLTYPE
.
The reason for the above is that bison DOES NOT export your definition of YYLTYPE
from the top of your .y
file, so if you want it somewhere else, you need to arrange that it be available. Worse, the .tab.h
file will always have the default YYLTYPE
(guarded by #ifndef YYLTYPE_IS_DECLARED
) so you need to ensure that your definition is seen before it.
来源:https://stackoverflow.com/questions/10386567/yylloc-undefined-in-this-scope