CS5031 Exercises 4: Semantics

1. a) Show which declaration is referenced each time an identifier is used.
b) Construct a parse tree for each executable statement, and add the relevant type information to them, inserting extra nodes to represent automatic type conversions. Which statements are illegal?

static int i, j, k, a;
static float b, c;
static struct {
  int i, j, k;
} x;

static void p()
{
  int i, n, d;
  float e;
  for (n = 1; n <= 2; n++)
  {
    i= j; k++; d= a+b*c+e;
    if (e) e= d;
    if (n == 1)
    {
      int i, m, a;
      float f;
      i= j; m= n; f= a*b+c*d; p= x+f;
      if (k < 2)
        p();
    }
  }
}

int main(void)
{
  k= 0; j= 0;
  x.k= 0; x.j= 0;
  p();
  return 0;
}

2. Show the dictionary when the compiler reaches the point indicated in the following ANSI C program:

static int i, j, a[10];

static void p(long *i, long *j)
{
  typedef enum {red, blue} x;
  typedef char y;
  typedef x z[5];
  typedef long s;
  typedef struct r {
    long f1, f2;
    double f3, f4;
    char f5;
    union {
      char b;
      y f6;
      } UU;
    } r;
  x xx;
  z zz;
  s ss;
  r rr;
  /* show the dictionary here */
}