I'm writing a parser, and I do some error message to check.
But when I go to a loop like
while ( index<=n && index > 0 ) do ...
my syntax is fine.
but the process to run this line's code, according to my trace,
it trace the expression:index>0 first, then trace expression && expression,
the last one will be the first expression (index<=n). but why?
Isn't it correct to check index <=n first, then index >0, the last one will be expression && expression?
In only one condition, it'll be fine.
for example like: while (index<=n) do
But if it's combined conditions, things go wrong.
this are my partial code
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;
}
These are the result according to my trace....
while
'('
ID:index
<=
ID:n
&&
ID:index
'>'
INTEGER:0
')'
Line: 16 expression > expression
Line: 16 expression && expression
Line: 16 type not match
Line: 16 operator error
Line: 16 expression <= expression
Line: 16 type not match
Your precedence declarations are incorrect or missing.
Without actually seeing them, it is hard to provide more information, but it looks like all of your operators have the same precedence and right associativity.
If you received a warning about parsing conflicts, it would have been useful had you mentioned that fact (and even better had you solved the issue).
It is almost always better to use Bison's built-in trace facility than to try to do it yourself. Bison's feature is more comprehensive, more accurate, and a lot less work. See the Debugging your Parser section in the Bison manual.