Topic 2 · Class activity 5 · Week 4 · Wednesday

Predict the Symbol Table

Write down what the table will hold before you run anything, grade your own error messages, and say why the parser could not have caught any of it.

Format pairsTime 45 min

The point

Predict what the symbol table will contain before you look. Being able to do that reliably is the difference between debugging and guessing.

Do this

  1. Predict. For the program below, write down what the symbol table holds after each line: names, and the storage location each one gets.
    int a;
    int b;
    int sum;
    a = 3;
    b = 4;
    sum = a + b;
    print(sum);
  2. Check. Compile without -q, find the symbol-table trace, and reconcile any differences.
  3. Break it three ways, predicting the message before each run:
    • int a; int a; — duplicate declaration
    • x = 1; with no declaration of x
    • print(y); with no declaration of y
  4. Grade your own messages. For each: does it give the LINE? name the IDENTIFIER? say what was EXPECTED? suggest a FIX? Rewrite any that score below three out of four.
  5. Theory question. Explain in two sentences why none of these three errors could have been caught by the parser, referring to what a context-free grammar can and cannot express.
  6. Design question. Our table is a flat array searched linearly. Sketch what changes if it becomes a hash table. Which functions change? Which do not? Why does that second answer matter more?

Deliverable

Your predictions beside the actual traces; three improved error messages; your two-sentence answer to step 5; and a short answer to step 6. Steps 3–4 feed Lab Question 5 directly.