CSS Prepare

optional · CSS study notes

Syntax Error Handling & Recovery

Definition

Syntax error handling refers to how a parser detects, reports, and recovers from input that does not conform to the grammar — so compilation can continue and report multiple errors in one pass rather than stopping at the first.

Key Points — Recovery Strategies

  • Panic-mode recovery: parser discards input tokens one at a time until a synchronizing token (e.g., ;, }) is found, then resumes — simple, may skip real errors, but never loops infinitely.
  • Phrase-level recovery: parser performs local correction (insert/delete/replace a token) on the remaining input — better error messages, but risk of infinite loops if not careful.
  • Error-productions: grammar is augmented with productions matching common known mistakes, so the parser can directly recognize and report them.
  • Global correction: computes a minimal sequence of edits (insertions/deletions) to transform the erroneous input into a valid string — theoretically ideal but too expensive for practical compilers.

Example

Missing semicolon: x = a + b (no ;) → panic-mode recovery skips tokens until it finds the next ; or } and resumes parsing from there, reporting "expected ';'".

📌 CSS Frequency: Moderate — usually a conceptual/short-note question listing the four recovery strategies.

Model Answer (short)

"A parser must detect and recover from syntax errors so compilation can proceed and report further errors. Panic-mode recovery skips input until a synchronizing token; phrase-level recovery performs local token insertion/deletion/replacement; error productions bake anticipated mistakes directly into the grammar; and global correction computes the minimal edit distance to a valid program, though it is rarely used in practice due to cost."

📜 Past-Paper Questions & MCQs (CSS 2016–2026)

2017 · Paper-I · Q7(a) · 8 marks

Q. The handling of syntax errors involves the use of the parser and the lexical analyser. Comment on its functioning.

Ans.

  • Lexical analyser: detects illegal characters and malformed tokens (for example 12abc) and reports them with the line number. It recovers by skipping the bad characters (panic mode) and continues.
  • Parser: detects errors in structure, such as a missing semicolon, an unbalanced bracket or a statement in the wrong place. A parser should report the error clearly, recover quickly and not produce false errors.
  • Recovery strategies:
    • Panic mode: discard tokens until a synchronising token (such as ; or }) is found
    • Phrase-level recovery: a local fix such as inserting a missing semicolon
    • Error productions: add grammar rules for common mistakes
    • Global correction: find the minimum change that makes the program valid (costly)
  • Prefix property: LL and LR parsers detect an error as soon as the token prefix cannot lead to a valid program.