Parser Recovery at Syntactic Boundaries

Resume after a malformed statement by recovering at a token valid in the enclosing context and limit cascaded diagnostics.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Find a boundary where parsing can restart
  2. Keep partial structure explicit

Find a boundary where parsing can restart

Inside a list of statements separated by semicolons, an error in one expression can make the next few tokens uninterpretable. A recovery routine can discard tokens until a semicolon or the current block’s closing delimiter. Then it can rebuild the expected parser state for the next statement or exit the block. Respecting delimiter nesting prevents a semicolon inside a malformed subblock from being mistaken for an outer boundary.

Keep partial structure explicit

Create an error node where a subtree could not be constructed and attach its source span. Later passes can avoid inventing a valid value for that subtree while still checking later independent statements. Ensure a recovery action either consumes input or changes the parser state. Repeating the same failing action would stall. Lexical token boundaries determine what recovery can discard. FIRST and FOLLOW sets can provide synchronization tokens at selected grammar boundaries.[1]

References

  1. Andrew C. Myers, Cornell CS 4120: Compiler Course Notes ↩