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

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

问题描述

我是Haskell和Parsec的新手。在阅读第16章使用真实世界Haskell 之后,我的一个问题出现在我的记:为什么当比其他的解析器生成类似的Yacc秒差距更好的/野牛/ ANTLR的



我的理解是,秒差距创造写作解析器的一个很好的DSL和Haskell做呢?非常容易和表达。但解析是这样一种标准/流行的技术,值得自己的语言,输出到多种目标语言。那么,我们什么时候应该使用Parsec而不是比如说从Bison / Antlr生成Haskell代码呢?

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

BTW:我的问题与这一个,但没有得到满意的回答。

解决方案

您列出的工具之间的主要区别之一是ANTLR,Bison和他们的朋友是解析器 ,而Parsec是一个解析器 combinator 库。

解析器生成器读入语法描述并吐出解析器。通常不可能将现有的语法组合成一个新的语法,并且将两个现有的生成语法分析器合并到一个新的语法分析器当然是不可能的。



解析器组合器OTOH没有做什么,而是将现有解析器合并到新的解析器中。通常,解析器组合器库附带一对可以解析空字符串或单个字符的简单内置解析器,并附带一组组合器,它们携带一个或多个解析器并返回一个新的解析器,例如,解析原始解析器的序列(例如,可以将 d 解析器和 o 解析器组合成一个 do 解析器),原始解析器的交替(例如 0 解析器和 1 解析器解析为 0 | 1 解析器)或者多次解析原始解析(重复)。

这意味着您可以为现有的Java解析器和现有的HTML解析器组合并将它们组合为JSP解析器。



大多数解析器生成器不支持这一点,或者仅以有限的方式支持它。 Parser combinators 仅限于OTOH 支持此操作,而不是其他操作。


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?

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?

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.

解决方案

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.

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).

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.

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天全站免登陆