Page 1 of 1

Creating a compiler

Posted: Mon Nov 25, 2002 12:00 am
by Mara SelVador
To create a goodos you must have a good compiler
I'm building a basic compiler but i don't have any experience.
I would like to have only suggections.
Thank ou.

RE:Creating a compiler

Posted: Tue Nov 26, 2002 12:00 am
by carbonBased
There's a great from-scratch tutorial (using Pascal code) written by Jack Crenshawe.  I'd take a look at that for some ideas on where to start.

Personally, I wrote a tokenizer first, and then built upon it.  The tokenizer breaks up the input string into tokens (which are defined in a global array along with a made up "token number" (easier/faster to use once you've broken up on the strings).  The tokenizer adds each token to a linked list, but in an altered order (in other words, order of operations, etc, are taken care of by the tokenizer).

For eg.
y = 4 * (2 + 3)

equates to
2, 3, +, 4, *, = y

On my linked list.

Which the second phaze interprets as:
mov ebx, 2
mov eax, 3
add eax, ebx
mov ebx, 4
mul ebx
mov eax, [y]

(or something to that effect... I haven't had the time to work on it much lately, so I can't remember everything completely).

With this two pass method, I find it a lot easier to optomize code.  For example, after tokenizing, you can skim through the linked list, and find code that does nothing (ie, "y, 1, *, = y" or "y = y * 1" can be completely ignored.)

If you're interested, the source code to the current version of my compiler can by found on my web site at www.neuraldk.org  Hopefully soon I'll have a much more advanced version, but this one is fairly functional, and works with basic arithmatic (including brackets and variables), and supports quite a few constructs -> C style for loop, do {} while/until, while/until {}, if)

Jeff

RE:Creating a compiler

Posted: Wed Nov 27, 2002 12:00 am
by Mara
It's OK, but is code tiny??? I don't think so.

RE:Creating a compiler

Posted: Thu Nov 28, 2002 12:00 am
by carbonBased
Mine is much more tight then Crenshawe's compiler.

Once I'm finished the basic compilation, I'm going to create a peephole optimization phaze to the compiler, which'll produce even tigher code.

However, if you want the most tight code, output to C, and compile through GCC :)

Jeff

RE:Creating a compiler

Posted: Thu Nov 28, 2002 12:00 am
by drwatts
If you would like, I am in a class right now concerning compilers, and have a small, quick C++ compiler which currently supports switch statements, for loops, while loops, and a number of other commands.  It does convert C code directly to assembly, although some methods are not quite efficent enough yet.  However, if you would like to have a copy of the code, please send me an e-mail at [email protected]. Good luck in your endeavours.

RE:Creating a compiler

Posted: Fri Nov 29, 2002 12:00 am
by eox
if you need a good compiler, why don't you just use watcom?
that would save a lot of time -> more time for osdev :)