We present the squirrel parser, a PEG packrat parser that directly handles all forms of left recursion with optimal error recovery, while maintaining linear time complexity in the length of the input even in the presence of an arbitrary number of errors. Traditional approaches to handling left recursion in a recursive descent parser require grammar rewriting or complex algorithmic extensions. We derive a minimal algorithm from first principles: cycle detection via per-position state tracking and $O(1)$-per-LR-cycle communication from descendant to ancestor recursion frames, and fixed-point search via iterative expansion. For error recovery, we derived a set of four axioms and twelve constraints that must be imposed upon an optimal error recovery design to ensure completeness, correctness, optimality of performance, and intuitiveness of behavior. We utilized a constraint satisfaction mechanism to search the space of all possibilities, arriving at a provably optimal and robust error recovery strategy that maintains perfect performance linearity.
翻译:本文提出松鼠解析器,这是一种能够直接处理所有形式左递归并具备最优错误恢复能力的PEG记忆包解析器,即使在任意数量错误存在的情况下仍能保持输入长度级的线性时间复杂度。传统递归下降解析器处理左递归的方法需要语法重写或复杂的算法扩展。我们从基本原理推导出一个最小化算法:通过逐位置状态追踪进行环检测,建立后代递归帧到祖先递归帧的每左递归周期O(1)通信机制,并采用迭代展开的定点搜索策略。针对错误恢复问题,我们推导出包含四条公理与十二条约束的集合,这些约束必须施加于最优错误恢复设计,以确保其完备性、正确性、性能最优性及行为直观性。我们采用约束满足机制遍历所有可能性空间,最终得到可证明最优且鲁棒的错误恢复策略,该策略始终保持完美的线性性能表现。