如何编写C编译器? [英] How to code a compiler in C?

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

问题描述

我在C中编译了一个编译器,我已阅读了中的所有编译器龙书。但我发现它真的很难实现,我不知道从哪里开始。即使当它涉及到lexer部分,所以会喜欢知道一步一步的方法在代码写的基础上编写一个编译器在C!



您建议我下一步做什么?

解决方案

请参阅 C中的Appel现代编译器实现



通过它的声音,你需要找出你想要编译的语言:你想要一个C语言的子集或一个容易解析语言如Scheme,或者只是算术表达式语言?



选择/设计一种语言,在其中写几个真正的小程序,为其中一部分写一个词法分析器/解析器,解释开始 - 只是为了你可以看到它运行),然后迭代看起来有趣的块,构建到完整的语言。



基于额外的细节 的python,但保持简单
as c


我不知道我会这样写一切都是手工的,但如果我... ...



我会用混合语言写出一些程序,我想结束:所以如果你想C类似列表推理的Python可能

  void main()
{
int [] x = {1 ,2,3,4,5};
int [] y = {i * i for i in x where i%2 == 0};
for(int i in y){printf(%d,i); }
}

[C样式数组,读者:-)!]



然后得到一个绝对最小的C程序工作,hello world或者甚至只是静态添加一些数字(如果是hello world我甚至可以开始通过特殊的外壳printf所以我不必解析stdio.h - 如果你正朝着一个C Python混合,你可能最终会保持那样)。一旦你可以做

  void main()
{
int x = 0;
int y;
y = 5;
x + y;
}

您可以开始添加复杂性:任意函数定义和调用,值,数组,数据结构,常量,指针,...逐步建立到最简单的示例程序。



你有很多C编译器,你可以看看的想法,所以你去例如 TinyCC 因此,当你遇到添加蟒蛇片的困难,你有一个坚实的基础。



这是在漫长的路上滑过很多细节。祝你好运。


I am coding a compiler in C, and I have read all about compilers in the Dragon book. But I am finding it really hard to implement, and I have no clue as to where to start. Even when it comes to lexer part, so would love to know a step by step method on the basis of code writing to write a compiler in C!!

What would you suggest I do next?

解决方案

You could look at Appel's Modern Compiler Implementation in C.

By the sounds of it you need to work out what language you want to compile: do you want a subset of C say or a easy to parse language like Scheme, or just an arithmetic expression language?

Pick/design a language, write a couple of really small programs in it, write a lexer/parser for part of it, then the back to get parts working (possibly interpreted to start - just so you can see it running) and then iterate chunks that seem interesting, building up to the full language.

Edit based on extra details supplied

"i want to make a super set of c , like implementing various advantages of python , but keeping it as simple as c"

I'm not sure I'd do that by writing everything by hand but if I did ...

I'd write out some programs in the hybrid language that I want to end up with: so if you want C with Python like list comprehensions then maybe

void main()
{
    int[] x = {1,2,3,4,5};
    int[] y = {i*i for i in x where i % 2 == 0};
    for (int i in y) { printf("%d", i); }
}

[C style arrays that include their count as implied above left as exercise for the reader :-) !]

Then get an absolutely minimal C program working, hello world or even just adding some numbers statically (if it was hello world I might even start by special casing printf so I didn't have to parse stdio.h - if you're heading towards a C-Python hybrid you may end up keeping that). Once you could do

void main() 
{
    int x = 0; 
    int y; 
    y = 5; 
    x + y;
}

You can start adding complexity: arbitrary function definitions and calls, more operators, return values, arrays, data structures, const, pointers, ... building towards the simplest of the example programs step by step.

The advantage of starting with the C subset is you have lots of C compilers you can look at for ideas so you get going e.g. TinyCC so by the time you get to the difficulties of adding python-esque pieces you've got a solid base.

This is skating over a lot of details on a long road. Good luck.

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

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