We present ZipLex, a verified framework for invertible linear-time lexical analysis following the longest match semantics. Unlike past verified lexers that focus only on satisfying the semantics of regular expressions and the longest match property, ZipLex also guarantees that lexing and printing are mutual inverses. Thanks to verified memoization, it also ensures that the lexical analysis of a string is linear in the size of the string. Our design and implementation rely on two sets of ideas: (1) a new abstraction of token sequences that captures the separability of tokens in a sequence while supporting their efficient manipulation, and (2) a combination of verified data structures and optimizations, including Huet's zippers and memoization with a verified imperative hash table. We implemented and verified ZipLex using the Stainless deductive verifier for Scala. Our evaluation demonstrates that ZipLex supports realistic applications such as JSON processing and lexers of programming languages, and behaves linearly even in cases that make flex-style approaches quadratic. ZipLex is two orders of magnitude faster than Verbatim++, showing that verified invertibility and linear-time algorithms can be developed without prohibitive cost. Compared to Coqlex, ZipLex also offers linear (instead of quadratic) time lexing, and is the first lexer that comes with invertibility proofs for printing token sequences.
翻译:本文提出ZipLex,一个遵循最长匹配语义的可验证线性时间可逆词法分析框架。与以往仅关注满足正则表达式语义和最长匹配特性的验证型词法分析器不同,ZipLex还保证词法分析与打印操作互为逆过程。通过验证的记忆化技术,该框架进一步确保字符串的词法分析时间复杂度与字符串长度呈线性关系。我们的设计与实现基于两组核心理念:(1) 提出一种新的词符序列抽象方法,既能捕捉序列中词符的可分离性,又能支持其高效操作;(2)结合经过验证的数据结构与优化技术,包括Huet拉链结构以及采用验证型命令式哈希表实现的记忆化机制。我们使用Scala语言的Stainless演绎验证器实现并验证了ZipLex。评估结果表明,ZipLex能够支持JSON处理及编程语言词法分析器等实际应用场景,即使在导致flex类方法出现二次复杂度的案例中仍保持线性行为。ZipLex的运行速度比Verbatim++快两个数量级,证明可验证的可逆性与线性时间算法可以在可接受的成本内实现。与Coqlex相比,ZipLex提供线性(而非二次)时间复杂度的词法分析,并且是首个附带词符序列打印可逆性证明的词法分析器。