Compiler / Linker question

Started by
14 comments, last by Dave Hunt 18 years, 7 months ago
How long does it generally take someone to write a compiler and linker from scratch? How many lines of code would it take on average? Im looking to try and make some real software instead of 200-500 line stuff so I'm thinking of writing my own compiler and linker and thought it would be a good learning experience + good on resume etc.
Advertisement
Once you feel pretty comfortable skiing on the beginner runs, you try your luck on the intermediate runs. You do not jump out of a helicopter onto a mile-high triple-black-diamond snowy cliff of death. [wink] That should give you some indicator of the difficulty of writing a C++ compiler and linker. IIRC, GCC is around 500,000 lines of code.
It depends, really, on what kind of language you're trying to compile and the features you want your software package to have.

Take a look at the source code to GNU ld and gcc. ld is the linker. These are obviously very large.

You can make compilers and linkers very small too, depending on your skill. Your experience with making these types of things will determine how long it will take.
I could probably write a simple language that created x86 object files and write a linker to create exes in about 80 hours, so your milage may vary.


Quote:Original post by Sneftel
Once you feel pretty comfortable skiing on the beginner runs, you try your luck on the intermediate runs. You do not jump out of a helicopter onto a mile-high triple-black-diamond snowy cliff of death. [wink] That should give you some indicator of the difficulty of writing a C++ compiler and linker. IIRC, GCC is around 500,000 lines of code.




Like what? I have no clue what to program.. Ive written animation progs. and stuff in java and used the WinAPI a bit, but besides games and stuff.. what can I write that would take some time/research and have a decent payoff?

I was thinking about a language like C or maybe something lower level than that(maybe one of those extinct languages)
The thing about writing a compiler is that you will spend most of your time learning how to write compilers, and the majority of the benefit you will receive from the exercise will be an understanding of how to write compilers. Obviously that's not a bad thing--everyone should write a (simple) compiler at least once--but if you're trying to get more experience with larger applications, there are less painful ways to do it.

I'm sure others can chime in on ideas for things you can make. Also, you might consider offering your services in the Help Wanted forum.
Buy Compilers: Principles, Techniques, and Tools and as you're reading it, try doing simple stuff like making a calculator that at first can do things like "9+3" and advance it to where you can have it do things like "sin(50)^2+3" then to where to can do things like "x=50;y=sin(x)^2+6;print(y-3)" and finally to where it can produce object files and link them together to make programs that do the simple statements. Lastly, you can make a compiler for whatever language you want (with enough determination)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
What are the prerequisites for writing one though? I havent really done any 32 bit asm ( i learned the obsolete 16 bit asm ). So should I do a lot more of ASM programming before I even start?

Compilers, as people have pointed out, are a gi-normous undertaking. If you really want to go down that route, you probably would be best off starting with lexical analysis. There are two tools, lex and yacc, that come packaged with linux distros that are very useful for things like that. Next, you really need to know assembly inside and out. By then, it will probably be 2007. but seriously, the dragon book (which extrarius linked) is the most popular introduction to compilers out there, and there aren't many other options available even if you wanted one. If you're serious about writing a compiler, that would be a good starting point.
Which assembly book would be better for this? I noticed that this book uses its own IO routines which dont require any knowledge of asm. Introduction to 80x86 Assembly
I already can do output fairly well in ASM so maybe I could just use my own routines

Either that book or this one, which is a bit expensive, but more people use it.

Assembly language for intel-basecomputers

After either of these books would I be able to do most of the stuff I'd need to for a compiler in ASM?
Quote:Original post by Gink
Which assembly book would be better for this? I noticed that this book uses its own IO routines which dont require any knowledge of asm. Introduction to 80x86 Assembly
I already can do output fairly well in ASM so maybe I could just use my own routines

Either that book or this one, which is a bit expensive, but more people use it.

Assembly language for intel-basecomputers

After either of these books would I be able to do most of the stuff I'd need to for a compiler in ASM?


You really don't want to write a compiler in ASM. There is no need to, and it would take forever. If you're comfortable with C/C++, use that to write the compiler. You don't even have to generate assembly from your compiler. You could generate C and use a C-compiler to generate the assembly/machine code. Or you could create an intermediate code and provide an interpreter/executor for it.

If you want a project that you might actually finish, don't write it in ASM, and don't generate ASM. Life is too short. ;)

This topic is closed to new replies.

Advertisement