arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2209.08383cs.FLcs.PL
收藏

实用的LR语法分析器生成

Practical LR Parser Generation

Joe Zimmerman

摘要:

解析是现代编译器中的一个基本构建块,对于工业程序设计语言来说,它是一个令人惊讶的复杂任务。存在自动生成解析器的已知方法,但是普遍的共识是自动解析器生成对于真正的程序设计语言是不实际的:LR/LALR解析器被认为在它们支持的语法方面限制太多,并且LR解析器在实践中通常被认为效率太低。因此,几乎所有现代语言都使用手工编写的递归下降解析器,这是一个冗长且容易出错的过程,大大增加了新程序设计语言开发的障碍。在这项工作中,我们证明,相反的普遍共识,我们可以有两全其美:一个非常普遍的,实用的类的语法-一个严格的超集克努特的规范LR -我们可以自动生成解析器,并由此产生的解析器代码,以及生成过程本身,是非常有效的。这一进展依赖于几个新的想法,包括新的自动机优化程序;一个新的语法转换(“CPS”);每符号属性;递归下降动作;和规范LR解析的扩展,我们称之为XLR,它赋予移位/归约解析器有界非确定性选择的能力。有了这些成分,我们可以为几乎所有的程序设计语言自动生成高效的解析器,这些语言直观上很容易解析--我们通过在一个名为langcc的新软件工具中实现新算法,并在Golang 1.17.8和Python 3.9.12的语法规范上运行它们来实验性地支持这一说法。该工具自动处理这两种语言,并且生成的代码在标准代码库上运行时,分别比Golang的相应手写解析器快1.2倍,比CPython解析器快4.3倍。

英文摘要:

Parsing is a fundamental building block in modern compilers, and for industrial programming languages, it is a surprisingly involved task. There are known approaches to generate parsers automatically, but the prevailing consensus is that automatic parser generation is not practical for real programming languages: LR/LALR parsers are considered to be far too restrictive in the grammars they support, and LR parsers are often considered too inefficient in practice. As a result, virtually all modern languages use recursive-descent parsers written by hand, a lengthy and error-prone process that dramatically increases the barrier to new programming language development. In this work we demonstrate that, contrary to the prevailing consensus, we can have the best of both worlds: for a very general, practical class of grammars -- a strict superset of Knuth's canonical LR -- we can generate parsers automatically, and the resulting parser code, as well as the generation procedure itself, is highly efficient. This advance relies on several new ideas, including novel automata optimization procedures; a new grammar transformation ("CPS"); per-symbol attributes; recursive-descent actions; and an extension of canonical LR parsing, which we refer to as XLR, which endows shift/reduce parsers with the power of bounded nondeterministic choice. With these ingredients, we can automatically generate efficient parsers for virtually all programming languages that are intuitively easy to parse -- a claim we support experimentally, by implementing the new algorithms in a new software tool called langcc, and running them on syntax specifications for Golang 1.17.8 and Python 3.9.12. The tool handles both languages automatically, and the generated code, when run on standard codebases, is 1.2x faster than the corresponding hand-written parser for Golang, and 4.3x faster than the CPython parser, respectively.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑