因此,我正在製作一個使用f lex和bison解析器來讀取配置文件的巨大程序。你如何獲得一個標題作爲flex + bison解析器的輸出?
我需要在我的main.cpp程序
分析器的輸出使用
%output "config.cc"
%defines "config.h"
只是增加了「身份不明的字符串」錯誤。其他選項也沒有太好。任何想法爲什麼這不起作用?
這裏是我的詞法分析器和野牛文件
%{
#include <stdio.h>
#include <string>
#include <map>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "Monopoly.h"
using namespace std;
extern int yylex();
extern void yyerror(char*);
Monopoly game;
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
%}
%token LTOKEN INTEGER LOCATIONWORD WORD GTOKEN CURRENCYTOKEN JAILFINETOKEN STARTINGMONEYTOKEN RTOKEN COSTTOKEN RENTTOKEN FILEPATHTOKEN OBJEXTENSION MODELTOKEN TAXTOKEN PERCENTSIGN
%union{
std::string *str;
int number;
}
%token <number> INTEGER
%token <str> WORD
%%
commands: /* empty */
| command commands
;
command:
currency_set
|
location_set
|
startingMoney_set
|
jailFine_set
|
route_add
|
cost_set
|
rent_set
|
filepath_found
|
tax_set
;
filepath_found:MODELTOKEN LTOKEN INTEGER FILEPATHTOKEN WORD OBJEXTENSION
{
printf(" File %s HAS BEEN IMPORTED for location number %d \n",$5,$3);
}
rent_set:RENTTOKEN LTOKEN INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER
{
printf("rent for house no 4 is %d \n",$7);
}
cost_set:COSTTOKEN LTOKEN INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER
{
printf("prices set to %d\n",$10);
}
currency_set:CURRENCYTOKEN WORD
{
game.currency=($2);
printf("Currency set to %s",game.currency->c_str());
}
location_set:
LOCATIONWORD LTOKEN INTEGER WORD GTOKEN INTEGER
{
printf("location number %d set to %s in froup number %d",$3,$4,$6);
}
startingMoney_set:
STARTINGMONEYTOKEN INTEGER
{
printf("starting money set to %d \n",$2);
}
jailFine_set:
JAILFINETOKEN INTEGER
{
printf("jailfine set to %d\n",$2);
}
route_add:
RTOKEN LTOKEN INTEGER LTOKEN INTEGER
{
printf("Route set up between location no %d and %d\n",$3,$5);
}
tax_set:
TAXTOKEN INTEGER PERCENTSIGN INTEGER
{
printf("tax set to %d percent\n",$2);
}
%%
int yywrap()
{
return 1;
}
extern FILE * yyin;
int main()
{
yyin=fopen("config.txt","r");
yyparse();
string json="{ \"hello\" : \"world\"} ";
rapidjson::Document d;
d.Parse<0>(json.c_str());
printf("%s\n", d["hello"].GetString());
return 1;
}
%{
#include <stdio.h>
#include <string>
#include "bisoner.tab.h"
using namespace std;
%}
%%
"Tax" return TAXTOKEN;
"rent" return RENTTOKEN;
"cost" return COSTTOKEN;
"route" return RTOKEN;
"Currency" return CURRENCYTOKEN;
"StartingMoney" return STARTINGMONEYTOKEN;
"JailFine" return JAILFINETOKEN;
"location" return LOCATIONWORD;
"@l" return LTOKEN;
"@g" return GTOKEN;
"model" return MODELTOKEN;
"#"[a-zA-Z0-9.-_= ]+ printf("comment ignored\n");
[0-9]+ yylval.number=atoi(yytext);return INTEGER;
[a-zA-Z]+ yylval.str=new string(yytext);return WORD;
"./"([[a-zA-Z0-9]+"/"]*)? return FILEPATHTOKEN;
".obj" return OBJEXTENSION;
"%" return PERCENTSIGN;
%%
dp你實現main作爲bison/flex的一部分,還是與此分開? – Tanuki 2014-10-29 19:12:03
分開。主要包含一堆opengl代碼,實際上。 – 2014-10-29 21:36:53
然後你在bison/flex中主要有明顯的錯誤。如果我有時間,我會嘗試編寫示例C++實現。我真的建議你不用main和C++代碼來重寫它。 – Tanuki 2014-10-29 22:54:01