在没有全局或静态变量的情况下配置Bison和Flex

Bru*_*tag 10 c++ multithreading bison flex-lexer

我在一个小语言/ IDE工作.我需要知道如何配置flex和bison以协同工作,但不使用任何全局或静态变量.我需要传递给我的AST指针.我也需要野牛通过我的AST来弯曲.这是一个线程环境,但我不需要任何线程同步.我需要为每个yyparse()调用一个separete 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
Run Code Online (Sandbox Code Playgroud)

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...
Run Code Online (Sandbox Code Playgroud)

我得到了这个:

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;
Run Code Online (Sandbox Code Playgroud)

//更多错误......

我还要将yylineno修复为每个文件...我基于http://benlynn.blogspot.com.br/2013/12/reentrant-parsers-with-flex-and-bison.htmlhttp:/ /www.lemoda.net/c/reentrant-parser/index.html

小智 16

首先,这是一个C reentrant flex解析器和纯野牛示例,它解析与以下内容匹配的语法:

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

()()(())()()
Run Code Online (Sandbox Code Playgroud)

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 */
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

建造

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
Run Code Online (Sandbox Code Playgroud)

注意: OSX的内置野牛已经过时,所以安装3.x:

brew install bison
Run Code Online (Sandbox Code Playgroud)

然后运行它 /usr/local/opt/bison/bin/bison ....

现在,迁移到C++

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

lexer.lxx

  • %option c++
  • 删除reentrant,bison-bridgebison-locations
  • 更改所有令牌,如下所示:LPARENtoyy::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++示例文档