Lexers and parsers are typically defined separately and connected by a token stream. This separate definition is important for modularity and reduces the potential for parsing ambiguity. However, materializing tokens as data structures and case-switching on tokens comes with a cost. We show how to fuse separately-defined lexers and parsers, drastically improving performance without compromising modularity or increasing ambiguity. We propose a deterministic variant of Greibach Normal Form that ensures deterministic parsing with a single token of lookahead and makes fusion strikingly simple, and prove that normalizing context free expressions into the deterministic normal form is semantics-preserving. Our staged parser combinator library, flap, provides a standard interface, but generates specialized token-free code that runs two to six times faster than ocamlyacc on a range of benchmarks.
翻译:词法分析器和语法分析器通常是分别定义的,并由标记流连接。这种单独的定义对于模块化非常重要,并减少了解析歧义的可能性。然而,将标记实现为数据结构并在标记上进行case-switching是有代价的。我们展示了如何融合分别定义的词法分析器和语法分析器,显着提高了性能,而不会损害模块化或增加歧义。我们提出了Greibach正常形式的确定性变体,确保确定性解析单个标记并使融合变得非常简单,并证明将上下文无关表达式归一化为确定性正常形式是保留语义的。我们的分阶段解析组合器库flap提供标准接口,但生成专门的无标记代码,在各种基准测试中运行速度比ocamlyacc快两到六倍。