gpt4 book ai didi

c++ - 在 Bison 和 Flex 中使用变体

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:02:45 25 4
gpt4 key购买 nike

我一直在将解析器从 c 重写为 c++,因此我尝试在我的代码中使用 variant。但是,我不确定如何将它与 flex 集成,而且我不断收到深奥的错误消息。

我的 Bison 文件看起来像

%require "3"
%language "c++"

%{
// declarations
%}

%define api.value.type {std::variant<double, std::string>}

%token COMMENT
%token <double> DOUBLE
%token <std::string> STRING

// grammar

我的词法分析器看起来像

%{
#include "y.tab.h"
%}
%option noyywrap

ID [a-zA-Z][a-zA-Z0-9_]*


%%
[ \t\n ]+ ;

\-?[0-9]+ |
\-?[0-9]+\. |
\-?[0-9]+\.[0-9]+ |
\-?\.[0-9]+ { yylval.emplace<double>(std::atof(yytext)); return DOUBLE;}
// other tokens
zA-Z][\.a-zA-Z0-9_]* { yylval.emplace<std::string>(yytext); return STRING;}
%%

我不确定我对 yylval 的使用情况,我正在尝试访问变体,就像使用 %union 时一样。 .

我收到以下错误:

y.tab.h:125:18: error: ‘variant’ in namespace ‘std’ does not name a template type
typedef std::variant<double, std::string> semantic_type;
^~~~~~~
y.tab.h:197:27: error: ‘semantic_type’ does not name a type
const semantic_type& v);
^~~~~~~~~~~~~
y.tab.h:212:7: error: ‘semantic_type’ does not name a type
semantic_type value;
^~~~~~~~~~~~~
my_mdl.l: In function ‘int yylex()’:
my_mdl.l:16:3: error: ‘yylval’ was not declared in this scope
\-?\.[0-9]+ { yylval.emplace<double>(std::atof(yytext)); return DOUBLE;}
^~~~~~
my_mdl.l:16:3: note: suggested alternative: ‘yylex’
\-?\.[0-9]+ { yylval.emplace<double>(std::atof(yytext)); return DOUBLE;}
^~~~~~
yylex
my_mdl.l:16:18: error: expected primary-expression before ‘double’
\-?\.[0-9]+ { yylval.emplace<double>(std::atof(yytext)); return DOUBLE;}
^~~~~~
my_mdl.l:16:53: error: ‘DOUBLE’ was not declared in this scope
\-?\.[0-9]+ { yylval.emplace<double>(std::atof(yytext)); return DOUBLE;}
^~~~~~
my_mdl.l:18:10: error: ‘COMMENT’ was not declared in this scope
"//".* { return COMMENT;}
^~~~~~~
my_mdl.l:37:29: error: expected primary-expression before ‘>’ token
[a-zA-Z][\.a-zA-Z0-9_]* { yylval.emplace<std::string>(yytext); return STRING;}
^
my_mdl.l:37:47: error: ‘STRING’ was not declared in this scope
[a-zA-Z][\.a-zA-Z0-9_]* { yylval.emplace<std::string>(yytext); return STRING;}
^~~~~~

我的 .y 也有几百行错误文件如

my_mdl.y:88:79: error: no matching function for call to ‘MOVE::MOVE(<brace-enclosed initializer list>)’
p.add_command(Command{in_place_index<5>, MOVE( {{$2, $3, $4}}, $5)});
^
In file included from parsing/symt.h:7:0,
from my_mdl.y:10:
parsing/cmd.h:44:5: note: candidate: MOVE::MOVE(const Scalable<double, 3>&, const string&)
MOVE(const Scalable<double, 3> &params, const std::string &scaleFactorName);
^~~~

MOVE是定义为

的结构
struct MOVE {
MOVE(const Scalable<double, 3> &params, const std::string &scaleFactorName);

Scalable<double, 3> params; // todo equationify
std::string scale_factor_name;
};

它是变体 ( std::variant<MOVE, etc...> Command ) 中的一种类型。奇怪的是,如果我写,这在我的代码中正常工作 p.add_command(Command{in_place_index<5>, MOVE{{{x, y, z}}, "asdfads"}});

最佳答案

您的程序中包含的内容不足,无法给出准确的答案。请参阅有关准备 [mcse] 的 SO 帮助页面。但是您似乎很可能会收到错误

y.tab.h:125:18: error: ‘variant’ in namespace ‘std’ does not name a template type

因为你还没有安排#include <variant>在您的 flex 文件中。

typedef 本身来自bison 生成的头文件中生成的代码,但bison 猜不出是什么#include它可能需要的指令,因此由您来插入它们。您必须确保在您 #include 之前定义了您的语义类型所需的所有类型。 Bison 生成的 header 。您可以插入适当的 #include flex 文件序言 block 中的指令,或者您可以使用 %code requires 阻止你的 Bison 文件。 (由于您使用的是 bison 3,后者可能是最佳解决方案。)

我不知道是什么SAVE表示您的 Bison 文件中的错误。我假设它是您拥有(或尚未定义)的宏,因此错误将是宏展开的结果。

关于c++ - 在 Bison 和 Flex 中使用变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56552396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com