paperbot · PL 论文追踪

全部论文 待读 收藏

P2 Parsing with zippers (functional pearl)

使用 zipper 的解析(函数式珍珠)
ICFP 4(ICFP)2020匹配度 1/10 · 论文属于函数式编程与解析算法(PwD/zipper)的 PL 理论研究,未涉及 AI/Agent、静态分析、浮点程序分析或贝叶斯程序分析,与用户画像的核心交叉方向基本无关。引用 5 inbox
Pierce Darragh, Michael D. Adams
dblp: 2026-09-10crossref: 2026-09-10

AI 速览

一句话:将 zipper 从树推广到上下文无关文法,通过挂起遍历状态消除 Parsing with Derivatives 的重复遍历并显著提速。

Parsing with Derivatives(PwD)为任意上下文无关文法(CFG)提供了一种优雅的解析方法,它把 Brzozowski 正则表达式导数理论扩展到带惰性、记忆化和不动点的 CFG,但算法会反复遍历文法的某些部分,因而效率不高。本文提出用 zipper 挂起遍历状态:后续求导时可从上次中断处继续,无需重新遍历已访问部分。由于传统 zipper 面向树,而 CFG 包含共享区域、环和分支选择,作者将 Huet 的 zipper 从树推广到 CFG。所得算法简洁高效,导数函数仅 31 行 OCaml,速度达原始 PwD 的 6,500 倍、优化版 PwD 的 3.24 倍。

问题
PwD 虽能优雅地解析任意 CFG,却因算法反复遍历文法某些部分而效率低下。
方法
作者将遍历状态挂起在 zipper 中,并把 Huet 的树 zipper 推广到具有共享、环和选择的 CFG,使后续求导无需重新遍历。
评估
在 OCaml 中实现 31 行的导数函数,相比原始 PwD 提速 6,500 倍,相比优化版 PwD 提速 3.24 倍。
原文摘要(Abstract)

Parsing with Derivatives (PwD) is an elegant approach to parsing context-free grammars (CFGs). It takes the equational theory behind Brzozowski's derivative for regular expressions and augments that theory with laziness, memoization, and fixed points. The result is a simple parser for arbitrary CFGs. Although recent work improved the performance of PwD, it remains inefficient due to the algorithm repeatedly traversing some parts of the grammar. In this functional pearl, we show how to avoid this inefficiency by suspending the state of the traversal in a zipper. When subsequent derivatives are taken, we can resume the traversal from where we left off without retraversing already traversed parts of the grammar. However, the original zipper is designed for use with trees, and we want to parse CFGs. CFGs can include shared regions, cycles, and choices between alternates, which makes them incompatible with the traditional tree model for zippers. This paper develops a generalization of zippers to properly handle these additional features. Just as PwD generalized Brzozowski's derivatives from regular expressions to CFGs, we generalize Huet's zippers from trees to CFGs. Abstract The resulting parsing algorithm is concise and efficient: it takes only 31 lines of OCaml code to implement the derivative function but performs 6,500 times faster than the original PwD and 3.24 times faster than the optimized implementation of PwD.

链接与引用

DOI 原文 · PDF(开放获取) · DBLP

BibTeX
@article{DarraghA20,
  title = {Parsing with zippers (functional pearl)},
  author = {Pierce Darragh and Michael D. Adams},
  journal = {Proceedings of the ACM on Programming Languages},
  volume = {4},
  number = {ICFP},
  year = {2020},
  doi = {10.1145/3408990}
}