g++编译器无法识别lex的内置input()函数
g++ compiler is not recognizing inbuilt input() function of lex
代码在gcc编译器下运行良好。但是我需要使用g++
error: ‘input’ was not declared in this scope
while ((c = input()) != 0)
^
与yacc链接后发生错误
static void comment(void)
{
int c;
while ((c = input()) != 0)
if (c == '*')
{
while ((c = input()) == '*')
;
if (c == '/')
return;
if (c == 0)
break;
}
yyerror("unterminated comment");
}
如果您打算用c++编译flex生成的扫描器,那么您需要使用yyinput
而不是input
。在扫描器中,函数的名称取决于编译器是C还是c++,据称是为了避免名称冲突(尽管我不知道哪个版本的c++定义了名称input
):
#ifdef __cplusplus
static int yyinput (void );
#else
static int input (void );
#endif
此行为在flex手册中有记录:
(注意,如果扫描器是使用c++编译的,那么
input()
将被称为yyinput()
,以避免与名为input
的c++流发生名称冲突。)
我认为这是由于C和c++的混合。我有一段时间没有使用lex了,不记得你是否必须声明这些函数,或者它们是由include提供的,但你应该做的是将input
的声明包装在extern "C" {}
块中。
C模式下的Yacc不声明很多东西,您必须手动提供input()
的声明。
(提示:尝试bison/flex代替,我认为他们支持编译与c++编译器更好)
添加:
extern "C" int input();
相关文章:
- 内置函数可查看CPP中的成员变量
- 是否有内置方法可以强制转换为不同的基础类型,但保留常量限定符?
- 按字母顺序对字符串中的字母进行排序,而无需使用内置的 sort()
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 如何从 c++ 中类中内置的数组继承
- 如何捕获 C++ 内置异常对象
- macOS 是内置在 clang 编译器中还是内置于 xcode ide 中?
- 将编译器开关添加到 Eclipse CDT 内置编译器设置生成?
- gcc Atomic在gcc 4.1.1中内置了奇怪的行为
- 是否有用于元素部分移位的 simd 指令/内在/内置指令?
- 何时包含内置类型和运算符的标头?
- 基本类型与内置类型有什么区别C++
- 指内置类型的文字
- GLUT 问题:重新声明 c++ 内置类型'wchar_t'时出错
- 像自定义类一样构造的指针(内置类型)如何工作?
- 内置类型与用户定义的类型 C++
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- C++内置类型的基于类型的调度
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- g++编译器无法识别lex的内置input()函数