The point
Make the tree real, and make it visible. This is where a syntax-directed definition stops being a diagram and becomes code that runs.
Do this
- Write the AST constructors. Follow the pattern of the one that is given. Five functions, three lines each.
- Wire them into the grammar actions. Each action computes this node's attribute from its children's — write one out longhand as a syntax-directed definition rule before you write the C, and confirm it is synthesized.
- Write the tree printer. Two spaces of indent per level; recurse at
level + 1— except forNODE_STMT_LIST, whose children print at the same level, because a sequence is not a nesting. - Test it against your paper answer from Activity 3. If the printed tree does not match what you drew, one of the two is wrong. Find out which before going further.
- Deliberately break it. In
createAssign, store the name withoutstrdup. Rebuild, run, and describe what you see. Then explain it: what does the scanner do with its buffer between tokens?
The scanner reuses its buffer for the next token, so a name you merely point at will have changed by the time the semantic analyzer reads it. The symptom is an AST that looks randomly corrupted, and it is among the hardest bugs to find in this course — unless you have seen it once, on purpose, when you knew what you had just done.
Deliverable
A working tree printer, its output for the Activity 3 program, one grammar action written out as a syntax-directed rule, and a paragraph describing what step 5 produced and why.