0
$\begingroup$

I given the following code to write as AST:

 {
       a = 4;
       while (a<10)
         if (a<5)
           a = a+5;
         else
           a = a+3;
 }

Looking up the solution:

                       PROG
                         |
                        SEQ
         _______________/ \_______________
       SEQ                              WHILE
    ___/ \___                    ________/ \________
  EMPTY     EXPR                LT                _IF2_
             |                 / \         ______/  |  \______
           ASSIGN            VAR CST      LT      ASSIGN    ASSIGN
            / \               0  10      / \       / \       / \
          VAR CST                      VAR CST   VAR ADD   VAR ADD
           0   4                        0   5     0  / \    0  / \
                                                   VAR CST   VAR CST
                                                    0   5     0   3

Why is in the left side of the tree we have an EMPTY?

Why is the do we need SEQ why isn't it directly EXPR in the left side?

$\endgroup$
  • 2
    $\begingroup$ This depends on the grammar you're using. Can you describe it? $\endgroup$ – Yuval Filmus Oct 24 '19 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.