flex,定义 char
我想定义 char (即 'a AND 'a'),但我在检查错误时遇到问题。 这是我如何编写规则并检查:
char " ' " {letter}
代码
{char} {
int x =input() ;
//printf("%d",'a');
if(x == 10)
{
return(tCharunterm);
}
else if(x == '\'')
{
return(tChar);
}
else
{
yyerror("char overflow");
}
最后检查它:
'a
token = tCharunterm, value = "(null)"
'a'
token = tChar, value = "(null)"
'as
char overflow
'asddd
char overflow
token = tIdentifier, value = "ddd"
^Z
I want to define char (ie 'a AND 'a') but I am having issues in checking errors.
Here how I write the rule and check:
char " ' " {letter}
code
{char} {
int x =input() ;
//printf("%d",'a');
if(x == 10)
{
return(tCharunterm);
}
else if(x == '\'')
{
return(tChar);
}
else
{
yyerror("char overflow");
}
And finally checking it:
'a
token = tCharunterm, value = "(null)"
'a'
token = tChar, value = "(null)"
'as
char overflow
'asddd
char overflow
token = tIdentifier, value = "ddd"
^Z
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,你永远不想在你的flex代码中直接调用“input”——这是flex用来获取更多输入的例程,所以如果你调用它,你就会从输入中间拉入随机字符并使flex感到困惑认为它们不存在。做到这一点的最佳方法是定义多个规则并依靠最长的匹配来获得正确的规则。
您可能还希望这些规则中的
yylval.ch = yytext[1];
返回您匹配的实际字符值。Generally, you NEVER want to call 'input' directly in your flex code -- that's the routine flex uses to get more input, so if you call it, you're pulling in random characters out of the middle of the input and confusing flex into thinking they don't exist. The best way to do this is to define multiple rules and rely on the longest match to get the right one.
you might also want
yylval.ch = yytext[1];
in these rules to return the actual character value you've matched.