Jump to content
  • Advertisement
Sign in to follow this  
ubek666

how writing a compiler???

This topic is 4879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Believe it or not they have compilers for writing compilers. I saw an article about it a few years ago on an old site. I'm sure you can use a search engine and come up with some good information.

Share this post


Link to post
Share on other sites
Get this book

http://www.amazon.com/exec/obidos/tg/detail/-/0201100886/ref=pd_bxgy_text_1/002-3788057-5397617?v=glance&s=books&st=*

CHeers
CHris

Share this post


Link to post
Share on other sites
Quote:

Believe it or not they have compilers for writing compilers. I saw an article about it a few years ago on an old site. I'm sure you can use a search engine and come up with some good information.

Interesting. Most compilers I've seen are either written in C/C++ or O'Caml.

EDIT:
If you have to ask, I wouldn't suggest buying that book just yet. Just wait until you get a bit more experience and it'll come to you :)

Share this post


Link to post
Share on other sites
A very good tutorial: Let's build a Compiler!

It's a lot of work starting out, but definitely rewarding. I've written a few little interpreters and compilers, only one of which has become particularly useful. It's a lot of fun.

Share this post


Link to post
Share on other sites
Writing a simple compiler isn't all that difficult. You can write a compiler in any language (many compilers are written in the language they compile) and there are several different steps which you follow.

Lexical Analysis - Which is the process of reading the input and recognizing tokens which usually consist of keywords, letters, numbers, etc. Generally Lexical analyzers are a pain in the butt to create by hand and should be generated using a tool like Lex.

Parseing - This transforms the stream of tokens from the lexical analyzer into what's referred to as an Abstract Syntrax Tree(AST). The parser uses a context free grammar to which provides rules as to how to transform the stream of token into the AST. These are also a pain to create by hand and should be generated by a tool like Bison.

From there there are various things you can do the AST to optimize it.

The next step is code generation which creates your target code which could be anything like ASM, C, Java, Scheme, etc. During this time is when you'd want to optimize your generated code.

These are the most basic steps that you'd want to. In more advanced compilers there are more steps for different optimizations.

There are plenty of resources on the web. GIYF.

Share this post


Link to post
Share on other sites
Quote:
Original post by Julio
Believe it or not they have compilers for writing compilers. I saw an article about it a few years ago on an old site. I'm sure you can use a search engine and come up with some good information.

Only partly. There are tools that generate lexers, and tools that generate parsers (search for 'lex', 'yacc', 'flex', 'bison', 'antlr', 'spirit' or the 'gold parser generator' for examples)

That still leaves a lot of stuff you have to write yourself.

John B

Share this post


Link to post
Share on other sites
I have problem with buying of this book, because i am from polish so probably wait until expertise on come alone. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Julio
Believe it or not they have compilers for writing compilers. I saw an article about it a few years ago on an old site. I'm sure you can use a search engine and come up with some good information.

Hmm, not sure exactly what you're referring to. There are the usual tools like Flex, Bison or yacc, which can generate the lexer and parser for your compiler, or there are a few programs that can construct a compiler from an interpreter, and finally there's a lot of work being put into compiler generators that can construct a compiler given just a (very strict and rigid) definition of the language, although I don't think anyone has managed to implement that yet.

But getting back on topic, the basic approach is something like this:

1: Construct a lexer to read the source code and split it up into a list of tokens. This is often done with Flex or similar tools
2: Construct a parser to parse these tokens according to the grammar you specify, constructing a syntax tree containing the entire program structure (Typically the program's entry point as the root and below it, each statement in the program, represented as a node with children for each statement that has to be computed to compute the first statement. (For example, something like "int a = 2 * b;" would look something like this:


:= operator
/ \
(look up a's address in * operator
your vtable) / \
2 (Look up b's address in your vtable)





(above the assignment operator, you'd have a parent node representing the current code line that the above statement is part of, and with another child node representing the subsequent code line.)

The parser can be made with Yacc or Bison or other similar tools.

3: Simply traverse the tree, inserting the appropriate code for each node (and maintain the neccessary tables of which functions or variables exist, and their values).
You sometimes use some kind of intermediate code, to separate the parsing stage from the actual code generation, and so you can change details in the language/code generation (or even output to an entirely different target language, without having to mess with the parser, and vice versa.

Anyway, this is just to give a very rough overview of what happens. I left out all the complicated stuff though. If you want to make a compiler, better buy a book or two on the subject (or read a lot of tutorials)

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBSmall
Quote:
Original post by Julio
Believe it or not they have compilers for writing compilers. I saw an article about it a few years ago on an old site. I'm sure you can use a search engine and come up with some good information.

Only partly. There are tools that generate lexers, and tools that generate parsers (search for 'lex', 'yacc', 'flex', 'bison', 'antlr', 'spirit' or the 'gold parser generator' for examples)

That still leaves a lot of stuff you have to write yourself.

John B

Don't forget JavaCC.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!