简体   繁体   English

野牛:what():basic_string :: _ S_construct null无效

[英]Bison: what(): basic_string::_S_construct null not valid

So I'm using Bison for a project I'm working on. 因此,我正在将Bison用于我正在从事的项目。 My bison file looks similar to this: 我的野牛文件看起来像这样:

%{
#include <iostream>
....
%}
%union
{
  int intVal;
  double dVal;
  char charVal;
  char* strVal;
}
%token ID NUMBER INT DOUBLE CHAR STR END
%type <strVal> ID 
%type <strVal> INT DOUBLE CHAR STR END
%type <intVal> NUMBER
%type <strVal> dataType
%start program
%%
program: expressions
...
dataType: INT
        | DOUBLE
        | CHAR
        | STR
varDef: dataType ID { std::cout << $1 << endl; }
....

When i compile and run this and try running "int a" through it, it will print out $2 of varDef ("a") but when I tell it to print $1, i get 当我编译并运行它并尝试通过它运行“ int a”时,它将打印出2美元的varDef(“ a”),但是当我告诉它打印$ 1时,我得到了

terminate called after throwing an instance of 'std::logic_error'
    what():  basic_string::_S_construct null not valid

I've also tried changing my %union to be 我还尝试过将%union更改为

%union
{
   struct {
      ....
   };
}

and it didnt' change a thing. 它并没有改变任何事情。 Any Idea what I'm doing wrong? 知道我在做什么错吗?

You have no actions for: 您没有采取以下行动:

  dataType: INT
    | DOUBLE
    | CHAR
    | STR

The default action in bison is 野牛的默认动作是

{ $$ = $1 }

As the tokens 作为代币

%token ID NUMBER INT DOUBLE CHAR STR END

Have no type you passed something with no value and no type to something which is type *char 没有类型,您传递了没有值的东西,也没有类型传递给*char类型的东西

(I see you now have solved it, but hopefully my explanation explains why) (我看到您现在已经解决了问题,但希望我的解释能够解释原因)

If anyone's interested I just solved it. 如果有人感兴趣,我就解决了。 I changed 我变了

dataType: INT { $$ = "int"; }
        | DOUBLE { $$ = "double"; }
....

So i was actually passing in something null into cout. 所以我实际上是将null传递给cout。 Thanks for the help! 谢谢您的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 basic_string :: _ S_construct null无效 - basic_string::_S_construct null not valid openCV basic_string :: _ S_construct null无效 - openCV basic_string::_S_construct null not valid 抛出&#39;std :: logic_error&#39;what()实例后调用终止终止:basic_string :: __ S_construct null无效 - terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 在函数参数/套接字中发送std :: string时basic_string :: _ S_construct null无效错误 - basic_string::_S_construct null not valid error while sending std::string in function argument/socket std :: string(empty char *)错误:basic_string :: _ S_construct null无效错误 - std::string(empty char*) error: basic_string::_S_construct null not valid error 字符串 std::logic_error: basic_string::_S_construct null 无效 - String std::logic_error: basic_string::_S_construct null not valid std :: logic_error:basic_string :: _ s_construct null无效 - std::logic_error: basic_string::_s_construct null not valid C ++:脚本中发生异常:basic_string :: _ S_construct NULL无效 - C++ : Exception occurred in script: basic_string::_S_construct NULL not valid 我运行程序后,C ++给我basic_string :: _ S_construct null无效错误 - C++ giving me basic_string::_S_construct null not valid error after I run my program 如何避免错误:在抛出&#39;std :: logic_error&#39;的实例后调用terminate what():basic_string :: _ S_construct null无效 - How to avoid the error: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM