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.
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
to work,
CHARneeds to have a defined type, for example:and the lexer needs to provide the value in
yylval.dvalwhen it returns aCHARtoken. Now depending on what you want the inputa+1=bto actually do, (and assuming the lexer returnsCHARforaandband notSYMBOL) this may or may not be what you want.