如何编写简单的编译器? [英] How do I write a simple compiler?

查看:112
本文介绍了如何编写简单的编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够编写一个简单的c编译器或python解释器,我不知道如何。我这样做是因为有一天我想编写自己的操作系统而且我听说如果我开始在另一个操作系统上编写我的操作系统,我将需要制作一个自定义编译器,所以基本上我的操作系统可以在没有其他操作系统的情况下运行 - 这称为交叉编译器。如果我顺便说错了,请纠正我。

我也听说编写操作系统是所有编程任务中最难的,因为编译器创建也很困难我知道我会如果我不能编写编译器或翻译器,就没有机会设计操作系统。



我知道这是非常含糊不清的,但是有人能指出我正确的方向实际上学习如何制作python解释器的C编译器,并不一定指向我只会教我编译器如何工作的方向。

I want to be able to write a simple c compiler or a python interpreter by I have no clue how. I do this because one day i want to write my own OS and I hear that if i start writing my OS on another OS i will need to make a custom compiler so basically my OS can run without the other OS- something called a cross-compiler. Correct me if I've said anything wrong by the way.
I've also heard that writing an OS is the hardest of all programming tasks and since compiler creation is also quite difficult I know I'll have no chance of designing an os if i cna't write a compiler or interpreter.

I know this is very ambiguous but can someone point me in the right direction to actually learning HOW to make a C compiler of python interpreter and not necessarily point me in a direction that'll only teach me how compilers work.

推荐答案

有一些评论到这个发布 [ ^ ]将指向右侧方向 - 特别是关于编译器设计的几本好书。



设计一个操作系统并设计一个编译器是两个非常不同的东西,与你建议的方式完全没有关系......它不是一个困难的任务,而是你需要了解每个工作的方式
There are some comments to this post[^] that will point you in the right direction - specifically a couple of good books on compiler design.

Designing an Operating System and designing a Compiler are two very different things and not at all related in the way that you suggest ... it's not about being a "difficult task" but you do need to understand how each works


这不做C或Python - 它使用一种简单的演示语言来调用Kaleidoscope - 但它是一个从头开始编写编译器的教程: http://llvm.org/docs/tutorial/ [ ^ ]



请注意 - 这是很多工作,并且没有实际的理由需要编写编译器为了编写操作系统:您需要编写与您的操作系统接口的库,但是交叉编译器可以编译操作系统,然后在编写必要的接口库时自行编译以在操作系统下运行。如果您不提供匹配,字符串处理等库,那么编写编译器是没有意义的 - 您的操作系统将确定库代码如何与用户交互,而不是语言。想想.NET和C#:后者是一种非常小而紧凑的语言 - 这项工作的重要部分是在.NET中,它与语言无关。
This doesn't do C, or Python - it works with a simple demo language it calls Kaleidoscope - but it is a tutorial on writing a compiler from scratch: http://llvm.org/docs/tutorial/[^]

Be aware - it's a lot of work, and there is no actual reason that you need to write a compiler in order to write an OS: you will need to write libraries that interface to your OS, but a cross compiler can compile the OS and then compile itself to run under the OS when you have the necessary interface libraries written. There is no point in writing a compiler if you don't provide libraries to do match, string handling and so forth - and your OS will determine how the library code interfaces with the user, not the language. Think about .NET and C#: the latter is a language, that is pretty small and compact - the massive part of the work is in .NET which is language independent.


这篇关于如何编写简单的编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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