【发布时间】:2014-04-02 03:59:03
【问题描述】:
我正在使用一种小型语言/IDE。而且我需要知道如何配置 flex 和 bison 以协同工作,但不使用任何全局或静态变量。我需要将我的 AST 指针传递给野牛。我还需要野牛通过我的 AST 来弯曲。这是一个线程环境,但我不需要任何线程同步。而且我需要为每个 yyparse() 调用一个单独的 yylineno 变量。我读到了 %define api.pure 、 %parse-param 和 %option reentrant。但我不知道如何让它们一起工作......提前谢谢......
我试过了:
scanner.l:
%{
#include <iostream>
#include <sstream>
#include "parser.tab.h"
#define YY_DECL extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner)
extern void yyerror(yyscan_t scanner, NBloco * bloco, const char *s);
%}
%option noyywrap
%option yylineno
%option reentrant
%option bison-bridge
%%
//...scanner code
parser.y:
%{
#include <iostream>
#include "AST.h"
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
extern "C" FILE *yyin;
extern int yylineno;
void yyerror(yyscan_t scanner, NBloco * bloco, const char *s);
%}
%union{
//union code
}
%define api.pure full
%lex-param { yyscan_t scanner }
%parse-param { yyscan_t scanner }
%parse-param { NBlock* block}
//tokens...
//types...
%%
//parser code...
我得到了这个:
parser.y:13:22: warning: 'yylex' initialized and declared 'extern' [enabled by default] extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
parser.y:13:22: error: 'YYSTYPE' was not declared in this scope
parser.y:13:32: error: 'lvalp' was not declared in this scope extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
parser.y:13:48: error: expected primary-expression before 'scanner'
extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
^
parser.y:13:55: error: expression list treated as compound expression in initializer [-fpermissive]
extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
^
parser.tab.c: In function 'int yyparse(yyscan_t, NBloco*)':
parser.tab.c:932:39: error: 'yylex' cannot be used as a function
# define YYLEX yylex (&yylval, scanner)
parser.tab.c:1618:16: note: in expansion of macro 'YYLEX'
yychar = YYLEX;
// 更多错误...
我还将 yylineno 修复为每个文件... 我基于http://benlynn.blogspot.com.br/2013/12/reentrant-parsers-with-flex-and-bison.html 和http://www.lemoda.net/c/reentrant-parser/index.html
【问题讨论】:
-
为什么不直接传递在
main()范围内创建的实例? -
我添加了它,但我还需要野牛将它传递给 flex...
-
也许,一些小代码示例可能有助于说明您的问题。我认为没有必要在这里发布完整的代码,只是相关部分,你特别有问题。
-
多个 yyparse 调用是否支持包含?那么您是否看过“多个输入缓冲区”flex.sourceforge.net/manual/…?还有为什么 AST 到 FLEX?您的扫描仪依赖于 AST?
-
没有。它不支持包括。这是一个带有多个选项卡的 IDE,我希望支持同时执行多个选项卡。好吧,flex 并不真的需要 AST...
标签: c++ multithreading bison flex-lexer