Как мне скомпилировать следующий flex-файл в C ++ со вспомогательными функциями - PullRequest
0 голосов
/ 24 февраля 2012

У меня есть следующий файл 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

Как мне исправить эти ошибки?

1 Ответ

1 голос
/ 24 февраля 2012

Вы должны объявить их в первую очередь. Изменить первый раздел:

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

#define AND 1
#define BEGINN 2

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

Кроме того, если вам нужны эти функции только в одном файле (что, вероятно, имеет место, если они не объявлены в заголовке), вам, вероятно, следует объявить их static или в анонимном пространстве имен, если вы ' использую C ++.

...