1

我正在尝试用 c++14 中的 flex/bison 工具链编写我的玩具语言。

使用带有 flex reentrant 的 bison c++ 变体时我很困惑,yylex找不到参数yylval

我的开发环境是装有最新操作系统和 XCode 的 macbook,自制软件安装了最新的 flex 2.6.4 和 bison 3.7.1。

为方便起见,您可以在此处下载有错误的项目:https ://github.com/linrongbin16/tree 。

现在让我介绍这个不那么简单的tree项目:

首先让我们看看makefile

clean:
    rm *.o *.out *.yy.cc *.yy.hh *.tab.cc *.tab.hh *.output

tree.out: tree.o token.yy.o parser.tab.o
    clang++ -std=c++14 -o tree.out tree.o token.yy.o parser.tab.o

token.yy.cc token.yy.hh: token.l
    flex --debug -o token.yy.cc --header-file=token.yy.hh token.l

parser.tab.cc parser.tab.hh: parser.y
    bison --debug --verbose -Wcounterexamples -o parser.tab.cc --defines=parser.tab.hh parser.y

token.yy.o: token.yy.cc
    clang++ -std=c++14 -g -c token.yy.cc token.yy.hh

parser.tab.o: parser.tab.cc
    clang++ -std=c++14 -g -c parser.tab.cc parser.tab.hh

tree.o: tree.cpp parser.tab.hh token.yy.hh
    clang++ -std=c++14 -g -c tree.cpp

该应用程序是一个tree.out,它取决于 3 个组件:tree tokenparser.

树组件

tree.h定义了一个简单的抽象语法树类,由于我没有实现它,所以它只有一个虚拟析构函数:

#pragma once

struct Tree {
  virtual ~Tree() = default;
};

tree.cpp是函数,它从lexer 和 parsermain读取文件名并初始化它,然后进行解析:stdin

#include "parser.tab.hh"
#include "token.yy.hh"
#include <cstdio>
#include <cstdlib>

struct Scanner {
  yyscan_t yyscanner;
  FILE *fp;
  YY_BUFFER_STATE yyBufferState;

  Scanner(const char *fileName) {
    yylex_init_extra(this, &yyscanner);
    fp = std::fopen(fileName, "r");
    if (!fp) {
      printf("file %s cannot open!\n", fileName);
      exit(-1);
    }
    yyBufferState = yy_create_buffer(fp, YY_BUF_SIZE, yyscanner);
    yy_switch_to_buffer(yyBufferState, yyscanner);
    yyset_lineno(1, yyscanner);
  }

  virtual ~Scanner() {
    if (yyBufferState) {
      yy_delete_buffer(yyBufferState, yyscanner);
    }
    if (yyscanner) {
      yylex_destroy(yyscanner);
    }
    if (fp) {
      std::fclose(fp);
    }
  }
};

int main(int argc, char **argv) {
  if (argc != 2) {
    printf("missing file name!\n");
    return -1;
  }

  Scanner scanner(argv[1]);
  yy::parser parser(scanner.yyscanner);
  if (parser.parse() != 0) {
    printf("parsing failed!\n");
    return -1;
  }
  return 0;
}

重要的是,我使用 bison c++ 变体和 flex 可重入特性,我想让项目变得现代(使用 c++ 14)并且使用多线程安全。所以初始化的时候有点复杂。但是当项目扩大到一个大项目时,它是值得的。

词法分析器组件

token.l

%option noyywrap noinput nounput
%option nodefault
%option nounistd
%option reentrant

%{
#include <cstdio>
#include <cstring>
#include "parser.tab.hh"
%}

%%

"+"     { yylval->emplace<int>(yy::parser::token::PLUS); return yy::parser::token::PLUS; }
"-"     { yylval->emplace<int>(yy::parser::token::MINUS); return yy::parser::token::MINUS; }
"*"     { yylval->emplace<int>(yy::parser::token::TIMES); return yy::parser::token::TIMES; }
"/"     { yylval->emplace<int>(yy::parser::token::DIVIDE); return yy::parser::token::DIVIDE; }
"("     { yylval->emplace<int>(yy::parser::token::LPAREN); return yy::parser::token::LPAREN; }
")"     { yylval->emplace<int>(yy::parser::token::RPAREN); return yy::parser::token::RPAREN; }
";"     { yylval->emplace<int>(yy::parser::token::SEMICOLON); return yy::parser::token::SEMICOLON; }
"="     { yylval->emplace<int>(yy::parser::token::EQUAL); return yy::parser::token::EQUAL; }

[a-zA-Z][a-zA-Z0-9]+    { yylval->emplace<std::string>(yytext); return yy::parser::token::ID; }
[0-9]+                  { yylval->emplace<int>(atoi(yytext)); return yy::parser::token::NUM; }

%%

在这里,我遵循了野牛拆分符号手册(注意:这里我们得到了编译错误,我也尝试了make_XXXapi,这也给了我错误)。

它生成token.yy.cc token.yy.hh,期望编译一个token.yy.o对象。

解析器组件

parser.y

%require "3.2"
%language "c++"
%define api.value.type variant
%define api.token.constructor
%define parse.assert
%define parse.error verbose
%define parse.lac full
%locations
%param {yyscan_t yyscanner}

%code top {
#include <memory>
}

%code requires {
#include <memory>
#include "token.yy.hh"
#include "tree.h"
#define SP_NULL (std::shared<Tree>(nullptr))
}

%token<int> PLUS '+'
%token<int> MINUS '-'
%token<int> TIMES '*'
%token<int> DIVIDE '/'
%token<int> SEMICOLON ';'
%token<int> EQUAL '='
%token<int> LPAREN '('
%token<int> RPAREN ')'
%token<int> NUM

%token<std::string> ID

%type<std::shared_ptr<Tree>> prog assign expr literal

/* operator precedence */
%right EQUAL
%left PLUS MINUS
%left TIMES DIVIDE


%start prog

%%

prog : assign { $$ = SP_NULL; }
     | prog ';' assign { $$ = SP_NULL }
     ;

assign : ID '=' expr { $$ = SP_NULL; }
       | expr { $$ = $1; }
       ;

expr : literal { $$ = SP_NULL; }
     | expr '+' literal { $$ = SP_NULL; }
     | expr '-' literal { $$ = SP_NULL; }
     | expr '*' literal { $$ = SP_NULL; }
     | expr '/' literal { $$ = SP_NULL; }
     ;

literal : ID { $$ = SP_NULL; }
        | NUM { $$ = SP_NULL; }
        ;

%%

我按照bison c++ 变体手册,它生成parser.tab.cc parser.tab.hh parser.output,输出文件仅供分析。

由于 flex 是可重入的,我需要添加一个参数%param {yyscan_t yyscanner}

错误信息

这是使用时的错误消息make tree.out

bison --debug --verbose -Wcounterexamples -o parser.tab.cc --defines=parser.tab.hh parser.y
flex --debug -o token.yy.cc --header-file=token.yy.hh token.l
clang++ -std=c++14 -g -c tree.cpp
clang++ -std=c++14 -g -c token.yy.cc token.yy.hh
token.yy.cc:820:10: error: use of undeclared identifier 'yyin'; did you mean 'yyg'?
                if ( ! yyin )
                       ^~~~
                       yyg
token.yy.cc:807:23: note: 'yyg' declared here
    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
                      ^
token.yy.cc:822:4: error: use of undeclared identifier 'yyin'
                        yyin = stdin;
                        ^
token.yy.cc:827:10: error: use of undeclared identifier 'yyout'
                if ( ! yyout )
                       ^
token.yy.cc:829:4: error: use of undeclared identifier 'yyout'
                        yyout = stdout;
                        ^
token.yy.cc:837:23: error: use of undeclared identifier 'yyin'
                                yy_create_buffer( yyin, YY_BUF_SIZE , yyscanner);
                                                  ^
token.yy.cc:895:3: error: use of undeclared identifier 'YY_DO_BEFORE_ACTION'
                YY_DO_BEFORE_ACTION;
                ^
token.yy.cc:902:8: error: use of undeclared identifier 'yy_flex_debug'; did you mean 'yyget_debug'?
                if ( yy_flex_debug )
                     ^~~~~~~~~~~~~
                     yyget_debug
token.yy.cc:598:5: note: 'yyget_debug' declared here
int yyget_debug ( yyscan_t yyscanner );
    ^
token.yy.cc:908:45: error: use of undeclared identifier 'yytext'
                                         (long)yy_rule_linenum[yy_act], yytext );
                                                                        ^
token.yy.cc:911:14: error: use of undeclared identifier 'yytext'
                                         yytext );
                                         ^
token.l:12:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<int>(yy::parser::token::PLUS); return yy::parser::token::PLUS; }
  ^
token.l:13:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<int>(yy::parser::token::MINUS); return yy::parser::token::MINUS; }
  ^
token.l:14:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<int>(yy::parser::token::TIMES); return yy::parser::token::TIMES; }
  ^
token.l:15:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<int>(yy::parser::token::DIVIDE); return yy::parser::token::DIVIDE; }
  ^
token.l:16:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<int>(yy::parser::token::LPAREN); return yy::parser::token::LPAREN; }
  ^
token.l:17:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<int>(yy::parser::token::RPAREN); return yy::parser::token::RPAREN; }
  ^
token.l:18:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<int>(yy::parser::token::SEMICOLON); return yy::parser::token::SEMICOLON; }
  ^
token.l:19:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<int>(yy::parser::token::EQUAL); return yy::parser::token::EQUAL; }
  ^
token.l:21:3: error: use of undeclared identifier 'yylval'
{ yylval->emplace<std::string>(yytext); return yy::parser::token::ID; }
  ^
token.l:21:32: error: use of undeclared identifier 'yytext'
{ yylval->emplace<std::string>(yytext); return yy::parser::token::ID; }
                               ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [token.yy.o] Error 1

你能帮我解决这些问题吗?

4

1 回答 1

3

好吧,我再次阅读了野牛手册并自己解决了问题......

bison c++ 示例中,我们可以看到yylex声明被重新定义:

// Give Flex the prototype of yylex we want ...
# define YY_DECL \
  yy::parser::symbol_type yylex (driver& drv)
// ... and declare it for the parser's sake.
YY_DECL;

这就是为什么我们可以在 flex 规则中编写如下内容:

return yy::parser::make_MINUS  (loc);
于 2020-08-26T09:39:18.287 回答