为什么在函数式语言中编写一个编译器更容易? [英] Why is writing a compiler in a functional language easier?

查看:198
本文介绍了为什么在函数式语言中编写一个编译器更容易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想这个问题很长,但真的找不到在谷歌的答案,以及一个类似的问题在Stackoverflow。如果有重复,我很抱歉。

I've been thinking of this question very long, but really couldn't find the answer on Google as well a similar question on Stackoverflow. If there is a duplicate, I'm sorry for that.

很多人似乎说,编写编译器和其他语言工具在功能语言如OCaml和Haskell是更有效率和更容易,然后用命令式语言写它们。

A lot of people seem to say that writing compilers and other language tools in functional languages such as OCaml and Haskell is much more efficient and easier then writing them in imperative languages.

这是真的吗?如果是这样 - 为什么它是如此高效和容易地写在功能语言而不是命令式语言,如C?另外 - 在某些低级语言如C

Is this true? And if so -- why is it so efficient and easy to write them in functional languages instead of in an imperative language, like C? Also -- isn't a language tool in a functional language slower then in some low-level language like C?

推荐答案

时间一个编译器工作很多与树。源代码被解析为语法树。然后可以将该树转换为具有类型注释的另一个树以执行类型检查。现在你可以将该树转换为只包含核心语言元素的树(将语法糖样符号转换为未定义的形式)。现在,您可以执行各种优化,这些优化基本上是树上的变换。之后,你可能会创建一个正常形式的树,然后迭代该树以创建目标(程序集)代码。

Often times a compiler works a lot with trees. The source code is parsed into a syntax tree. That tree might then be transformed into another tree with type annotations to perform type checking. Now you might convert that tree into a tree only containing core language elements (converting syntactic sugar-like notations into an unsugared form). Now you might perform various optimizations that are basically transformations on the tree. After that you would probably create a tree in some normal form and then iterate over that tree to create the target (assembly) code.

功能语言具有模式匹配和良好的支持高效的递归,这使得它很容易与树的工作,这就是为什么他们通常被认为是编写编译器的良好的语言。

Functional language have features like pattern-matching and good support for efficient recursion, which make it easy to work with trees, so that's why they're generally considered good languages for writing compilers.

这篇关于为什么在函数式语言中编写一个编译器更容易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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