KOI: Some more hints for lab 5

The ugly printout in the example was generated with the following code.

First the function printtree:

void printtree(TreeNode* p) {
  printf("Syntax tree:\n");
  printtree1(p, 0);
}
Then the recursive function printtree1:
void printtree1(TreeNode* p, int level) {
  if (p == 0)
    ;
  else if (p->type == ID) {
    printf("%*s", 2*level, "");
    printf("%s\n", symtable[p->leaf_value].lexptr);
  }
  else if (p->type == NUM) {
    printf("%*s", 2*level, "");
    printf("%d\n", p->leaf_value);
  }
  else if (p->type == '+') {
    printf("%*s", 2*level, "");
    printf("+\n");
    printtree1(p->args[0], level + 1);
    printtree1(p->args[1], level + 1);
  }
  else if (p->type == ';') {
    printf("%*s", 2*level, "");
    printtree1(p->args[0], level + 1);
    printf("%*s", 2*level, "");
    printf(";\n");
    printtree1(p->args[1], level);
  }
}

Thomas Padron-McCarthy (thomas.padron-mccarthy@oru.se) February 11, 2002