"Go" Write a C language compiler: BABYC

Source: Internet
Author: User

"Reproduced" This article is reproduced, convenient for later reading and learning.

Original link: http://blog.jobbole.com/77305/

It's a very effective way to learn how a computer works by writing a compiler and learning about the lower-level programming approach.

Compilers are often considered to be very complex projects. In fact, writing a product-level compiler is really a huge task. But it's not so hard to write a small, usable compiler.

The trick is to find the smallest available project first, and then add the features you want. This method is also the approach mentioned by Abdulaziz Ghuloum in his famous paper "A shortcut to construct a compiler." But this is a viable solution. You just have to follow the first step in this paper to get a really usable compiler! Of course, it can only compile a very small subset of the programming language, but it is really a compiler that is actually available. You can expand the compiler at will and learn more from it.

Inspired by this article, I wrote a C compiler. In a sense it's harder than writing a scheme compiler (because you have to parse the complex syntax of C), but in some ways it's handy (you don't have to deal with runtime types). To write a compiler like this, you only need to start with the smallest compiler available to you.

For the compiler I wrote, I called it babyc, and I chose this code as the first program I needed to run:

C
int Main () {    return2;}

There are no variables, no function calls, no extra dependencies, and even if statements, loop statements are not, everything looks so simple.

We first need to parse this piece of code. We will use Flex and Bison to do this. Here are examples of how to use the reference, fortunately our syntax is so simple, the following is the lexical analyzer:

"{"{return '{'; }"}"{return '}'; }"("{return '('; }")"{return ')'; }";"{return ';'; } [0-9]+ {returnNumber ;}"return"{returnRETURN;}"int"{returnTYPE;}"Main"{returnIDENTIFIER; }

Here is the parser:

function:     ' (') ' ' {'} '     ; expression:    '; '

Finally, we need to generate some assembly code. We will use the 32-bit X86 assembly, because it is very versatile and can be easily run on your machine. Here are the related websites of the X86 assembly.

Here is the assembly code we need to generate:

. Text        . Global  _start # Tell the loader we want-to-start at _start. _start:        movl    $2,%ebx # the argument to Our system call.        MOVL    $1is1.         int     $0x80 # Send an interrupt

Then add the lexical parsing code above and write this assembly code into a file. Congratulations to you! You are already a compiler writer!

This is how BABYC was born, and you can see the way it started.

Of course, if the assembly code is not able to run is in vain. Let's use the compiler to generate the real assembly code we want.

# here's The file we want to compile.$CatReturn_two.c#include<stdio.h>intMain () {return2;} # Run The compiler with thisfile.$ ./babyc return_two.cwritten out.s. # Check The output looks sensible.$Catout.s.text. Global _start _START:MOVL $2, %ebx MOVL $1, %eaxint$0x80

Very good! Then let's really run the compiled code to make sure it gets the results we want.

file -o out.o-------ld -M Elf_i386-s- o out OUT.O # Run it! $ . /Out # What is thereturn code?  Echo $? 2 # woohoo!

We took the first step, then how to do it all to see you. You can do it all as instructed in that article, and then make a more complex compiler. You need to write a more elaborate syntax tree to generate the assembly code. The next steps are: (1) allow the return of any value (for example, return 3; some executable code); (2) Add support for "non" (for example, return to; Some executable code). Every extra feature teaches you more about C, how the compiler executes it, and how the other people in the world write compilers think.

This is the way to build BABYC. BABYC now has the IF statement, loops, variables, and the most basic data structure. You are welcome to check out its code, but I hope to read my article you can write one yourself.

Don't be afraid of some things at the bottom. This is a very wonderful world.

"Go" Write a C language compiler: BABYC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.