2014-06-17 23 views
0

我一直在翻譯我的分析器/詞法分析器到一個可重入的實現,最後的障礙是能夠嵌套include script.txt類型的指令。我該如何做一個純(可重入)flex/bison分析器嵌套包含?

這就是我爲include位實現:但賽格故障,標記生成器的,否則休息和解析器正常工作:

^{include}{ws}+  { BEGIN INCL;    } 
<INCL>[^ \t\n\r\f]+ { /* Swallow everything up to whitespace or an EOL character. 
         * When state returns to initial, the whitepsace 
         * and/or EOL will be taken care of. */ 
         { 
         FILE * _yyin; 
         _yyin = fopen (yytext, "r"); 
         if (! _yyin) { 
          char buf[256]; 
          snprintf(buf, DIM(buf) - 1,"Script include file ""%s"" couldn't be opened: %s.",yytext,strerror(errno)); 
          yyerror(yyscanner, "buf"); 
         } else { 
          extern int yyparse(yyscan_t * scanner); 
          yyscan_t incl_scanner; 
          struct my_parser_data incl_data; 

          yylex_init(& incl_scanner); 
          yylex_init_extra(& incl_data, & incl_scanner); 
          yyset_in(_yyin, & incl_scanner); 

          /* This call causes a segmentation fault. */ 
          yyparse(& incl_scanner); 

          yylex_destroy(& incl_scanner); 
         } 
         } 
         BEGIN 0; 
        } 

我怎麼會去實現嵌套包括?

回答

1

你不叫yylex_inityylex_init_extra。你打電話給另一個人;如果您需要額外的數據,請致電yylex_init_extra而不是yylex_init

但是,這不是什麼產生段錯誤。對yylex_init的雙重調用是內存泄漏,而不是段錯誤。

該段錯誤來自路過的incl_scanner地址yyset_inyyparseyylex_destroy。這些函數期望的值爲的參數(它是一個指針)。

下面是來自flex manual一個相關的例子:

  yylex_init_extra(buf, &scanner); 
     yyset_in(in, scanner); 
     yylex(scanner); 
     yylex_destroy(scanner); 

注意哪些功能需要&scanner且只取scanner