YACC when expression && expression error!不能先读第一个表达式
YACC when expression && expression error! can't read the first expression first
我正在写一个解析器,我做了一些错误信息来检查。
但是当我进入
这样的循环时
while ( index<=n && index > 0 ) 做...
我的语法没问题。
但是运行这一行代码的过程,根据我的追踪,
它首先跟踪 expression:index>0,然后跟踪表达式 && 表达式,
最后一个将是第一个表达式 (index<=n)。但是为什么?
是不是先检查index <=n,再检查index >0,最后一个就是expression && expression?
只要有一种情况就可以了
例如:while (index<=n) do
但是如果是组合条件,就出问题了。
这是我的部分代码
expr: expr LE expr
{<br/>
Trace("expression <= expression");
if (->type != ->type) yyerror("type not match");
if (->type != intType && ->type != realType) yyerror("operator error");
idInfo *info = new idInfo();
info->attribute = 2; //variable type
info->type = boolType;
$$ = info;
}<br/>
|expr AND expr
{
Trace("expression && expression");
if (->type != ->type) yyerror("type not match");
if (->type != boolType) yyerror("operator error");
idInfo *info = new idInfo();
info->attribute = 2; //variable type
info->type = boolType;
$$ = info;
}
|expr GG expr
{<br/>
Trace("expression > expression");
if (->type != ->type) yyerror("type not match");
if (->type != intType && ->type != realType) yyerror("operator error");
idInfo *info = new idInfo();
info->attribute = 2; //variable type
info->type = boolType;
$$ = info;
}
这些是我追踪的结果....
而
'('
ID:index
<=
ID:n
&&
ID:index
'>'
INTEGER:0
')'
第 16 行表达式 > 表达式
行:16 表达式 && 表达式
行:16 类型不匹配
行:16 运算符错误
行:16 表达式 <= 表达式
行:16 类型不匹配
您的 precedence declarations 不正确或缺失。
没有实际看到它们,很难提供更多信息,但看起来您的所有运算符都具有相同的优先级和正确的结合性。
如果您收到有关解析冲突的警告,如果您提到该事实将会很有用(如果您解决了该问题则更好)。
使用 Bison 的 built-in 跟踪工具几乎总是比尝试自己做更好。 Bison 的功能更全面,更准确,工作量也少很多。请参阅 Bison 手册中的 Debugging your Parser 部分。
我正在写一个解析器,我做了一些错误信息来检查。
但是当我进入
这样的循环时
while ( index<=n && index > 0 ) 做...
我的语法没问题。
但是运行这一行代码的过程,根据我的追踪,
它首先跟踪 expression:index>0,然后跟踪表达式 && 表达式,
最后一个将是第一个表达式 (index<=n)。但是为什么?
是不是先检查index <=n,再检查index >0,最后一个就是expression && expression?
只要有一种情况就可以了
例如:while (index<=n) do
但是如果是组合条件,就出问题了。
这是我的部分代码
expr: expr LE expr
{<br/>
Trace("expression <= expression");
if (->type != ->type) yyerror("type not match");
if (->type != intType && ->type != realType) yyerror("operator error");
idInfo *info = new idInfo();
info->attribute = 2; //variable type
info->type = boolType;
$$ = info;
}<br/>
|expr AND expr
{
Trace("expression && expression");
if (->type != ->type) yyerror("type not match");
if (->type != boolType) yyerror("operator error");
idInfo *info = new idInfo();
info->attribute = 2; //variable type
info->type = boolType;
$$ = info;
}
|expr GG expr
{<br/>
Trace("expression > expression");
if (->type != ->type) yyerror("type not match");
if (->type != intType && ->type != realType) yyerror("operator error");
idInfo *info = new idInfo();
info->attribute = 2; //variable type
info->type = boolType;
$$ = info;
}
这些是我追踪的结果....
而
'('
ID:index
<=
ID:n
&&
ID:index
'>'
INTEGER:0
')'
第 16 行表达式 > 表达式
行:16 表达式 && 表达式
行:16 类型不匹配
行:16 运算符错误
行:16 表达式 <= 表达式
行:16 类型不匹配
您的 precedence declarations 不正确或缺失。
没有实际看到它们,很难提供更多信息,但看起来您的所有运算符都具有相同的优先级和正确的结合性。
如果您收到有关解析冲突的警告,如果您提到该事实将会很有用(如果您解决了该问题则更好)。
使用 Bison 的 built-in 跟踪工具几乎总是比尝试自己做更好。 Bison 的功能更全面,更准确,工作量也少很多。请参阅 Bison 手册中的 Debugging your Parser 部分。