与使用解析器组合器相比,使用像happy这样的解析器生成器的优点是什么? [英] What is the advantage of using a parser generator like happy as opposed to using parser combinators?

查看:140
本文介绍了与使用解析器组合器相比,使用像happy这样的解析器生成器的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了学习如何编写和解析上下文无关语法,我想选择一个工具。对于Haskell,有两个大的选项:Happy,它从一个语法描述中生成一个解析器,* Parsec,它允许你直接在Haskell中编写一个解析器。



这两种方法的优点是什么?

外部vs内部DSL



Happy的解析器规范格式是一个外部DSL,而使用Parsec时,您可以在定义解析器时使用Haskell的全部功能。这意味着你可以编写函数来生成解析器,使用Template Haskell等等。

优先规则 使用Happy可以使用 precedences 来简化您的语法,而使用Parsec时,您必须正确地嵌套语法规则。因此,在Parsec中更改运算符的优先级是非常单调乏味的。



静态检查

Happy会在编译时警告你语法中的含糊不清。 (尽管它告诉你它们在哪里并不是很好)。使用Parsec,直到解析器在运行时失败,你才会得到警告。


To learn how to write and parse a context-free grammar I want to choose a tool. For Haskell, there are two big options: Happy, which generates a parser from a grammar description and *Parsec, which allows you to directly code a parser in Haskell.

What are the (dis)advantages of either approach?

解决方案

External vs internal DSL

The parser specification format for Happy is an external DSL, whereas with Parsec you have the full power of Haskell available when defining your parsers. This means that you can for example write functions to generate parsers, use Template Haskell and so on.

Precedence rules

With Happy, you can use precedences to simplify your grammar, whereas with Parsec you have to nest the grammar rules correctly yourself. Changing the precedence of an operator is therefore much more tedious in Parsec.

Static checking

Happy will warn you about ambiguities in your grammar at compile time. (Though it's not great at telling you where they are.) With Parsec, you get no warning until your parser fails at run time.

这篇关于与使用解析器组合器相比,使用像happy这样的解析器生成器的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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