I want my parser to handle case like:
a='A'; // a should be set with ASCII(A) i.e 65
My token declaration looks like :
%union {
double dval;
char *symbol;
}
%token <symbol> SYMBOL_NAME
%token <dval> NUMBER
%token BINARY
%token OCTAL
%token DECIMAL
%token HEXADECIMAL
%token UNSIGNED
%token <symbol>CHAR
%token SHORT
%token INT
%token LONG
my statement declaration looks 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); }
my expression declaration looks like this:
expression: expression ',' expression { $$ = $3; }
| SY开发者_开发技巧MBOL_NAME '=' expression { if(assign_value($1, $3, &$$)) YYABORT; }
| NUMBER { $$ = $1; }
| '\'' CHAR '\'' { if(set_ASCII($2, &$$)) YYABORT; }
shouldn't
'\'' CHAR '\'' { if(set_ASCII($2, &$$)) YYABORT; }
handle my case? as I see that when I input like
a='A';
set_ASCII() is not being called. what is incorrect here?
what is incorrect here?
The answer almost certainly lies in the flex side of your parser: It is never returning a CHAR
token.
You haven't shown us the flex side of your parser, but how are you making flex distinguish between a SYMBOL_NAME
and a CHAR
? Typically the code that detects a <single_quote> <char> <single_quote>
sequence is implemented in flex, not bison. The <char>
is stored in the union and the return value CHAR
. On the bison side, change your '\'' CHAR '\''
pattern to CHAR
.
Alternatively, just change your '\'' CHAR '\''
pattern to '\'' SYMBOL_NAME '\''
.
精彩评论