如何在 C++ 中编译以下 Flex 文件具有辅助功能

发布于 01-08 06:40 字数 1031 浏览 2 评论 0 原文

我有以下 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

我该如何解决这些错误?

I have the following lex.l file.

 %{
 #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 */
 }

I run this file with flex, and it compiles with gcc but reports the following errors with 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

How do I resolve these errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2025-01-15 06:40:19

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

%{
#include <stdio.h>
#include <stdlib.h>

#define AND 1
#define BEGINN 2

int storeLexeme(void);
int storeInt(void);
%}

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

You have to declare them first. Change the first section:

%{
#include <stdio.h>
#include <stdlib.h>

#define AND 1
#define BEGINN 2

int storeLexeme(void);
int storeInt(void);
%}

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++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文