Я следовал учебнику Flex and Bison , чтобы изучать flex & bison, но я застрял.Когда я компилирую с помощью "g ++ snazzle.tab.c lex.yy.c -lfl -o snazzle", я получаю следующие сообщения об ошибках:
snazzle.tab.c: In function ‘int yyparse()’:
snazzle.tab.c:1403: warning: deprecated conversion from string constant to ‘char*’
snazzle.tab.c:1546: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccFyBCBm.o: In function `yyparse':
snazzle.tab.c:(.text+0x1e0): undefined reference to `yylex'
collect2: ld returned 1 exit status
мой env is ubuntu 10.04 bison 2.4.1 flex 2.5.35
Я до сих пор не могу найти проблему.сначала я компилирую с помощью "bison -o snazzle.y", затем он генерирует два snazzle.tab.c и snazzle.tab.h соответственно.наконец, я компилирую с помощью g ++ snazzle.tab.c lex.yy.c -lfl -o snazzle.мой код как следующий:
snazzle.l
%{
#include <cstdio>
#include <iostream>
using namespace std;
#include "snazzle.tab.h"
%}
%%
[ \t] ;
[0-9]+\.[0-9]+ { yylval.fval = atof(yytext); return FLOAT; }
[0-9]+ { yylval.ival = atoi(yytext); return INT; }
[a-zA-Z0-9]+ {
// we have to copy because we can't rely on yytext not changing underneath us:
yylval.sval = strdup(yytext);
return STRING;
}
. ;
%%
snazzle.y
%{
#include <cstdio>
#include <iostream>
using namespace std;
extern "C" int yylex();
extern "C" int yyparse();
extern "C" FILE *yyin;
void yyerror(char *s);
%}
%union {
int ival;
float fval;
char *sval;
}
%token <ival> INT
%token <fval> FLOAT
%token <sval> STRING
%%
snazzle:
INT snazzle { cout << "bison found an int: " << $1 << endl; }
| FLOAT snazzle { cout << "bison found a float: " << $1 << endl; }
| STRING snazzle { cout << "bison found a string: " << $1 << endl; }
| INT { cout << "bison found an int: " << $1 << endl; }
| FLOAT { cout << "bison found a float: " << $1 << endl; }
| STRING { cout << "bison found a string: " << $1 << endl; }
;
%%
main() {
// open a file handle to a particular file:
FILE *myfile = fopen("a.snazzle.file", "r");
// make sure it is valid:
if (!myfile) {
cout << "I can't open a.snazzle.file!" << endl;
return -1;
}
// set flex to read from it instead of defaulting to STDIN:
yyin = myfile;
// parse through the input until there is no more:
do {
yyparse();
} while (!feof(yyin));
}
void yyerror(char *s) {
cout << "EEK, parse error! Message: " << s << endl;
// might as well halt now:
exit(-1);
}