首页 > 解决方案 > 为什么`string``vector`等在野牛的联合中不起作用?

问题描述

我试图为 C 语言的一个子集编写一个解析器。但是当我包含stringvector在里面时%union,它会抛出错误。使用char*orarray 似乎没问题。

%union{
    string s;
    vector<int>v;
}

我在某些地方看到它与标题问题有关,他们建议在其中包含此

%code requires{
    #include <string>
}

对于字符串大小写。但这对我也不起作用。

我得到的错误是

y.tab.c:1174:9: error: use of deleted function ‘YYSTYPE::YYSTYPE()’
 YYSTYPE yylval;
         ^~~~~~
y.tab.c:223:7: note: ‘YYSTYPE::YYSTYPE()’ is implicitly deleted because the default definition would be ill-formed:
 union YYSTYPE
       ^~~~~~~
demo.y:48:12: error: union member ‘YYSTYPE::s’ with non-trivial ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
     string s;
            ^
demo.y:49:16: error: union member ‘YYSTYPE::v’ with non-trivial ‘std::vector<_Tp, _Alloc>::vector() [with _Tp = int; _Alloc = std::allocator<int>]’
     vector<int>v;
                ^
y.tab.c: In function ‘int yyparse()’:
y.tab.c:1203:30: error: use of deleted function ‘YYSTYPE::YYSTYPE()’
     YYSTYPE yyvsa[YYINITDEPTH];
                              ^
y.tab.c:1203:30: error: use of deleted function ‘YYSTYPE::~YYSTYPE()’
y.tab.c:223:7: note: ‘YYSTYPE::~YYSTYPE()’ is implicitly deleted because the default definition would be ill-formed:
 union YYSTYPE
       ^~~~~~~
demo.y:48:12: error: union member ‘YYSTYPE::s’ with non-trivial ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::~basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
     string s;
            ^
demo.y:49:16: error: union member ‘YYSTYPE::v’ with non-trivial ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = int; _Alloc = std::allocator<int>]’
     vector<int>v;
                ^
y.tab.c:1203:30: error: use of deleted function ‘YYSTYPE::~YYSTYPE()’
     YYSTYPE yyvsa[YYINITDEPTH];
                              ^
y.tab.c:1215:11: error: use of deleted function ‘YYSTYPE::YYSTYPE()’
   YYSTYPE yyval;
           ^~~~~
y.tab.c:1215:11: error: use of deleted function ‘YYSTYPE::~YYSTYPE()’
y.tab.c:1381:14: error: use of deleted function ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’
   *++yyvsp = yylval;
              ^~~~~~
y.tab.c:223:7: note: ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’ is implicitly deleted because the default definition would be ill-formed:
 union YYSTYPE
       ^~~~~~~
demo.y:48:12: error: union member ‘YYSTYPE::s’ with non-trivial ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
     string s;
            ^
demo.y:49:16: error: union member ‘YYSTYPE::v’ with non-trivial ‘std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]’
     vector<int>v;
                ^
y.tab.c:1412:24: error: use of deleted function ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’
   yyval = yyvsp[1-yylen];
                        ^
y.tab.c:1569:14: error: use of deleted function ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’
   *++yyvsp = yyval;
              ^~~~~
y.tab.c:1713:14: error: use of deleted function ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’
   *++yyvsp = yylval;
              ^~~~~~
y.tab.c: In function ‘void __static_initialization_and_destruction_0(int, int)’:
y.tab.c:1174:9: error: use of deleted function ‘YYSTYPE::~YYSTYPE()’
 YYSTYPE yylval;
         ^~~~~~

标签: parsingcompiler-constructionbisonyacclex

解决方案


推荐阅读