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
- Build the skeleton and confirm it compiles:
It will fail — the grammar is a placeholder. Read the message.cd student/topic-2-minimal-compiler/compiler make ./minicompiler tests/t2_01_basics.cm out.s - 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. - Deliberately create a conflict. Add a second rule
stmt : INT ID ';'alongsidestmt : decl. Rebuild and read what bison says. - Get the full report. Run
bison -d -v parser.yand openparser.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. - Remove the conflict, then add the remaining rules for
assignandprint_stmt. - Watch a parse. Add
%define parse.traceand setyydebug = 1, or simply add aprintfto each action. Parsex = 1 + 2;and write down the sequence of shifts and reduces. Compare it with the trace table in the lecture notes.
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.