从一个简单的(最简单的)C编译器? [英] Starting off a simple (the simplest perhaps) C compiler?

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

问题描述

我遇到了这个问题:使用Turbo Pascal编写一个编译器

我很好奇,如果有任何教程或参考解释如何去创建一个简单的C编译器。我的意思是,如果它让我达到使它理解算术运算的水平就足够了。在 Ken Thompson 阅读本文后,我变得非常好奇。写一些理解的东西的想法似乎令人兴奋。

I am curious if there are any tutorials or references explaining how to go about creating a simple C compiler. I mean, it is enough if it gets me to the level of making it understand arithmetic operations. I became really curious after reading this article by Ken Thompson. The idea of writing something that understands itself seems exciting.

为什么我会提出这个问题,而不是要求Google?我试过谷歌和Pascal一是第一个链接。其他没有似乎相关的,并添加到...我不是一个CS专业(所以我仍然需要学习所有的工具,如yacc做),我想通过做,并希望有更多的经验的人总是比这些东西更好。我想阅读一些与上面列出的一样精神的文章,但至少强调了构建一个简单的C编译器的启动阶段。

Why did I put up this question instead of asking Google? I tried Google and the Pascal one was the first link. The rest did no seem relevant and added to that... I am not a CS major (so I still need to learn what all those tools like yacc do) and I want to learn this by doing and am hoping people with more experience are always better at these things than Google. I want to read some article written in the same spirit as the one I listed above but that which highlights at least the bootstrapping phases of building a simple C compiler.

此外,我不知道最好的学习方式。我开始用C或其他语言构建C编译器吗?我写一个C编译器或其他语言吗?我觉得这样的问题更好地回答一旦我有一些方向探索。任何建议?

Also, I don't know the best way to learn. Do I start off building a C compiler in C or some other language? Do I write a C compiler or some other language? I feel questions like this are better answered once I have some direction to explore. Any suggestions?

任何建议?

推荐答案

三个:


  1. 解析器

  2. 抽象语法树(AST)

  3. 代码生成器

有很多不错的解析器生成器,从语言语法开始。也许ANTLR将是一个好地方,你开始。如果你想坚持C根,尝试lex / yacc或bison。

There are lots of nice parser generators that start with language grammars. Maybe ANTLR would be a good place for you to start. If you want to stick to C roots, try lex/yacc or bison.

有C的语法,但我认为C的整体是复杂的。

There are grammars for C, but I think C in its entirety is complex. You'd do well to start off with a subset of the language and work your way up.

一旦你有一个AST,你使用它来生成机器代码,

Once you have an AST, you use it to generate the machine code that you'll run.

这是可行的,但不是微不足道的。

It's doable, but not trivial.

关于编写编译器。

更新:堆栈溢出有类似的问题,例如这一个。也请查看这些资源。

UPDATE: There have been similar questions on Stack overflow, like this one. Check out those resources as well.

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

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