Topic 2 · Class activity 2 · Week 2 · Friday

First Contact with Bison

One rule at a time, a deliberate conflict, and your first reading of parser.output — items, dots and all.

Format pairs, at a laptopTime 50 min

The point

Get bison to talk to you. Its conflict reports and its parser.output file are the most useful and least read output in this course, and Topic 5 goes much better for people who have read one before.

Do this

  1. Build the skeleton and confirm it compiles:
    cd student/topic-2-minimal-compiler/compiler
    make
    ./minicompiler tests/t2_01_basics.cm out.s
    It will fail — the grammar is a placeholder. Read the message.
  2. Write ONE rule and nothing else: program : stmt_list, stmt_list : stmt | stmt_list stmt, stmt : decl, decl : INT ID ';'. Build. Feed it a file of declarations only, and confirm it parses.
  3. Deliberately create a conflict. Add a second rule stmt : INT ID ';' alongside stmt : decl. Rebuild and read what bison says.
  4. Get the full report. Run bison -d -v parser.y and open parser.output. Find the conflicted state. It lists the items — productions with a dot — exactly as in §5 of the lecture notes. Copy that state into your notes and identify which item wanted to shift and which wanted to reduce.
  5. Remove the conflict, then add the remaining rules for assign and print_stmt.
  6. Watch a parse. Add %define parse.trace and set yydebug = 1, or simply add a printf to each action. Parse x = 1 + 2; and write down the sequence of shifts and reduces. Compare it with the trace table in the lecture notes.
parser.output is a gift

Almost everyone in this course eventually has a grammar with a conflict they do not understand. The answer is always in parser.output. Fifteen minutes learning to read it now saves an evening in Topic 5, where the dangling-else conflict arrives on schedule.

Deliverable

Your parser.y with all four statement forms parsing; the parser.output excerpt from step 4 with one sentence identifying the two competing items; and your shift/reduce trace from step 6.