将 ASCII 值分配给 bison 中的变量
我希望我的解析器处理如下情况:
a='A'; // a should be set with ASCII(A) i.e 65
我的令牌声明如下所示:
%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
我的语句声明如下所示:
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); }
我的表达式声明如下所示:
expression: expression ',' expression { $$ = $3; }
| SYMBOL_NAME '=' expression { if(assign_value($1, $3, &$$)) YYABORT; }
| NUMBER { $$ = $1; }
| '\'' CHAR '\'' { if(set_ASCII($2, &$$)) YYABORT; }
不应该
'\'' CHAR '\'' { if(set_ASCII($2, &$$)) YYABORT; }
处理我的情况? 正如我所见,当我输入类似
a='A';
set_ASCII() 时,没有被调用。这里有什么不正确的吗?
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; }
| SYMBOL_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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案几乎肯定在于解析器的灵活方面:它永远不会返回
CHAR
标记。您还没有向我们展示解析器的 Flex 方面,但是您如何使 Flex 区分
SYMBOL_NAME
和CHAR
呢?通常,检测的代码是<字符>
序列是在 flex 中实现的,而不是在 bison 中实现的。
存储在联合体和返回值CHAR
中。在野牛方面,将'\'' CHAR '\''
模式更改为CHAR
。或者,只需将
'\'' CHAR '\''
模式更改为'\'' SYMBOL_NAME '\''
。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 aCHAR
? 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 valueCHAR
. On the bison side, change your'\'' CHAR '\''
pattern toCHAR
.Alternatively, just change your
'\'' CHAR '\''
pattern to'\'' SYMBOL_NAME '\''
.