RE: Help with IF-ELSEIF-ELSE-ENDIF Coding

"Quinn Tyler Jackson" <qjackson@home.com>
16 Oct 2001 00:10:31 -0400

          From comp.compilers

Related articles
RE: Help with IF-ELSEIF-ELSE-ENDIF Coding qjackson@home.com (Quinn Tyler Jackson) (2001-10-16)
| List of all articles for this month |

From: "Quinn Tyler Jackson" <qjackson@home.com>
Newsgroups: comp.compilers
Date: 16 Oct 2001 00:10:31 -0400
Organization: Compilers Central
Keywords: design
Posted-Date: 16 Oct 2001 00:10:31 EDT

> I am enhancing a compiler written be a previous employee. This may
> seem like a naive question but, I figured out the grammer we wanted
> now I am trying to do the code part. All the example I have looked at
> are more complicated then I really want to do. This may be my lack of
> knowledge but I just want to know if someone can give me an C code
> example for the following:
>
> IF '(' cond ')' statements ELSE statements ENDIF { ? },
>
> how do I get to the ELSE statements if the "cond" ends up being false.


[...]


> [The short answer is "with a jump". Straight-through interpretation
> starts to get unpleasant as soon as you have flow of control. It's
> usually easier to compile to an internal form like a parse tree or
> RPN strings and then interpret that. -John]


Assuming you go the parse tree route suggested by John, that
particular reduction would generate a tree node traversal code that
would look something like this (keeping in mind that the following
code is greatly abbreviated):


struct EXPRESSION_NODE
{
  //
};


struct STATEMENTS_NODE
{
  //
};


struct IF_ELSE_NODE
{
      EXPRESSION_NODE* expr;
      STATEMENTS_NODE* true_path;
      STATEMENTS_NODE* else_path;
};


void interpret_if_else_node(IF_ELSE_NODE* pnode)
{
        if(interpret_expression(pnode->expr) != 0)
        {
                interpret_statement(pnode->true_path);
        }
        else
        {
                  if(pnode->else_route) // if statements that do not contain an else path
would simply have this set to 0
                          interpret_statement(pnode->else_path);
        }
}


The above example assumes that the translator has already constructed
an EXPRESSION_NODE and two STATEMENT_NODE tree nodes by the time the
IF_THEN_ELSE production is triggered. These nodes would then be
attached to the IF_ELSE_NODE, and this, in turn, would percolate up by
being attached to a statements_node.
--
Quinn Tyler Jackson


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.