Semantic Analysis and SDT Mistakes That Cost Marks: Exact Fixes
Knowing a grammar is not enough if attributes flow backwards or actions fire at the wrong point. Correct seven common errors with worked semantic and TAC traces.
KnowledgeGate Team
Exam prep & CS education

You may know the grammar and still lose a question by sending an attribute in the wrong direction, executing an action too early, or importing a type rule that was never given. The costly errors are directional: an attribute can flow from the wrong node, an action can fire before its operands exist, or a type rule can be applied without evidence. One arithmetic expression, one declaration, one scope trace, and one three-address-code trace show exactly where each shortcut changes the answer.
1. Mistake: Treating Parser Success as Semantic Correctness
Why it happens: Parsing gives a visible accept or reject result, so it is tempting to stop when the tokens fit a production.
What goes wrong: Syntax proves structure, not meaning. Under a conventional typed-language example, int x; x = true + 2; can match the assignment and expression grammar, yet fail because true + 2 has no permitted numeric type.
What to do instead: After parsing, check declaration before use, nearest-scope binding, operator and assignment compatibility, and context restrictions such as break, continue, and return type. Base every conclusion on the rules supplied in the question.
2. Mistake: Reversing Synthesised and Inherited Attribute Flow

Why it happens: Learners memorise that values move “up” or “down” without identifying which occurrence supplies them.
What goes wrong: For 2 + 3 * 4, use E -> E1 + T, E -> T, T -> T1 * F, T -> F, and F -> digit. The leaves supply 2, 3, and 4. Multiplication gives 3 * 4 = 12, then the root gives 2 + 12 = 14. The val attributes are synthesised because information moves from children to parent. Blind left-to-right calculation instead gives (2 + 3) * 4 = 20.
What to do instead: Mark the source of every right-hand-side value before drawing an arrow. For D -> T L and float rate, tax, total, set T.type = float, pass L.in = T.type, and give each identifier the inherited type float from its parent or left context.

3. Mistake: Reversing the S-Attributed and L-Attributed Relation
Why it happens: Attribute direction, definition class, and evaluation order are remembered as one fact.
What goes wrong: The arithmetic definition above uses only synthesised attributes, so it is S-attributed and therefore also L-attributed. The declaration uses inherited L.in, so it can be L-attributed without being S-attributed. Also, production order is not automatically evaluation order. If A.x = B.y + 1 and B.y = A.x + 2, the two dependency edges form a cycle, so no topological evaluation exists. In the acyclic case, B.y = 3, A.x = B.y + 1 = 4, and C.z = 2 * A.x = 8.
What to do instead: Classify attributes, draw dependency edges, reject cycles, and only then choose a topological order. The Semantic Analysis and SDT worked guide owns broad definitions and longer 3 * 5 + 4 traces. The Semantic Analysis and Syntax-Directed Translation worked examples owns binary-value and action-placement walkthroughs; this post owns mistake-first repairs for attribute flow, coercion, scope, and TAC.
4. Mistake: Treating an SDT Action as a Comment
Why it happens: A semantic action looks like an annotation beside a grammar rule.
What goes wrong: Its written position fixes its execution time. Use E -> E1 + T { emit('+') }, E -> T, T -> T1 * F { emit('*') }, T -> F, and F -> digit { emit(digit.lexeme) }. For 6 * 2 + 1, end-position actions emit 6 2 * 1 +. Moving emit('*') before its operands emits * before 6 2, which is not the required postfix order.
What to do instead: Number each action, follow parser order, and emit when it fires. See syntax-directed translation and code optimization for more action-placement traces.
5. Mistake: Guessing Coercion Rules Instead of Reading the Type System
Why it happens: Familiar behaviour from C, Java, or another language is silently imported.
What goes wrong: Both “mixed types always fail” and “the compiler always converts them” are unsafe. Suppose the question states: int + int -> int; mixed int/float addition widens the integer and returns float; float + float -> float; assignment float <- int is allowed; assignment int <- float needs an explicit cast. With int n = 4; float r = 2.5; r = n + r;, 4 widens to 4.0, the sum is 6.5, and the assignment is valid. Under the same rules, n = r; is invalid.
What to do instead: Write the operator-result and assignment rules separately. Insert only stated coercions, and do not call a runtime-dependent failure a static semantic error without a supplied rule.
6. Mistake: Using One Flat Symbol Table for Every Scope
Why it happens: A symbol table is imagined as one dictionary from spelling to type.
What goes wrong: Take global level 0 with int count = 1 and function level 1 with float count = 2.5. Inside the function, count + 1.0 resolves to level 1 and evaluates to 3.5. After leaving the function, lookup returns the level-0 integer, still 1. A flat overwrite can destroy the global binding or return the wrong type.
What to do instead: Model enterScope, same-scope duplicate detection, insertion, nearest-enclosing lookup, and exitScope. Keep legal shadowing separate from duplicate declaration in one scope.
7. Mistake: Emitting Three-Address Code in Token Order
Why it happens: The source is scanned left to right, so code is emitted before the operator tree and dependencies are settled.
What goes wrong: For y = p + q * 2, with p = 5 and q = 3, multiplication comes first. Generate t1 = q * 2, giving 6; t2 = p + t1, giving 11; then y = t2. The token-order translation t1 = p + q, giving 8; t2 = t1 * 2, giving 16; then y = t2 changes the program's meaning.
What to do instead: Respect the parse tree or AST, then compute each node's place and code attributes after its dependencies. The Intermediate Code Generation worked examples extend this method to TAC, quadruples, triples, and DAGs.
For representative GATE-style practice, verify four checkpoints: the arithmetic value is 14, the postfix trace is 6 2 * 1 +, S-attributed definitions form a subset of L-attributed definitions, and the TAC value is 11, not 16. Use these checkpoints before timed practice.
8. The Short Correction Checklist and Next Step
Before calculating, identify the compiler phase, mark every attribute source, classify S-attributed versus L-attributed definitions, draw dependencies and reject cycles, execute actions at their written positions, apply only the stated coercion and scope rules, and build the operator tree before TAC. The short version is simple: make information flow and execution order explicit.
If you miss two or more of those checks, revisit them through the Compiler Design course. For a wider core-CS path, browse the Theory of Computation and Compilers learning module.
Keep learning
Lexical Analysis Mistakes for GATE: 7 Mark-Losing Errors and Exact Fixes
These seven repairs show exactly where lexical analysis answers go wrong. Trace one shared source, separate token counts from lexemes and categories, and keep each compiler phase in its lane.
Intermediate Code Generation Mistakes: 7 Mark-Losing Errors and Exact Fixes
Seven tempting shortcuts can corrupt an otherwise neat intermediate-code answer. Trace each wrong result, then apply the exact repair for TAC, arrays, records, jumps, loops and expression reuse.
Run-Time Environment Mistakes: 7 Mark-Losing Errors and Exact Fixes
Definitions are not enough when a trace depends on storage duration, calling order, lexical nesting and frame-address rules. Repair seven common errors through small, checkable examples.
Control Flow in C: 7 Mark-Losing Mistakes and Precise Fixes
Trace seven common C control-flow mistakes step by step, see exactly where execution changes course, and learn a short routine for checking every branch and loop.





