Reputation: 75
So I'm using Bison for a project I'm working on. 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
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
{
struct {
....
};
}
and it didnt' change a thing. Any Idea what I'm doing wrong?
Upvotes: 1
Views: 600
Reputation: 5893
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
(I see you now have solved it, but hopefully my explanation explains why)
Upvotes: 4
Reputation: 75
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. Thanks for the help!
Upvotes: 0