Click to See Complete Forum and Search --> : Bison and C Problem


sjaguar13
October 7th, 2005, 10:06 PM
I am trying to get my Bison program to all variables of numbers, a character, and a string of characters. I want to be able to do addition on the numbers and strings. String addition is just concatenation. I can get something like:
a = "Hello ";
b = "World";
c = "Hello " + "World";

to work, but this does not:
c = a + b;

How do I define variables as being okay in the rules for the '+'?

These are my rules:

charexp: CHARACTER { $$ = $1; }
| CHARVAR { $$ = $1->value.chr; }
| VAR '=' charexp { $$ = $3; $1->value.chr = $3; $1->type=1;}
;

strexp: STR { $$ = $1; }
| STRVAR { $$ = $1->value.strng; }
| VAR '=' strexp { $$ = $3; $1->value.strng = $3; $1->type=2;}
| strexp '+' strexp { strcpy($$,$1); strcat($$,$3); }
;

exp: NUM { $$ = $1; }
| VAR { $$ = $1->value.var; }
| VAR '=' exp { $$ = $3; $1->value.var = $3; $1->type=3;}
| FNCT '(' exp ')' { $$ = (*($1->value.fnctptr))($3); }
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { $$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2; }
| exp '^' exp { $$ = pow ($1, $3); }
| '(' exp ')' { $$ = $2; }
;

RoboTact
October 8th, 2005, 06:57 AM
You need variable table (mapping from varibale name/context to its value). Bison doesn't do it for you. Do you handle it on lexical stage? And post types of symbols next time.

sjaguar13
October 8th, 2005, 02:31 PM
I use a type variable to determine wether its a character (1), string (2) or number (3).

The files are here:
http://www.ceetus.com/bison/