问题描述
文本
Text.Parsec
Text.Parsec.ByteString
Text.Parsec.ByteString.Lazy
Text.Parsec.Char
Text.Parsec.Combinator
Text.Parsec.Error
Text.Parsec.Expr
Text.Parsec.Language
Text.Parsec.Perm
文本。 Parsec.Pos
Text.Parsec.Prim
Text.Parsec.String
Text.Parsec.Token
ParserCombinators
Text.ParserCombinators.Parsec
Text。 ParserCombinators.Parsec.Char
Text.ParserCombinators.Parsec.Combinator
Text.ParserCombinators.Parsec.Error
Text.ParserCombinators.Parsec.Expr
Text.ParserCombinators.Parsec.Language
Text.ParserCombinators.Parsec.Perm
Text.ParserCombinators.Parsec.Pos
Text.ParserCombinators.Parsec.Prim
Text.ParserCombinators.Parsec.Token
它们是否一样?
目前有两种广泛使用的专业Parsec 2和
Parsec 3的版本。
我的建议只是使用Parsec 3的最新版本。但是如果你想要
进行有意识的选择,继续阅读。
Parsec 3中的新功能
Monad Transformer
Parsec 3引入了一个monad转换器,
ParsecT
,可用于将解析与其他一元效果组合在一起。
Streams
尽管Parsec 2允许您选择令牌类型(当您
需要时将词法分析从解析中分离出来),令牌总是被排列成列表中的
。列表可能不是存储
大文本的最有效的数据结构。
Parsec 3可以使用任意流 - 数据具有列表式
接口的结构。您可以定义自己的流,但Parsec 3还包含基于ByteString(用于 Char
- 基于
解析)的流行
和高效Stream实现,通过 Text.Parsec.ByteString
和
Text.Parsec.ByteString.Lazy
p>
选择Parsec 2的理由
需要更少的扩展名
Parsec 3提供的高级功能并不是免费的:要实现它们
需要几种语言扩展。
这两个版本都不是Haskell-2010 (即都使用扩展),但是
Parsec 2使用的扩展比Parsec 3少,所以任何给定的编译器
都可以编译Parsec 2的机会要高于Parsec 3的那些。
到目前为止,这两个版本都与GHC一起工作,而Parsec 2也是到
使用JHC构建,包括ded作为JHC的标准库之一。
表现
最初(即从3.0版开始)Parsec 3比
Parsec 2慢得多。但是,已完成,
和3.1版本的Parsec 3只比Parsec 2
稍微慢一些(基准:,)。
兼容层
可以在Parsec 3中重新实现所有的Parsec 2 API。这个
兼容层由Parsec 3包在模块层次结构
下提供 Text.ParserCombinators.Parsec
(与Parsec 2相同的层次结构)
,而新的Parsec 3 API可在 Text.Parsec
层次结构。
这意味着您可以使用Parsec 3作为Parsec 2的直接替代品。
Text
Text.Parsec
Text.Parsec.ByteString
Text.Parsec.ByteString.Lazy
Text.Parsec.Char
Text.Parsec.Combinator
Text.Parsec.Error
Text.Parsec.Expr
Text.Parsec.Language
Text.Parsec.Perm
Text.Parsec.Pos
Text.Parsec.Prim
Text.Parsec.String
Text.Parsec.Token
ParserCombinators
Text.ParserCombinators.Parsec
Text.ParserCombinators.Parsec.Char
Text.ParserCombinators.Parsec.Combinator
Text.ParserCombinators.Parsec.Error
Text.ParserCombinators.Parsec.Expr
Text.ParserCombinators.Parsec.Language
Text.ParserCombinators.Parsec.Perm
Text.ParserCombinators.Parsec.Pos
Text.ParserCombinators.Parsec.Prim
Text.ParserCombinators.Parsec.Token
Are they the same?
At the moment there are two widely used major versions of Parsec, Parsec 2 andParsec 3.
My advice is simply to use the latest release of Parsec 3. But if you want tomake a conscious choice, read on.
New in Parsec 3
Monad Transformer
Parsec 3 introduces a monad transformer, ParsecT
, which can be used to combineparsing with other monadic effects.
Streams
Although Parsec 2 lets you to choose the token type (which is useful when youwant to separate lexical analysis from the parsing), the tokens are alwaysarranged into lists. List may be not the most efficient data structure to storelarge texts.
Parsec 3 can work with arbitrary streams -- data structures with list-likeinterface. You can define your own streams, but Parsec 3 also includes a popularand efficient Stream implementation based on ByteString (for Char
-basedparsing), exposed through the modules Text.Parsec.ByteString
andText.Parsec.ByteString.Lazy
.
Reasons to prefer Parsec 2
Less extensions required
Advanced features provided by Parsec 3 do not come for free: to implement themseveral language extensions are required.
Neither of the two versions is Haskell-2010 (i.e. both use extensions), butParsec 2 uses less extensions than Parsec 3, so chances that any given compilercan compile Parsec 2 are higher than those for Parsec 3.
By this time both versions work with GHC, while Parsec 2 is also reported tobuild with JHC and is included as one of the JHC's standard libraries.
Performance
Originally (i.e. as of 3.0 version) Parsec 3 was considerably slower thanParsec 2. However, work on improving Parsec 3 performance has been done,and as of version 3.1 Parsec 3 is only slightly slower than Parsec 2(benchmarks: 1, 2).
Compatibility layer
It has been possible to "reimplement" all of the Parsec 2 API in Parsec 3. Thiscompatibility layer is provided by the Parsec 3 package under the module hierarchyText.ParserCombinators.Parsec
(the same hierarchy which is used by Parsec 2),while the new Parsec 3 API is available under the Text.Parsec
hierarchy.
This means that you can use Parsec 3 as a drop-in replacement for Parsec 2.
这篇关于Text.ParserCombinators.Parsec和Text.Parsec有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!