ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In a nutshell, you want a lexical analyser, a syntax analyser and a code generator. And of course you need to define the grammar of the language you intend to compile. You can get a lot more sophisticated than this, but this is the basic structure of most simple compilers.
If this is a learning exercise, I'd consider compiling into your own interpreted byte code or into assember text (or even to C text) as binary code generation can be quite complex compared to the other stages.
I am not a programmer, but....If you have not done a lot of application and system programming, I would not think that designing a compiler would be a good place to start. This said, the first thing I would do is buy a book on compiler design. A search at Amazon or O'Reilly should turn up something.
I've got several good books about compilers; I'll post the list when I get home today. But a few things to consider are:
(1) Language design - what language are you implementing? How will it work?
(2) Compiler target - are you writing for a virtual or native machine?
As for language design, that's syntax and semantics. Take a look at existing language design books, as well as existing programming languages, for some ideas.
Targeting a virtual machine is usually easier than compiling to native code, as long as you're using an existing VM implementation like the JVM, CLR, or Parrot. Or you could write a VM spec, then write the VM and a compiler to bytecode at the sametime.
In a nutshell, you want a lexical analyser, a syntax analyser and a code generator. And of course you need to define the grammar of the language you intend to compile. You can get a lot more sophisticated than this, but this is the basic structure of most simple compilers.
If this is a learning exercise, I'd consider compiling into your own interpreted byte code or into assember text (or even to C text) as binary code generation can be quite complex compared to the other stages.
thank you for the sugesstions, can you please point to me for open source simple compilers code so that i can understand bit more about parser because i feel parser is bit difficult to understand.
I am not a programmer, but....If you have not done a lot of application and system programming, I would not think that designing a compiler would be a good place to start. This said, the first thing I would do is buy a book on compiler design. A search at Amazon or O'Reilly should turn up something.
can you please point to me for open source simple compilers code so that i can understand bit more about parser because i feel parser is bit difficult to understand.
I'll mail you some simple source if let me know a suitable email address (I'm nick.battle@gmail.com).
The book that I used to learn some of C had us write a emulator for a simple computer and create simple syntax to write programs with it. It then moved onto writing a simple language and a compiler for it to translate it into the sudo machine code that you come up with in the first section. So its not unheard of to write a very simple compiler for educative reasons.
However this was very basic and only could compile the sudo code writing a compiler to compile C code or something Like that would be difficult without knowledge in asm.
For a quick start, look at man yacc and the items referenced from there. (yacc is Yet Another Compiler Compiler.)
It's been a while since I needed to use yacc, but I think you can find examples of simple projects using it fairly easily. Now that I think about it, yacc may, itself, be a program created by yacc. So its description (in the yacc input language) may be a good example.
Last edited by PTrenholme; 08-31-2007 at 07:59 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.