File formats specify how data is encoded for persistent storage. They cannot be formalized as context-free grammars since their specifications include context-sensitive patterns such as the random access pattern and the type-length-value pattern. We propose a new grammar mechanism called Interval Parsing Grammars IPGs) for file format specifications. An IPG attaches to every nonterminal/terminal an interval, which specifies the range of input the nonterminal/terminal consumes. By connecting intervals and attributes, the context-sensitive patterns in file formats can be well handled. In this paper, we formalize IPGs' syntax as well as its semantics, and its semantics naturally leads to a parser generator that generates a recursive-descent parser from an IPG. In general, IPGs are declarative, modular, and enable termination checking. We have used IPGs to specify a number of file formats including ZIP, ELF, GIF, PE, and part of PDF; we have also evaluated the performance of the generated parsers.
翻译:文件格式规定了数据如何编码以进行持久存储。由于文件格式的规范包含上下文相关的模式(如随机访问模式和类型-长度-值模式),因此无法将其形式化为上下文无关文法。本文提出了一种新的文法机制——区间解析文法(Interval Parsing Grammars, IPGs),用于文件格式的规范。IPG为每个非终结符/终结符附加一个区间,该区间指定了该非终结符/终结符所消耗的输入范围。通过连接区间与属性,文件格式中的上下文相关模式能够得到妥善处理。本文形式化了IPG的语法及其语义,其语义自然地引出一个解析器生成器,该生成器可根据IPG生成递归下降解析器。总体上,IPG具有声明性、模块化特性,并支持终止性检查。我们已使用IPG规范了多种文件格式,包括ZIP、ELF、GIF、PE以及部分PDF格式,并对所生成解析器的性能进行了评估。