C Linux上的flex中存在冲突类型-Windows正常
我的flex文件的一些行:C Linux上的flex中存在冲突类型-Windows正常,c,linux,flex-lexer,C,Linux,Flex Lexer,我的flex文件的一些行: %{ #include <stdlib.h> #include <string.h> #include "types.h" #define NO_YY_UNPUT /* #define YY_NEVER_INTERACTIVE */ extern char *strdup(const char *); short unsigned int yylineno = 1; %} {ID} { yylv
%{
#include <stdlib.h>
#include <string.h>
#include "types.h"
#define NO_YY_UNPUT
/* #define YY_NEVER_INTERACTIVE */
extern char *strdup(const char *);
short unsigned int yylineno = 1;
%}
{ID} {
yylval.txt = strdup(yytext);
return ID;
};
\n { ++yylineno; }
第10行:extern char*strdup(const char*)代码>
第12行:short unsigned int yylineno=1
strdup
在string.h
中声明,但它是一个Posix接口,在包含任何系统头之前,您应该定义适当的功能测试宏:
%top {
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include "types.h"
}
%top{
#定义_POSIX_C_源200809L
#包括
#包括
#包括“types.h”
}
(注意:使用%top
强制将附带的代码插入到生成的C文件的顶部,以保证在任何系统头之前定义特性测试宏。)
我不知道这在Windows上是否有效(这当然取决于您的编译器和工具链),因此您可能需要在该平台上声明strdup
。如果是这样的话,请确保在声明中包含针对生成环境的预处理器测试。)
第10行的错误可能是由于strdup
被定义为宏导致的。我不确定在什么情况下会发生这种情况——这将是某种GNU扩展模式——但定义Posix feature test宏应该可以防止这种情况发生
对于类型为yylineno
的错误,有一个简单的解决方案:不要声明yylineno
。它是在flex生成的代码中声明的(如果您要求重新输入--“pure”-lexer,它可以声明为宏)。不声明yylineno
程序工作正常,也不声明与strdup
一致。。。这对我来说是最好的解决方案,但谢谢你的帮助!:)
%top {
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include "types.h"
}