Haskell - 如何最好地表示编程语言的语法?

发布于 2024-07-15 04:17:04 字数 203 浏览 8 评论 0原文

我一直在研究 Haskell,我很想在其中编写一个编译器(作为学习练习),因为它的许多固有功能可以很容易地应用于编译器(特别是递归下降编译器)。

我不太明白的是如何以 Haskell 式的方式表示语言的语法。 我的第一个想法是使用递归数据类型定义,但我不知道如何使用它们来匹配语言中的关键字(“if”)。

非常感谢

皮特的想法和建议

I've been looking at Haskell and I'd quite like to write a compiler in it (as a learning exercise), since a lot of its innate features can be readily applied to a compiler (particularly a recursive descent compiler).

What I can't quite get my head around is how to represent a language's grammar in a Haskell-ian way. My first thought was to use recursive data type definitions, but I can't see how I use them to match against keywords in the language ("if") for example.

Thoughts and suggestions greatly appreciated,

Pete

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城泪 2024-07-22 04:17:04

您可以使用相互递归的代数数据类型来表示程序,并使用解析组合器来解析程序。 有百万种口味; 您可以在我的课程安排中找到三篇有用的教程论文2009 年 3 月 23 日星期一。他们是

Hutton 和 Meijer 论文是最短、最简单的,但它使用了单子,这对于业余爱好者来说并不明显。 然而,他们有一个非常好的表达式语法和解析器。 如果您还没有了解 monad,Fokker 的教程就是您的最佳选择。

You represent programs using mutually recursive algebraic data types, and to parse programs you use parsing combinators. There are a million flavors; you will find three helpful tutorial papers on the schedule for my class for Monday, March 23, 2009. They are

The Hutton and Meijer paper is the shortest and simplest, but it uses monads, which are not obvious to the amateur. However they have a very nice grammar of and parser for expressions. If you don't grok monads yet, Fokker's tutorial is the one.

你与昨日 2024-07-22 04:17:04

递归数据类型适合于此。 例如,给定语言:

expr ::= var
      |  "true"
      |  "false"
      |  "if" expr "then" expr "else" expr
      |  "(" expr ")"

该语言的示例表达式将是:

if true then x else (if false then y else true)

您的 Haskell 数据类型将如下所示:

data Expr = Var String
          | Lit Bool
          | If Expr Expr Expr

然后,您的解析器会注意将例如 x 翻译为 Var " x”,和 trueLit True 等。即:

parse "if x then false else true" 
  ==  If (Var "x") (Lit False) (Lit True)

为了编写解析器,您可以使用 Norman 的答案中提到的技术或使用 < a href="http://legacy.cs.uu.nl/daan/parsec.html" rel="noreferrer">Parsec 或使用解析器生成器,例如 快乐

A recursive data type is fine for this. For example, given the language:

expr ::= var
      |  "true"
      |  "false"
      |  "if" expr "then" expr "else" expr
      |  "(" expr ")"

an example expression in this language would be:

if true then x else (if false then y else true)

Your Haskell data type would look something like this:

data Expr = Var String
          | Lit Bool
          | If Expr Expr Expr

Your parser then takes care to translate, e.g., x into Var "x", and true into Lit True, etc. I.e.:

parse "if x then false else true" 
  ==  If (Var "x") (Lit False) (Lit True)

For writing parsers you can roll your own using the techniques mentioned in Norman's answer, or using Parsec or use parser generators like Happy.

趴在窗边数星星i 2024-07-22 04:17:04

也许你可以看看一些现实世界的项目,看看他们是如何做到的?

不到一周前,Language-Python 项目是 Haskell-Cafe 邮件列表。 这是一个在 Haskell 中实现的 Python 解析器,使用 Happy 解析器生成器和 Alex 词法分析器生成器。

当然,还有 Pugs,它是 Haskell 中的 Perl 6(Perl 6 的第一个实现,符合 Perl 6 规范的重要子集)。

Maybe you can look at some real-world projects to see how they do it?

Less than a week ago, the Language-Python project was announced on the Haskell-Cafe mailinglist. It's a Python parser implemented in Haskell, using the Happy parser generator and Alex lexer generator.

And of course, there is Pugs, an implementation of Perl 6 in Haskell (the first implementation of Perl 6 that conforms to a significant subset of the Perl 6 specification).

萌能量女王 2024-07-22 04:17:04

我无法从你问题的语气判断这是否是你第一次尝试编写编译器,或者你之前是否编写过编译器并且正在寻找特定于 Haskell 的建议。 如果您已经是编译器专家,那么我提供的一点建议是没有帮助的。 :)

编程语言语法通常以 BNF 形式 表示,它可以被 Yacc 或 Bison 等工具用来解析源代码。 我不知道这是否算作 Haskell 式的方法,但这是我听说过的唯一方法。 通过一些挖掘,您可能可以找到一个从 BNF 语法生成 Haskell 代码的工具; 我发现这个工具声称能够做到那。

通过 Google 快速搜索,找到了这个 Haskell 的 BNF 语法,并且如果您想为 Haskell 编写一个编译器(也许您想在 Haskell 中编写一个 Haskell 编译器?),那么可能还有其他的编译器。C 和 Java 的 BNF 语法似乎很流行。

最后,如果您正在寻找一本有关编译器设计的书,经典文本是《The Dragon Book》 ”

I can't tell from the tone of your question whether this is the first time you're attempting to write a compiler, or if you've written compilers before and are looking for advice specific to Haskell. If you're already a compiler guru, what little advice I have to offer isn't going to help. :)

Programming language grammars are commonly represented in BNF form, which can be used by tools like Yacc or Bison to parse source code. I don't know if this counts as a Haskell-ian way to do it, but it's the only way that I've heard of. With some digging around you can probably dig up a tool to generate Haskell code from a BNF grammar; I found this tool which claims to be able to do that.

A quick Google search turned up this BNF grammar for Haskell, and there are probably others out there, in case you want to write a compiler for Haskell (maybe you'd like to write a Haskell compiler in Haskell?) BNF grammars for C and Java seem to be popular.

Finally, if you're looking for a book about compiler design, the classic text is "The Dragon Book".

肤浅与狂妄 2024-07-22 04:17:04

不幸的是,没有 ANTLR 的 Haskell 语法,但也许您可以使用上面引用的链接来创建一个。 ANTLR 是一个很棒的基于 Java 的解析器生成器。

Steve Yegge 有一篇关于编写编译器的精彩 博客,如果你需要更多动力。 这很有趣。

Unfortunately there's no Haskell grammar for ANTLR, but perhaps you can use the link cited above to create one. ANTLR is a great Java-based parser generator.

Steve Yegge has a nice blog about writing compilers, if you need more motivation. It's entertaining.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文