Reputation: 2635
I am learning bison through the standard calculator example, where I am trying to add some new functionalities to already written calculator. I am trying to add a feature to my calc where a+1 = b would work.
in my .y file I have to declare terminal symbols :
%token <symbol> SYMBOL_NAME
%token <dval> NUMBER
%token LIST_VAR_COMMAND
%token LIST_CONST_COMMAND
%token LIST_FUNC_COMMAND
%token DELETE_COMMAND
%token QUIT_COMMAND
%token CLEAR_COMMAND
%token BINARY
%token OCTAL
%token DECIMAL
%token HEXADECIMAL
%token UNSIGNED
%token CHAR
%token SHORT
%token INT
%token LONG
%token FOR
I declare my statement to be like :
statement: ';'
| expression { setp($1); print_universal_base($1, 0); }
| expression BINARY { setp($1); print_universal_base($1, 2); }
| expression OCTAL { setp($1); print_universal_base($1, 8); }
| expression DECIMAL { setp($1); print_universal_base($1, 10); }
| expression HEXADECIMAL { setp($1); print_universal_base($1, 16); }
I declare my expression to be like :
expression: expression ',' expression { $$ = $3; }
| NUMBER { $$ = $1; }
| CHAR { $$ =(int)$1; }
I get an error in line
| CHAR { $$ =(int)$1; }
saying : $1 of `expression' has no declared type
what am I doing wrong here?
Thanks.
Upvotes: 1
Views: 690
Reputation: 126408
To access the value of some symbol from the rule, that symbol needs to have a type, and its value needs to be set properly. So for the rule
expression: CHAR { $$ = $1; }
to work, CHAR
needs to have a defined type, for example:
%token<dval> CHAR
and the lexer needs to provide the value in yylval.dval
when it returns a CHAR
token. Now depending on what you want the input a+1=b
to actually do, (and assuming the lexer returns CHAR
for a
and b
and not SYMBOL
) this may or may not be what you want.
Upvotes: 1
Reputation: 63
Casting the value $1 to (int) is usually not how you do this. You need to declare that 'expression' has a certain type, thus bison knows what part of the union it needs to assign. Just try:
%type <dval> expression
Upvotes: 2