I'm trying to write a small compiler which can parse some commands I type.
The command I try to parse is:
create class something = create class do_something ;
My code would be something like this:
grammar : my_grammar
{
list<Class1 *> *obj = new list<Class1 *>;
obj->push_back($1);
}
my_grammar : my_definition SEMICOLON
{
report("something detected!");
$$ = $1;
}
my_definition : CREATE CLASS class_name EQU class_expression
{
$5->setClassName(*$3);
$$ = $5;
}
class_expression : CREATE CLASS operand_name
{
$$ = new OperandClass();
$$->setOperationType("createClass");
$$->setOperandName(*$3);
}
However, when I try to call the parser somewhere else, I can't get the Class
I have defined before.
I guess there must be something wrong with the parser and have done some debugging with GDB. But I just can't step in the function push_back()
, neither can I print the information of obj
correctly.
So, I am wondering if there is a way that I can get the value of $$
or $1
while using GDB. Simply type in p $$
would print something else.
The simplest way is probably declaring a variable of the same type as your rule ($$
has this type) and assigning it.
%union {
int I;
}
%type<I> rule whatever
rule: whatever {
int foo = $1;
// printf("%d", foo);
$$ = $1;
}
You can then either see it in the debugger or just printf it.
The debugger cannot go inside push_back
or other standard functions unless you have standard library debugging information installed.
As for your question in general, your obj
is local to the rule, which gets transformed to the function by Bison, and is not visible outside it unless you store it somewhere else, e.g. in a global.