且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Parsec vs Yacc/Bison/Antlr:为什么以及何时使用 Parsec?

更新时间:2022-05-19 00:04:35

您列出的工具之间的主要区别之一是 ANTLR、Bison 和他们的朋友是解析器生成器,而 Parsec 是一个解析器组合器库.

One of the main differences between the tools you listed, is that ANTLR, Bison and their friends are parser generators, whereas Parsec is a parser combinator library.

解析器生成器读取语法的描述并吐出解析器.通常不可能将现有的语法组合成一个新的语法,当然也不可能将两个现有的生成解析器组合成一个新的解析器.

A parser generator reads in a description of a grammar and spits out a parser. It is generally not possible to combine existing grammars into a new grammar, and it is certainly not possible to combine two existing generated parsers into a new parser.

解析器组合器 OTOH 但是 将现有的解析器组合成新的解析器.通常,解析器组合器库附带几个简单的内置解析器,可以解析空字符串或单个字符,并且附带一组组合器,这些组合器采用 1 个或多个解析器并返回一个新的解析器,例如, 解析原始解析器的序列(例如,您可以将 d 解析器和 o 解析器组合起来形成 do 解析器),交替原始解析器(例如 0 解析器和 1 解析器到 0|1 解析器)或多次解析原始解析(重复).

A parser combinator OTOH does nothing but combine existing parsers into new parsers. Usually, a parser combinator library ships with a couple of trivial built-in parsers that can parse the empty string or a single character, and it ships with a set of combinators that take 1 or more parsers and return a new one that, for example, parses the sequence of the original parsers (e.g. you can combine a d parser and an o parser to form a do parser), the alternation of the original parsers (e.g. a 0 parser and a 1 parser to a 0|1 parser) or parses the original parse multiple times (repetetion).

这意味着,例如,您可以将现有的 Java 解析器和 HTML 的现有解析器组合成 JSP 的解析器.

What this means is that you could, for example, take an existing parser for Java and an existing parser for HTML and combine them into a parser for JSP.

大多数解析器生成器不支持这一点,或者仅以有限的方式支持它.解析器组合器 OTOH支持这个,没有别的.

Most parser generators don't support this, or only support it in a limited way. Parser combinators OTOH only support this and nothing else.