如何在 C++ 中编译以下 Flex 文件具有辅助功能
我有以下 lex.l 文件。
%{
#include <stdio.h>
#include <stdlib.h>
#define AND 1
#define BEGINN 2
&}
/* regular definitions */
ws [ \t\n]+
letter [A-Za-z]
/* more declarations */
%%
{ws}
{id} {yylval = (int) storeLexeme(); return(ID);}
{num} {yylval = (int) storeInt(); return(NUM);}
/* more rules */
%%
int storeLexeme() {
/* function implementation */
}
int storeInt() {
/* function implementation */
}
我用 flex 运行这个文件,它用 gcc 编译,但用 g++ 报告以下错误。
lex.l:110: error: `storeLexeme' undeclared (first use this function)
lex.l:110: error: (Each undeclared identifier is reported only once for each function
it appears in.)
lex.l:111: error: `storeInt' undeclared (first use this function)
lex.l: In function `int storeLexeme()':
lex.l:117: error: `int storeLexeme()' used prior to declaration
lex.l: In function `int storeInt()':
lex.l:121: error: `int storeInt()' used prior to declaration
我该如何解决这些错误?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您必须先声明它们。更改第一部分:
此外,如果您只需要在该一个文件中使用这些函数(如果它们未在标头中声明,则可能是这种情况),您可能应该将它们声明为
static
,或者如果您使用的是 C++,则位于匿名命名空间中。You have to declare them first. Change the first section:
Also, if you only need these functions in that one file (which is probably the case if they're not declared in a header), you should probably be declaring them
static
, or in an anonymous namespace if you're using C++.