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

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

问题描述

我碰到这个就来了:编写使用的Turbo Pascal 编译

I came across this: Writing a compiler using Turbo Pascal

我很好奇,如果有解释如何去创建一个简单的C编译器的教程或引用。我的意思是,这是不够的,如果它得到我使其了解算术运算水平。我通过肯·汤普逊看完这篇文章后变得真的很好奇。写东西,明白自己的想法似乎令人振奋。

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.

为什么我提出了这个问题,而不是要求谷歌吗?我想谷歌和帕斯卡一个是第一个链接。其余的没有做相关的似乎并添加到...我不是一个主要的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?

有什么建议?

推荐答案

一个编译器由三部分组成:

A compiler consists of three pieces:


  1. 解析器

  2. 一个抽象语法树(AST)

  3. 系统code发电机

有很多与语言语法开始不错解析器生成的。也许ANTLR将是你一个很好的起点。如果你想坚持到C根源,尝试的lex / yacc的或野牛。

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,你用它来生成机器code,你会运行。

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

这是可行的,但不是小事。

It's doable, but not trivial.

我也检查亚马逊关于编写编译器的书籍。龙书是经典,但也有更现代的。

I'd also check Amazon for books about writing compilers. The Dragon Book is the classic, but there are more modern ones available.

更新:已经有上堆栈溢出类似的问题,如这个。看看这些资源。

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

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

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