LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2007, 02:45 AM   #1
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Rep: Reputation: 15
how to construct a compiler


Hello,

i have basic knowledge of C and bit of Operating system knowledge.

i am planning to write a compiler, but i couldnt find any tutorials or how to write a compiler and all that stuff.

can you anyone point me to online tutorials to design a compiler or open source for small compiler for understand about compilers.

Thanks
dayalan
 
Old 08-30-2007, 06:37 AM   #2
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
http://en.wikipedia.org/wiki/Compiler is a good start.

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.

HTH,
-nick
 
Old 08-30-2007, 07:01 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.

WHY do you want to write your own compiler?
 
Old 08-30-2007, 08:49 AM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.
 
Old 08-30-2007, 02:02 PM   #5
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Great book: http://www.amazon.com/Compilers-Prin...8500473&sr=8-1
It is sometimes called the Dragon Book.
 
Old 08-31-2007, 05:02 AM   #6
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Nick_Battle View Post
http://en.wikipedia.org/wiki/Compiler is a good start.

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.

HTH,
-nick
===========================================================

Hi Nick,

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.

Thanks
dayalan
 
Old 08-31-2007, 05:16 AM   #7
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Original Poster
Rep: Reputation: 15
how to construct a compiler

Quote:
Originally Posted by pixellany View Post
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.

WHY do you want to write your own compiler?
===============================================================

Hi pixellany,

i am interested to lean and write small compilers to understand how high-level language is converted into low level (Machine Level) code.

if you can please let me know some small compilers open source.

Thanks
dayalan
 
Old 08-31-2007, 06:11 AM   #8
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
Quote:
Originally Posted by dayalan_cse View Post
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).
 
Old 08-31-2007, 04:48 PM   #9
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.
 
Old 08-31-2007, 05:00 PM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 08-31-2007, 05:28 PM   #11
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Compiler Books

Here's a list:
  • Crafting a Compiler with C
  • Engineering a Compiler
  • Advanced Compiler Design & Implementation
  • Compiler Construction: Principles and Practice
  • Compiling with Continuations
  • Compilers (The Dragon Book)

Plus the series by Andrew Appel: Modern Compiler Implementation in G, where G ∊ {C, Java, ML}.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to construct a library file like DLL in windows. SameerLx Linux - Newbie 1 06-16-2007 01:10 AM
Construct a Service amirsarb Programming 2 06-01-2007 09:58 AM
C++: construct object at certain address addy86 Programming 18 07-22-2006 05:17 PM
getting rpoblem in assigning value to variable in if construct Ankit mittal Programming 9 01-07-2006 09:12 AM
how-to construct execution trees when using yacc sibtay Programming 0 12-30-2004 06:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration