设计编程语言时要记住的事情

发布于 2024-10-11 12:21:06 字数 151 浏览 5 评论 0原文

我正在设计一种用于个人使用和教育的编程语言。参考编译器的第一个版本会将源代码编译为其他语言(例如 C)。为了确保编译为另一个源代码和二进制可执行文件的编译时间都很快,我应该记住哪些事项?还有其他值得了解的事情吗?

尽管我谈论编译速度,但问题的主要焦点是语言,而不是编译器。

I'm designing a programming language for my personal use and education. The first versions of the reference compiler will compile the source to some other language like C. What things should I keep in mind to ensure the compile times will be fast for both compiling to another source and binary executable? Any other things that are good to know?

Even though I talk about compile speeds the main focus of the question is the language, not the compiler.

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

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

发布评论

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

评论(2

秋心╮凉 2024-10-18 12:21:07

除非你设计严重错误,否则你的语言编译速度几乎不会成为问题。您应该关注您需要的语法和语义。

Unless you severely mis-design your language compilation speed will hardly be a problem. You should focus on the syntax and semantics you need.

子栖 2024-10-18 12:21:06

如果您的重点是学习编译器设计,我认为编译速度不会是您的首要任务。我建议您首先使用递归后代技术手动实现自上而下的解析器,这既简单又直接。还可以使用使用有限状态机手动设计的词法分析器,这也很简单,但概念上非常丰富。

在定义语言的语法时,必须确保其一致且明确。如果您熟悉 Prolog,则可以在实现之前使用定语从句语法 (DCG) 来尝试您的语言。我觉得它非常有用。

然后,您可以使用生成词法分析器和解析器的工具(例如 Bison、ANTLR、Lemon、Yacc 等)来实现词法分析器和解析器。

另一个建议是保持简单。您可以实现要构建的语言的子集并对其进行完善,直到您对其感到满意为止,然后对其进行扩展以实现新功能等等。您会发现自己在不断发展您的语言和编译器,并看到它如何变得更大、更完整,并且随着它变得越来越好,您会感到更加自信和满意。毕竟这是一个美好而美好的挑战。

玩得开心。学习编译器设计只是一个有趣且非常有趣的学科!

附言。阅读龙之书

If your focus is on learning compiler design, I think compile speed won't be your priority. I suggest you implement first a top down parser manually using a recursive descendant technique, which is easy and straighfordward. Also use a lexer manually designed using a finite state machine which is also kind of easy, but very rich conceptually.

While defining the syntax of your language, you must ensure it's consistent and unambiguos. If you are familiar with Prolog, you could use Definite Clause Grammars (DCG) to play with your language before implementing it. I find it quite useful.

Then you can look fordward implementing a lexer and parser using tools for generating them (like Bison, ANTLR, Lemon, Yacc, etc.)

Another advise is to keep it simple. You can implement a subset of the language you want to build and polish it until you feel satisfied with it, then expand it implementing new features and so on. You will find yourself growing your language and your compiler and seing how it gets bigger and more complete and as it gets better and better you will feel more confident and satisfied. It's a nice and beautiful challenge after all.

Have fun. Learning compiler design is just a fun and a very interesting subject!

PS. Read the dragon book

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