[英]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.