I’m writing a compiler, and I have working code for handling infinitely nested if statements, but it’s kind of a hack. I don’t know if it’s safe to do this?
con_statement:
IF exp DO
{
$1 = ifNum++;
if($2 == BOOLEAN_TYPE || $2 == INTEGER_TYPE)
{
utstring_printf(code, "\tpop\teax\n");
utstring_printf(code, "\tcmp\teax, 0\n");
utstring_printf(code, "\tje\tIF_END_%d\n", $1);
}
if($2 == FLOAT_TYPE)
{
utstring_printf(code, "\tnop\n");
}
}
program FI
{
utstring_printf(code, "IF_END_%d:\n", $1);
}
;
This works fine but it would be IMO clearer to use $$/$4:
The first action is generating a value (which it puts into
$$), and then later actions can access that value.Alternately (and particularly if you want to support ELSE), it may make sense to split this initial action onto a separate production:
This allows using the same action for plain if and if/else, avoiding a grammar conflict, since at the point you are parsing the
IF..DOyou don’t know whether there will be anELSEor not.