PDA

View Full Version : Have you ever written a compiler?



ammar
12-25-2004, 09:05 AM
I'm finishing up my final project for the course Compiler Design, and I thought it would be nice to know how many of you tried writting compilers, and what did you use to write it.

What I did in the project is that I wrote a compiler for a "toy" language called C- (C minus), which is simmilar to C but simpler.

I wrote the scanner myself, I used YACC to make the parser (LR(1)) and wrote the semantics and code generation myself.

If you have ever done a similar thing I would like to know how did you do it. If you didn't I really encourage you to read about compiler construction, it's a very interesting topic.

[edit]
BTW: The text book that we have is Compiler Construction Principles and Practice by Kenneth C. Louden

sean
12-25-2004, 09:57 AM
I tried writing an assembler once. It was written in C (CGI program) and did little more than read in an instruction, and replace it with the correct binary sequence (lots of switch/case action going on...) . I never managed to work out the binary sequences for operators and that was what brought the project to a halt. I might try again one day.

-KEN-
12-25-2004, 11:31 AM
I write one every other week, just to make sure I don't get rusty.

ammar
12-25-2004, 12:47 PM
I write one every other week, just to make sure I don't get rusty.

Can you explain what kind of compilers you write, and what methods you use to do so.
I'm interested in knowing about other techniques in compiler construction.

Thanks.

sean
12-25-2004, 12:49 PM
I think -KEN- was being a tad sarcastic, though I wouldn't be surprized if has written ONE. In addition to picking the brains of board members, you might want to check out directory.google.com. I find the directory format better than the search format for finding tutorials on subjects like this. It was very useful when I was learning about OS theory.

ammar
12-25-2004, 01:17 PM
I think -KEN- was being a tad sarcastic, though I wouldn't be surprized if has written ONE. In addition to picking the brains of board members, you might want to check out directory.google.com. I find the directory format better than the search format for finding tutorials on subjects like this. It was very useful when I was learning about OS theory.

I know he was being sarcastic (who would write a compiler each week), I was just hoping that if has written one he would tell me more about it!

sean345
12-25-2004, 02:45 PM
I wrote a small BASIC compiler. It does not have offer much in terms of a language, but it was fun to write. It generates assembly code for NASM. If anyone cares you can download it at http://blackfrog.pagemac.com

I found a lot of information from comp.compilers and from looking at the source for the free C Compiler, lcc.

- Sean

sean
12-25-2004, 07:44 PM
I've been working on a server for some time and no idea what to do when I first started. I've learned a lot more by reading the source code of the most simple of servers than I have from studying the complex ones for hours. The same may apply to compilers, so keep it in mind. Once you've got the essntials, the rest is just good design and use of C/C++.

Prelude
12-25-2004, 07:48 PM
>I was just hoping that if has written one he would tell me more about it!
A compiler is one of the more complicated programs that one can write. So, for the most part, you can expect anyone who actually has written one to remain silent about it so as to avoid answering a flood of questions. :)

You would be better off asking more specific questions. "Tell me everything about <topic>" questions don't go over very well.

sean
12-25-2004, 07:51 PM
For example, "tell me everything about women".

edit: Sorry Prelude. But it's true. We have no idea what's going on.

andyhunter
12-25-2004, 08:02 PM
>For example, "tell me everything about women".

edit: Sorry Prelude. But it's true. We have no idea what's going on.



An iPod and a diamond makes Prelude a happy geek girl.




What else is there? :D

ammar
12-26-2004, 03:24 AM
>I was just hoping that if has written one he would tell me more about it!
A compiler is one of the more complicated programs that one can write. So, for the most part, you can expect anyone who actually has written one to remain silent about it so as to avoid answering a flood of questions. :)

You would be better off asking more specific questions. "Tell me everything about <topic>" questions don't go over very well.

What I was looking for is some little and general information, like saying: I used YACC for the parser, LR(1), or LALR(1), or I used reqursive decent parsing, I used lex for the scanner or I wrote the scanner myself, what kind of target code and or intermediate code did you generate etc...

I'm not looking for details because I already had a whole course called Compiler Design (CS474) that covered most of these topics, and also included a project that I talked about in my first post( which I finished already) , I only wanted to know what you ( people on this board ) did, maybe there are some methods that I don't know about, and I was looking forward to knowing it.

[edit]
I will not ask too many questions, I promise :D .

Sang-drax
12-26-2004, 07:44 AM
I haven't written a compiler to machine code, but I've created a scripting language called Omicron.
The parser finds the operator with the highest predecence and converts it to RPN. When the whole program is converted to RPN the program is exxcuted.
http://www.strandmark.com/omicron.shtml

-KEN-
12-26-2004, 10:24 AM
Sorry to disappoint. I've tried tackling an OS a looong time ago (which may sound odd from somebody who can't legally buy alcohol yet), but quickly got bored and left it after learning how to make it display pretty colors ;). Never tried a compiler.

sean
12-26-2004, 10:33 AM
Alternate Ending:

... but quickly got bored and left it to go buy some alocohol.

Darkness
12-26-2004, 12:01 PM
I'm working on a really 'neat' project right now that involves the development of a basic opcode interpreter. It is similar to assembly in that it only interprets (and executes) a single instruction at a time. However, each 'instruction' does more than just move a number around in registers, or push and pop things in and out of memory. The only limit to the complexity is that the instruction cannot use more than a 3D vector, a scalar, and a byte (that's sort of the definition of the size of an instruction). If it uses any more than that, it gets broken down into multiple instructions.

The cool thing about that is you can write your own programs in a text file by stringing together these basic instructions, and the opcode interpreter reads it in and basically 'compiles' a program (well, interprets it).

This is an AI program that uses genetic algorithms to sort of evolve specimens over time such that they can execute certain types of behaviors efficiently (although they don't learn new behavior...the pinnacle of artificial intelligence research).

doubleanti
12-26-2004, 09:58 PM
>BTW: The text book that we have is Compiler Construction Principles and Practice by Kenneth C. Louden

I'm going take a rather tasty intro course on it in a short week or so, I await it but thank you for such a tip as there is no required textbook, though a recommended one. It would be nice if I could get all my lexical questions answered, top of the list: why? =)

bithub
12-26-2004, 10:20 PM
I wrote an assembler before (using ASM) for a motorola 68000. I wrote a compiler for a fake language back in school. We had to write our own lex analyizer and recursive descent parser. Didn't take it all the way to machine code, although that would have been the easiest part. The BNF for the fake language only took about half a page, so I could only imagine what it would be like to write a parser, analyzer, and code generator for a language like C.

Darkness
12-26-2004, 11:25 PM
Yeah, I hear ya. People that write everything required to compile C / C++ code (including everything you mentioned) are gods, nuff said.

VirtualAce
12-27-2004, 02:43 AM
...couldn't figure out the operators....


What's to figure out? For Intel they are either hardcoded into the instruction or the operators are used to create a displacement and set the R/M byte to something meaningful.

ammar
12-27-2004, 02:53 AM
>BTW: The text book that we have is Compiler Construction Principles and Practice by Kenneth C. Louden

I'm going take a rather tasty intro course on it in a short week or so, I await it but thank you for such a tip as there is no required textbook, though a recommended one. It would be nice if I could get all my lexical questions answered, top of the list: why? =)

It's a nice textbook indeed, and it comes with a compiler of a "toy" language called TINY ( 4000+ line of code )which is based on Pascal... and it goes towards making a compiler of a language called C- ( C minus ) which is simmilar to C but simpler, but at the same time more complex than TINY. It discusses many methods for each phase of the compiler.

And thank you all for your input.

sean
12-27-2004, 07:10 AM
What's to figure out?

Whoa... operators was a crappy word choice... Operands would make more sense

I had a very old book technical book on using DOS's deeper features, and it had several chapter on assembly, and an apendix listing, among other things, the binary equivalents of each instruction. So that made it easy. I just tokenized the whole thing and ran each token through a switch/case loop, and if strcmp equaled zero, then the appropriate char was read on to the file. The problem came when I was trying to figure out what to do with the : at the end of labels, commas between operands, and whether I should reverse byte order from what was in the book. I had tonnes of questions and couldn't find any tutorials on the subject. I posted a couple of threads but eventually lost interest. That's why I said I'd probably go back to it sometime, but not soon - I'm really engrossed in my server.