◐ Off-By-One · answer catalog

20261005-incremental-grammar-repair

1 answer(s)pythonpython3

20261005-incremental-grammar-repair

📦 Source in repository (JSON)

Answer

Done. Here's what I built and verified.

Summary

The environment contained no starter code or tests, so I implemented the incremental parser from scratch and produced a complete, self-contained solution document.

Artifacts in ~: - incremental_parser.py — dependency-free implementation (~725 lines) - test_incremental_parser.py — 17-test verification suite - demo.py — evidence run - SOLUTION.md — the requested markdown (title, root-cause analysis, exact fix, verification, full embedded code)

Root causes addressed

  1. Ambiguous precedence — all binary ops on one level can't be a single tree; the chart packs multiple alternatives per span.
  2. >> vs nested generics — lexing > individually (no >> token) lets List<List<int>> close.
  3. Indentation — byte-exact INDENT/DEDENT lexer that ignores blank/comment lines and bracket continuations.
  4. Distant invalidation — reuse a chart cell only when its exact token subsequence is unchanged, shifted by token delta; recompute every overlapping cell.
  5. Epsilon on binary boundaries — if_rest -> cond_suite else_opt broke plain CYK; fixed with epsilon-aware unary closure.
  6. Stable identity — structural hash-consing of forest nodes.
  7. Malformed input — longest-prefix parse plus deterministic ERROR wrapper.

Verification results

Ran 17 tests in 0.158s
OK

Including a fuzz test of 120 random byte patches (insertions/deletions/replacements) across three sources, each matching a fresh full reparse structurally. Demo shows the affected interval is exactly the edited bytes (38, 47) with 237 cells reused, 169 recomputed, and all 54 unaffected first-statement node identities preserved. The code is Python 3.11-compatible (uses only __future__, dataclasses, typing).

Evidence & signatures

# Evidence
- Problem class: 20261005-incremental-grammar-repair
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-10-05T10:12:25.173Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Build a Python incremental parser for a programming language with ambiguous expression precedence, nested generics, and indentation-sensitive blocks, where edits arrive as byte-range patches and may invalidate distant parse decisions. Maintain a packed parse forest with stable node identities, report the smallest affected source interval, and recover deterministically from malformed input without reparsing unaffected regions.", "environment": "python3", "language": "python", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "20261005-incremental-grammar-repair", "provider": "openrouter", "solved_at": "2026-10-05T10:12:25.173Z", "version": "3.11"}
Generated from the verified corpus · MIT licensedBack to the catalog