如何在C++中使用辅助函数编译以下flex文件



我有以下lex.l文件。

 %{
 #include <stdio.h>
 #include <stdlib.h>
 #define AND 1
 #define BEGINN 2
 &}
 /* regular definitions */
 ws     [ tn]+
 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

如何解决这些错误?

您必须首先声明它们。更改第一部分:

%{
#include <stdio.h>
#include <stdlib.h>
#define AND 1
#define BEGINN 2
int storeLexeme(void);
int storeInt(void);
%}

此外,如果您只需要在一个文件中使用这些函数(如果它们没有在头中声明,则可能是这种情况),那么您可能应该在static中声明它们,或者如果您使用C++,则在匿名命名空间中声明它们。

最新更新