C 或 C++写一个编译器?

发布于 2024-10-10 14:49:57 字数 393 浏览 6 评论 0原文

我想为自定义标记语言编写一个编译器,我想获得最佳性能,并且我还想拥有良好的可扩展设计。

多范式编程语言(C++)更适合实现现代设计模式,但我认为这会稍微降低性能(例如 RTTI)或更少可能使 C 成为更好的选择。

我想知道如果有人想要创建一个快速、高效且设计良好的现代编译器(在作为软件遵守现代软件工程原理的意义上),最好的语言(C、C++ 甚至 Objective C)是什么。

I want to write a compiler for a custom markup language, I want to get optimum performance and I also want to have a good scalable design.

Multi-paradigm programming language (C++) is more suitable to implement modern design patterns, but I think that will degrade performance a little bit (think of RTTI for example) which more or less might make C a better choice.

I wonder what is the best language (C, C++ or even objective C) if someone wants to create a modern compiler (in the sense of complying to modern software engineering principles as a software) that is fast, efficient, and well designed.

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

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

发布评论

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

评论(6

手心的温暖 2024-10-17 14:49:57

C++ 的“昂贵”功能(例如,异常、虚函数、RTTI)在 C 中根本不存在。当您在 C 中模拟它们时,您最终可能会得到至少与它一样昂贵的东西在 C++ 中,但鲜为人知,文档较少等(让我们面对现实:编译器编写者并不愚蠢——虽然可能你可以实现比他们“更好”的功能,但这并不是特别特别可能)。

另一方面,模板(例如)通常可以相对容易地编写比 C 中的实用代码更快的代码。仅举一个明显的例子,使用 std 的 C++ 代码: :sort 通常比使用 qsort 的等效 C 代码快两到三倍。

底线:C++ 程序比用 C 编写的等效程序慢的唯一原因是您决定(无论出于何种原因)编写较慢的代码。常见的原因是简单性和可读性——在大多数情况下,这些比执行速度更重要。尽管如此,使用 C++ 并不一定会带来任何速度损失。是否做一些可能运行得更慢的事情完全取决于您。

The "expensive" features of C++ (e.g., exceptions, virtual functions, RTTI) simply don't exist in C. By the time you simulate them in C, you're likely to end up with something at least as expensive as it is in C++, but less known, less documented, etc. (let's face it: compiler writers aren't stupid -- while it's possible you can implement a feature "better" than them, it's not really particularly likely).

In the other direction, templates (for one example) often make it relatively easy to write code that is considerably faster than is practical in C. Just for one obvious example, C++ code using std::sort will often be two to three times as fast as equivalent C code using qsort.

Bottom line: the only reason for a C++ program to be slower than an equivalent written in C is if you've decided (for whatever reason) to write slower code. Common reasons are simplicity and readability -- and in most cases, those are more important than execution speed. Nonetheless, using C++ doesn't necessarily carry any speed penalty. It's completely up to you to decide whether to do something that might run more slowly.

你曾走过我的故事 2024-10-17 14:49:57

C++ 遵循“按使用量付费”的政策。您不会因为语言选择而看到性能下降;您的应用程序的性能将完全取决于您的实现。

C++ adheres to a "pay only for what you use" policy. You are not going to see performance hits due to the language choice; the performance of your application will be purely dependent upon your implementation.

动听の歌 2024-10-17 14:49:57

你考虑过 OCaml 吗?函数式语言非常适合编译器编写。模式匹配是一种非常有用的构造,并且没有副作用将使并行化变得容易。

OCaml可以编译为本机代码,其性能与C和C++相当。它的标准库有些缺乏,但你实际上不需要编写编译器。

如果您更喜欢 .NET 环境,F# 是一种非常相似的语言。

Have you considered OCaml? Functional languages are well-suited for compiler writing. Pattern matching is an extremely useful construct, and the lack of side effects will make parallelization easy.

OCaml can be compiled to native code, and its performance is comparable to C and C++. Its standard library is somewhat lacking, but you don't really much else to write a compiler.

F# is a very similar language if you prefer a .NET environment.

陌路终见情 2024-10-17 14:49:57

以 C 作为基本语言编写编译器的人通常很明智地在其中的某些部分使用工具。

具体来说,去了解 lex 和 yacc(在它们的免费实现中, flex野牛)。

这个建议几乎肯定适用于您选择的任何其他语言,无论是 C++、Java 还是其他语言。

People who write compilers in C as their basic language usually have the good sense to use tools for certain parts of it.

Specifically, go find out about lex and yacc (in their free implementations, flex and bison).

This advice almost certainly applies to any other language you choose, be it C++, Java or whatever.

多情出卖 2024-10-17 14:49:57

我没有任何链接,但根据我的听说和经验,C/C++ 是一种编写编译器的糟糕语言。首先,您真的需要它具有可扩展性吗?还是现阶段可扩展?特别是对于标记语言?您没有编译 60+ mb 的源代码,所以我认为您实际上并不需要它具有可扩展性。

无论如何,对于我的编程语言,我使用 bison 作为解析器(必须阅读 bison+flex,尽量避免我的语言没有的所有冲突)。然后我使用 C 和 C++ 来编写代码。 C 因为 bison 使用 C,我只是调用一个简单的 C 函数,它创建并填充一个结构来创建一个抽象语法树。然后,当它完成时,它会调用我的 C++ 代码,该代码运行 AST 并生成二进制文件。

标准机器学习被认为非常适合创建一种语言。如果您不使用函数式语言,那么函数式语言是一个不错的选择,因为它符合思维方式(解析可能是从左到右,但您的函数调用不会按该顺序)。所以我建议如果你不使用 bison (或者知道如何使用 C/C++ 和 bison 调用它)。

注意:我尝试编写编译器两次。第一次在 C 中没有野牛,第二次有野牛。毫无疑问,由于野牛为我找到了冲突,而且我并没有注定要在调试领域(事实上,我可能会在编写代码之前尝试找到一种报告冲突的方法),因此我会花费指数级更长的时间这正是野牛所做的)

I dont have any links but from what i hear and from experience C/C++ is a poor language to write a compiler with. First of all, do you really honestly need it to be scalable? Or scalable at this stage? Especially for a markup language? your not compiling 60+ mb of source so i dont think you actually need it to be scalable.

Anyways for my programming language i used bison for the parser (reading bison+flex is a must, try to avoid all conflicts my language has none). Then i use both C and C++ for the code. C because bison uses C and i just call a simple C function which creates and fill in a struct to create an abstract syntax tree. Then when its done it calls my C++ code that runs through the AST and generate the binary.

Standard ML is suppose to be really good with creating a language. If you dont use that a functional language is a good choice because it fits with the mindset (parsing may be left to right but your function calls wont be in that order). So i recommend that if you dont use bison (or know how to call it using C/C++ and bison).

Note: I tried writing a compiler twice. The first time in C without bison the 2nd time with bison. Theres no question that it would have taken me exponentially longer due to the fact that bison finds the conflicts for me and i am not doomed in debug land (i would probably in fact try to figure out a way to report conflicts before i write the code which is exactly what bison does)

陈独秀 2024-10-17 14:49:57

忘记您使用的编程语言&另外,考虑到您在现代计算机时代拥有巨大的内存支持,您可以写出好的内容和内容。使用解释语言的快速程序也非常糟糕使用 C/C++(编译语言)和运行缓慢的程序反之亦然。

重要的是使用正确的数据结构和算法&遵循您用来实现它的编程语言的风格/模式。请记住,有人说过“OO 不是万能药”。另一方面,其他人也说“展示你的数据结构,我将为你试图解决的问题编写算法”。

Forget what programming language you use & also given that you have huge memory support in these modern computer era you could write good & fast programs using interpreted language and also very bad & slow running programs using C/C++ (compiled languages) & vice versa.

What is important is to use right data structures and algorithms & follow the style/patterns of the programming language you use to implement it. Remember that some one said "OO is not a panacea" & to the other extent some one else also said "show your data structures and I will code up the algorithm for the problem you are trying to solve".

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