Skip to content

Adding Flow Control Statements

IFstmt ::= IF (E) then Slist Else Slist ENDIF;
    | IF (E) then Slist ENDIF;
Whilestmt ::= WHILE (E) DO Slist ENDWHILE;
E ::= E < E | E > E | E < =E | E >= E | E != E | E == E;
read(a);  
 read(b);  
 read(c);  
 if (a < b) then  
  if (b < c) then Write(c); else Write(b); endif;  
 else  
  if (a < c) then Write(c); else Write(a); endif;  
 endif;

There are the two types of expressions - arithmetic expressions and logical expressions.

The guard of the if-else and while-do statement must be a boolean expression.

#define inttype 1
#define booltype 0
typedef struct tnode {
    int val;
    int type;
    char* varname;
    int nodetype;
    struct tnode* left, *right;
}tnode;

Type Checking

The leaf nodes of the tree are either constants or variables, the type must be set to integer.

E :== E+E {
    if(($1->type != inttype) || ($2->type != inttype)) {
        yyerror("Type mismatch\n");
        exit(1);
    } else {
        $$->type = inttype;
    }
}