Parsec vs Yacc/Bison/Antlr:为什么以及何时使用 Parsec? [英] Parsec vs Yacc/Bison/Antlr: Why and when to use Parsec?

查看:26
本文介绍了Parsec vs Yacc/Bison/Antlr:为什么以及何时使用 Parsec?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Haskell 和 Parsec 的新手.看完第16章使用现实世界Haskell的Parsec后,我的脑海中出现了一个问题:为什么以及何时 Parsec 比 Yacc/Bison/Antlr 等其他解析器生成器更好?

I'm new to Haskell and Parsec. After reading Chapter 16 Using Parsec of Real World Haskell, a question appeared in my mind: Why and when is Parsec better than other parser generators like Yacc/Bison/Antlr?

我的理解是 Parsec 创建了一个很好的编写解析器的 DSL,而 Haskell 使它非常容易和富有表现力.但是解析是一种标准/流行的技术,值得拥有自己的语言,输出到多种目标语言.那么我们什么时候应该使用 Parsec 而不是从 Bison/Antlr 生成 Haskell 代码?

My understanding is that Parsec creates a nice DSL of writing parsers and Haskell makes it very easy and expressive. But parsing is such a standard/popular technology that deserves its own language, which outputs to multiple target languages. So when shall we use Parsec instead of, say, generating Haskell code from Bison/Antlr?

这个问题可能会超出技术范围,进入行业实践领域.从头开始编写解析器时,与 Bison/Antlr 或类似的东西相比,选择 Haskell/Parsec 有什么好处?

This question might go a little beyond technology, and into the realm of industry practice. When writing a parser from scratch, what's the benefit of picking up Haskell/Parsec compared to Bison/Antlr or something similar?

顺便说一句:我的问题与这个非常相似,但是那里没有得到满意的答复.

BTW: my question is quite similar to this one but wasn't answered satisfactorily there.

推荐答案

您列出的工具之间的主要区别之一是 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.

这篇关于Parsec vs Yacc/Bison/Antlr:为什么以及何时使用 Parsec?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆