2014-10-29 23 views
1

因此,我正在製作一個使用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; 

%% 
+0

dp你實現main作爲bison/flex的一部分,還是與此分開? – Tanuki 2014-10-29 19:12:03

+0

分開。主要包含一堆opengl代碼,實際上。 – 2014-10-29 21:36:53

+0

然後你在bison/flex中主要有明顯的錯誤。如果我有時間,我會嘗試編寫示例C++實現。我真的建議你不用main和C++代碼來重寫它。 – Tanuki 2014-10-29 22:54:01

回答

2

適當的柔性/野牛建立在現代C++項目是一個重大的皮塔餅,作爲例子是質量較差和生成的代碼鼓勵過時的做法,如非新手代碼和全局變量。這在80年代,不是在2014年。

嘗試適應我幾個星期前做的例子。該設置非常簡單,因爲它將Flex詞法分析器和Bison分析器封裝在獨立的名稱空間中的單獨類(稱爲解釋器)中。它是可重入的,並與良好的舊C++ 03兼容。

https://github.com/ezaquarii/bison-flex-cpp-example

唯一棘手的部分是重構類名,以滿足您的需求。一旦編譯完成,你就可以開始處理* .y和* .l文件來實現你的有趣的東西,而不會對抗Bison/Flex安裝的特性。請享用!

0

我想出的解決方案是一起編譯文件。只需在其他cpp文件和任何其他變量中聲明yyparse()函數爲extern。我將在下面附加我的makefile文件

CFLAGS=-ll 
CC=g++ 
all:example2 
example2: main.cpp lex.yy.c bisoner.tab.c 
    $(CC) main.cpp lex.yy.c bisoner.tab.c Player.cpp Location.cpp Monopoly.cpp -o example2 -ll 
bisoner.tab.c:bisoner.y 
    bison -d bisoner.y 
lex.yy.c:lexer.l 
    lex lexer.l 

clean: 
    rm -rf *o example2