I want to create a calculator using flex/bison. I have created the following %union
:
%union{
int intValue;
double realValue;
char * numType;
}
So when I want to reduce to a rule like term: primary
I check the numType if it is "INT" or "REAL" and i do something like this:
$<realValue>$=$<realValue>1; $<numType>$=$<numType>1;
(the same thing for intValue). Now the problem is that when i have rules like:
term:
expr '+' expr |
expr '*' expr |
...
I have to check too many times if the terms are real or ints so as to put them into the $realValue$ or $intValue$. The question is: Is it possible or is there any feature of bison/yacc that can reduce all this checking?
For example something like
%type <intValue,realValue>
Where you can assign a type two values and then for example if you have a rule like this:
a: primary ;
From now on when a is used to reduce to another rule and a is accessed then the realValue or intValue (the one that has value) will be accessed.Or something like that.... ?