将AST从flex+bison输出到main.cpp [英] Output AST from flex+bison to main.cpp

查看:0
本文介绍了将AST从flex+bison输出到main.cpp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:尽管我已经完成了本教程:http://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html

,但我只是个新手

现在,我正在为OpenGL-C++中的一个项目构建一个视频游戏。Cpp包含所有的游戏图形、逻辑等(相当容易管理,所以不是问题)。在游戏开始之前,它需要解析一个配置文件(让我们假设它是任意格式,所以INI和JSON API是不成问题的)。

我了解足够多的flex和bison来识别文件中的模式并创建AST(还使用$符号赋值变量)。现在,如何使这些变量在main.cpp中可用?

推荐答案

根据您的配置语言的复杂性,您可能更适合使用一遍解析器,而不是创建一个AST然后遍历树。但这两种方法都是完全有效的。

您可能应该花几分钟(或小时:)阅读bison manual。在这里,我将只关注您可能使用的一般方法和bison功能。

最重要的一点是向解析器传递额外参数的能力。具体地说,您需要将引用或指针传递给一个对象,该对象将包含解析的配置。您需要额外的输出参数,因为解析器本身将只返回成功或失败指示(您也需要)。

这里有一个简单的例子,它只构造了一个从名字到字符串的字典。请注意,与您提到的教程的作者不同,我更喜欢将扫描器和解析器编译为C++,从而避免了extern "C"接口的需要。这在flexbison的当前版本中工作得很好,只要您不尝试将非POD对象放到解析器堆栈上。遗憾的是,这意味着我们不能直接使用std::字符串;我们需要使用指针(我们也不能使用智能指针)。

文件scanner.l

%{
  #include <string>
  #include "config.h"
  using std::string;
%}

%option noinput nounput noyywrap nodefault
%option yylineno
 // Set the output file to a C++ file. This could also be done on the
 // command-line
%option outfile="scanner.cc"

%%

"#".*                      ; /* Ignore comments */
[[:space:]]                ; /* Ignore whitespace */
[[:alpha:]̣_][[:alnum:]_]*  { yylval = new string(yytext, yyleng); return ID; }
[[:alnum:]_@]+             { yylval = new string(yytext, yyleng); return STRING; }
["][^"]*["]                { yylval = new string(yytext+1, yyleng-2); return STRING; }
.                          { return *yytext; }
现在是bison文件,它只识别分配。这需要bison v3;需要进行细微调整才能与bison v2.7一起使用。

config.y

%code requires {
  #include <map>
  #include <string>
  #include <cstdio>
  using Config = std::map<std::string, std::string>;

  // The semantic type is a pointer to a std::string
  #define YYSTYPE std::string*

  // Forward declarations
  extern FILE* yyin;
  extern int yylineno; 
  int yylex();
  // Since we've defined an additional parse parameter, it will also
  // be passed to yyerror. So we need to adjust the prototype accordingly.
  void yyerror(Config&, const char*);
}

 // Set the generated code filenames. As with the flex file, this is
 // probably
 // better done on the command line.
%output "config.cc"
%defines "config.h"

 // The parser takes an additional argument, which is a reference to the
 // dictionary
 // which will be returned.
%parse-param { Config& config }

%token ID STRING

 // If semantic values are popped off the stack as the result of error
 // recovery,
 // they will leak, so we need to clean up.
%destructor { delete $$; } ID STRING

%%

config: %empty
      | config assignment
      ;

assignment: ID '=' STRING { config[*$1] = *$3;
                            delete $1; delete $3;
                          }
          | ID '=' ID     { config[*$1] = config[*$3];
                            delete $1; delete $3;
                          } 

%%
// The driver would normally go into a separate file. I've put it here
// for simplicity.

#include <iostream>
#include <cstring>

void yyerror(Config& unused, const char* msg) {
  std::cerr << msg << " at line " << yylineno << '
';
}

int main(int argc, const char** argv) {
  if (argc > 1) {
    yyin = fopen(argv[1], "r");
    if (!yyin) {
      std::cerr << "Unable to open " << argv[1] << ": "
                << strerror(errno) << '
';
      return 1;
    }
  } else {
    yyin = stdin;
  }
  Config config;
  int rv = yyparse(config);
  if (rv == 0)
    for (const auto& kv : config)
      std::cout << kv.first << ": "" << kv.second << ""
";
  return rv;
}

编译:

flex scanner.l
bison config.y
g++ --std=c++11 -Wall config.cc scanner.cc

试用:

$ cat sample.config
a=17
b= @a_single_token@
c = "A quoted string"
d9 =
"Another quoted string"
$ ./config sample.config
a: "17"
b: "@a_single_token@"
c: "A quoted string"
d9: "Another quoted string"

这篇关于将AST从flex+bison输出到main.cpp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆