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; }
;
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; }
;