compiler-construction - YACC时表达式&&表达式错误!无法先读取第一个表达式
问题描述
我正在写一个解析器,我做了一些错误信息来检查。
但是当我进入像
while ( index<=n && index > 0 ) 这样的循环时...
我的语法很好。
但是运行这行代码的过程,根据我的跟踪,
它首先跟踪表达式:index>0,然后跟踪表达式&&表达式,
最后一个将是第一个表达式(索引<=n)。但为什么?
先检查 index <=n,然后检查 index >0,最后一个将是表达式 && 表达式是不是正确的?
只有一种情况,会好的。
例如: while (index<=n) do
但是如果是组合条件,就会出错。
这是我的部分代码
expr: expr LE expr
{<br/>
Trace("expression <= expression");
if ($1->type != $3->type) yyerror("type not match");
if ($1->type != intType && $1->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 ($1->type != $3->type) yyerror("type not match");
if ($1->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 ($1->type != $3->type) yyerror("type not match");
if ($1->type != intType && $1->type != realType) yyerror("operator error");
idInfo *info = new idInfo();
info->attribute = 2; //variable type
info->type = boolType;
$$ = info;
}
这些是根据我的跟踪的结果....
while
'('
ID:index
<=
ID:n
&&
ID:index
'>'
INTEGER:0
')'
行:16 表达式 > 表达式
行:16 表达式 && 表达式
行:16 类型不匹配
行:16 运算符错误
行:16 表达式 <= 表达式
行:16 类型不匹配
解决方案
推荐阅读
- python - 有没有办法将 asyncio Writer 包装为 FileWriter 以将 async writer 传递给现有的 API?
- python - 如何从 Chisquare 函数检查 python 的拟合优度?
- batch-file - 在 START 命令中运行两个命令
- c# - 如何在 C# 中通过 Powershell 读取 VM 的 IP?
- wordpress - 移动网站后如何修复损坏的源链接?
- mysql - 从子表 MySQL 中获取最新行时,返回 GROUP BY 与 JOIN 子查询的不同结果
- apache-kafka - __consumer_offsets 如何为两个消费者保持偏移量?
- python - 'str' 和 'int' 的实例之间不支持 '<='
- angular - 如何从 Observable 返回一个数组
- python - 在 Django 中计算来自 Tabel 表单的用户输入