不使用全局或静态变量配置Bison和Flex

12

我在一款小型语言/IDE中工作。我需要知道如何配置flex和bison以使它们可以协同工作,并且不使用任何全局或静态变量。我需要将我的AST指针传递给bison。我还需要bison将我的AST传递给flex。这是一个线程环境,但我不需要任何线程同步。对于每个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

解析器.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.htmlhttp://www.lemoda.net/c/reentrant-parser/index.html


为什么不直接传递在 main() 作用域中创建的实例? - πάντα ῥεῖ
我已经添加了它,但我还需要将bison传递给flex... - Bruno Lebtag
也许,一些小的代码示例可以帮助说明你的问题。我认为没有必要在这里发布完整的代码,只需发布你特别遇到问题的相关部分即可。 - πάντα ῥεῖ
1
需要多个yyparse调用来支持包含吗?那你看过“多输入缓冲区”了吗?http://flex.sourceforge.net/manual/Multiple-Input-Buffers.html#Multiple-Input-Buffers。另外,为什么要使用AST到FLEX?您的扫描器依赖于AST吗? - jbp
不,它不支持包含文件。它是一个带有多个选项卡的IDE,我想要支持同时执行多个选项卡。而且,Flex并不真正需要AST... - Bruno Lebtag
1个回答

21

首先,这是一个C语言可重入的flex解析器和纯bison示例,用于解析符合以下语法的文本:

()
(())
(()())

()()(())()()

lexer.l

%option bison-bridge
%option bison-locations
%option ecs
%option nodefault
%option noyywrap
%option reentrant
%option stack
%option warn
%option yylineno

%{
  #include "parser.h"
%}

%%

"(" { return (LPAREN); }
")" { return (RPAREN); }

[ \f\r\t\v\n]+ /* eat whitespace */

%%

/* don't use lexer.l for code, organize it logically elsewhere */

parser.y

%define parse.error verbose
%define api.pure true
%locations
%token-table
%glr-parser
%lex-param {void *scanner}
%parse-param {void *scanner}

%{
/* your top code here */
%}

%union {
  int value; // or whatever else here
}

%token LPAREN
%token RPAREN

%%

document
    : exprs

exprs
    : %empty
    | expr exprs

expr
    : parens

parens
    : LPAREN exprs RPAREN


%%

int
yyerror(YYLTYPE *locp, char *msg) {
  if (locp) {
    fprintf(stderr, "parse error: %s (:%d.%d -> :%d.%d)\n",
                    msg,
                    locp->first_line, locp->first_column,
                    locp->last_line,  locp->last_column
    );
    /* todo: add some fancy ^^^^^ error handling here */
  } else {
    fprintf(stderr, "parse error: %s\n", msg);
  }
  return (0);
}

main.c

#include "parser.h"
#include "lexer.h"

int
main(int argc, char **argv) {
  int result;
  yyscan_t scanner;

  yylex_init(&scanner);
  result = (yyparse(scanner));
  yylex_destroy(scanner);
  return (result);
}

构建

flex --header-file=lexer.h --outfile=lexer.c lexer.l
bison --output-file=parser.c --defines=parser.h --warnings=all --feature=all parser.y
cc lexer.c parser.c main.c -o parser
./parser

注意: OSX 自带的 bison 已经过时,因此需要安装3.x版本:

brew install bison

然后像这样运行:/usr/local/opt/bison/bin/bison ....

现在,迁移到C++

  • 将.l复制为.lxx并将.y复制为.yxx
  • 更改输出文件的名称为*.cxx和*.hxx。

lexer.lxx

  • 添加%option c++
  • 删除reentrantbison-bridgebison-locations
  • 将所有类似下面的标记更改为:yy::parser::token::LPAREN

parser.yxx

  • 添加%skeleton "lalr1.cc"
  • 删除api.pure
  • 删除yyerror

main.cxx

  • 重写为C++

将词法分析器和解析器对象连接起来是读者的练习。

另请参阅:https://github.com/bingmann/flex-bison-cpp-example,但请注意它使用旧的bison 2.x接口。

GNU Bison 3.x C++ Example文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接