1, 背景介绍

来源:互联网 发布:达内培训 班java机 编辑:程序博客网 时间:2024/06/05 12:02

    我是个编程初学者, 会C语言和一点点C++, 对基本的数据结构和算法有一定的了解, 不精通. 我对编译原理蛮有兴趣的, 于是就下定决定要写一个编译器. 终于在2015年暑假, 花了1个月的时间, 完成了<计算机系统要素>这本书中的编译器项目.

    这本书介绍了一门语言jack, 这是一门简单的面向对象语言, 书中只是简单地对这门语言的规范作了点介绍而已, 没有任何代码指导. 仅仅有一些测试数据, 由于我之前学了点编译原理方面的知识, 于是我就参考这本书, 慢慢地完成了jack语言编译器.

    我对书中的jack语言规范作了一些改进, 使这门语言更加简洁美观, 同时也增加了一些难度.

    最后, 我把这门语言移植到了x86平台下.

    下面是一个简单的示例:

class Main {    function void main()     {        String s;        Output.printString("Hello, world!");        Output.println();        Output.printString("What's your name?");        Output.println();        s = Input.readLine();        Output.printString("Your name is: ");        Output.printString(s);        Output.println();        return;    }}
    执行 ./jackc Main.jack  就可以把这段源程序编译成Main.vm虚拟机代码文件:
function Main.main 1push constant 13call String.new 1push constant 72call String.appendChar 2push constant 101call String.appendChar 2push constant 108call String.appendChar 2push constant 108call String.appendChar 2push constant 111call String.appendChar 2push constant 44call String.appendChar 2push constant 32call String.appendChar 2push constant 119call String.appendChar 2push constant 111call String.appendChar 2push constant 114call String.appendChar 2push constant 108call String.appendChar 2push constant 100call String.appendChar 2push constant 33call String.appendChar 2call Output.printString 1pop temp 0call Output.println 0pop temp 0push constant 17call String.new 1push constant 87call String.appendChar 2push constant 104call String.appendChar 2push constant 97call String.appendChar 2push constant 116call String.appendChar 2push constant 39call String.appendChar 2push constant 115call String.appendChar 2push constant 32call String.appendChar 2push constant 121call String.appendChar 2push constant 111call String.appendChar 2push constant 117call String.appendChar 2push constant 114call String.appendChar 2push constant 32call String.appendChar 2push constant 110call String.appendChar 2push constant 97call String.appendChar 2push constant 109call String.appendChar 2push constant 101call String.appendChar 2push constant 63call String.appendChar 2call Output.printString 1pop temp 0call Output.println 0pop temp 0call Input.readLine 0pop local 0push constant 14call String.new 1push constant 89call String.appendChar 2push constant 111call String.appendChar 2push constant 117call String.appendChar 2push constant 114call String.appendChar 2push constant 32call String.appendChar 2push constant 110call String.appendChar 2push constant 97call String.appendChar 2push constant 109call String.appendChar 2push constant 101call String.appendChar 2push constant 32call String.appendChar 2push constant 105call String.appendChar 2push constant 115call String.appendChar 2push constant 58call String.appendChar 2push constant 32call String.appendChar 2call Output.printString 1pop temp 0push local 0call Output.printString 1pop temp 0call Output.println 0pop temp 0push constant 0return

    执行./jack Main.vm  即可看到运行结果:




    是不是觉得非常酷? 我发现很多人虽然学了编译原理, 但是如果要实际动手去写一个编译器, 却不知道如何下手. 以后我会慢慢更新我的博客, 来介绍如何写一个编译器!

    另外, 学过编译原理的人都知道, 有一些自动生成工具, 比如flex, bison等, 可以自动帮你完成很大部分的工作. 但是我的编译器不会借助于这些工具, 而是全手工编写的.

    当时写的完整编译器代码在这里: https://github.com/Xiang1993/jack-compiler

    其实这个编译器其实有很多的bug的, 因为比较忙, 所以一直都没有修复. 注释也比较少, 而且代码也写得很丑, 于是我决定重新写一遍! 新的编译器代码会放在这里: https://github.com/Xiang1993/new_jack_compiler 我会慢慢更新的!

    由于快要毕业了, 以后工作了之后时间也应该不会很多, 所以我的文章可能更新地比较慢!

0 0