【问题标题】:Configuring Bison and Flex without global or static variable在没有全局或静态变量的情况下配置 Bison 和 Flex
【发布时间】: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.htmlhttp://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


【解决方案1】:

首先,这是一个 C 可重入 flex 解析器和纯野牛示例,它解析符合以下内容的语法:

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

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

词法分析器.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
  • 将所有标记更改如下:LPARENyy::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 docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2022-01-28
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    相关资源
    最近更新 更多