Writing compiler 
Author Message
 Writing compiler

Any good texts on writing compilers?


Sat, 07 Jun 2003 18:59:03 GMT  
 Writing compiler
Hello!


Quote:

>Any good texts on writing compilers?

Many. Just check the comp.compilers archive and home page:
http://compilers.iecc.com/

There, especially check the comp.compilers FAQ which lists some books.

Kind regards,

Hannah.



Sat, 07 Jun 2003 23:35:18 GMT  
 Writing compiler
On Tue, 19 Dec 2000 10:59:03 GMT, "bjarnius"

Quote:

>Any good texts on writing compilers?

I dont have texts, but I assume you are starting to write a compiler
of your own. If you want to do it all yourself, its a lot of work. I
have tried once and produced a medium fast compiler that was not so
good to maintain in case of grammar changes. Then if found a compiler
construction toolkit on the net:
PCCTS with ANTLR ( http://www.antlr.org/)
Its easy to use und good to controll. Much better than lex and yac.

Hope this helps,
 Philipp



Sun, 08 Jun 2003 04:25:43 GMT  
 Writing compiler

Quote:

> On Tue, 19 Dec 2000 10:59:03 GMT, "bjarnius"

> >Any good texts on writing compilers?

> I dont have texts, but I assume you are starting to write a compiler
> of your own. If you want to do it all yourself, its a lot of work. I
> have tried once and produced a medium fast compiler that was not so
> good to maintain in case of grammar changes. Then if found a compiler
> construction toolkit on the net:
> PCCTS with ANTLR ( http://www.antlr.org/)
> Its easy to use und good to controll. Much better than lex and yac.

Why pccts/antlr is better than yacc ?

Quote:

> Hope this helps,
>  Philipp

--
Yoann  Padioleau,  INSA de Rennes, France,   http://www.irisa.fr/prive/padiolea
Opinions expressed here are only mine. Je n'cris qu' titre personnel.
**____   Get Free. Be Smart.  Simply use Linux and Free Software.   ____**


Tue, 10 Jun 2003 00:46:17 GMT  
 Writing compiler
On 21 Dec 2000 17:46:17 +0100, Yoann Padioleau

Quote:


>> PCCTS with ANTLR ( http://www.antlr.org/)
>> Its easy to use und good to controll. Much better than lex and yac.

>Why pccts/antlr is better than yacc ?

I have tried to use lex and yacc on my special kind of language and
got nothing than problems. Its was realy hard to write rules so that
yacc could parse the language without errors. Error handling was eaven
worse. How do you tell the user that he has to recompile and recompile
his code to find only one syntax error per compilation since yacc
makes it hard to do it otherwise. And error messages like 'I have
found an error somewhere near line 354' dont make error finding
easier. And if you solve this problem, you only have an parser, not an
compiler. Now you must define actions for the rules. But the rules
dont lool like what you have thought of when constructing your
language since for yacc they need to be heavily addapted.

So I tried to wrote my own parser generator and compiler, but this did
not work either. Some day I found pccts and tried it. It took my 2
days (!) to write the lexer and parser to recognize my language. After
a week i could construct a usable syntax tree and had fully features
syntax error handling (line, column, expected rule and found token).
After 3 weeks I had adapted the code generation of my hand written
compiler. And the generated compiler is fast. Now it takes 4 secs on a
200MHz processor to compile 600kb of code. Thats why I think pccts is
much better than yacc.

Here some more infos about pccts:

pccts can generate code for different programming languages. I have
only used the c/c++ version.

With antlr you describe lexer and parser. Token declaration use
regular expressions. For each token you can define an action that is
executed if the token is recognized. With a set of special lexer
functions you can controll the lexer behaviour. There functions to
track lines and collumns, skip a token, append or replace recognized
text and so on. Here a example:

#token ID "[a-zA-Z][a-zA-Z0-9]*"
#token  "//(~[\n \r])* (\n\r | \n | \r\n | \r)"
     << skip(); newline(); set_begcol(0); set_endcol(0); >>

Its possible to define your own functions within the grammar file and
call it from parser actions. On recognition errors special functions
are called for further error processing. Normaly just dumping errors
to stderr they can be overwritten for more complex error handling. The
paresed tokens are stored within an object of a userdefined class
(c++) derived from an abstract class. If you dont need this you can
use standard classes too. The lexer supports different 'lexer classes'
and switching between them. This makes it possible to handle the same
input differently dependend on the contex, for example switching
between normal code and block comments. The parsed tokens are
available as an stream.

The stream is used by the parser. For the parser rules are defined
within ebnf. Here actions can be used too. Its possible to parse rules
dependend on a semantic action, wich makes defining the rules
somethimes easer. So you may choose the right rule dependent on seeing
an variable or method idenfifier. With the used parsing technology,
wich makes it possible to use an ifinite lookahead if needed even
complex grammers can easy be implemented.
There ar two possible methods to use the rules: They can behave like
funktions taking arguments and returnning an result:

start
  : <<int r;>> // init-action declares local var r
expr[3,4] > [r] <<printf("result %d\n");>>
  ;
expr[int a, int b] > [int result]
  : i:INT <<$result = $a+$b+atoi($i->getText());>>
  ;

Or to construct an syntax tree from the token streem. This can be done
automatic trough the rule structure, or 'manualy' through defining
actions to construct the tree. If you use automatic construction its
possible to surpress tokens or choose or rotate the tree so that the
actual token is the new root of the rule. With this methods its very
simple to construct the desired syntax tree.

The resulting tree can be processed with a tree walker. To construct
one simple use the programm 'sorcerer'. The structure of the tree is
defines with special rules. Within the rule actions you can now
transform the tree for further processing or do other stuff, normaly
compiler actions like semantic checking and code generation.

I hope this description answers your question. If you want more to
know read the pccts book that is available as pdf for download.

Merry Christmas,
 Philipp



Wed, 11 Jun 2003 01:08:46 GMT  
 Writing compiler
I specifically asked for a text on how to write compilers, not a program to
do it for me. Damn now I got some bread stuck in my keyboard.


Wed, 11 Jun 2003 18:31:38 GMT  
 Writing compiler

Quote:

> I specifically asked for a text on how to write compilers, not a program to
> do it for me. Damn now I got some bread stuck in my keyboard.

If you can find a copy of _Dr. Dobb's Toolbook of C_, it's got a
description, with complete source, of the Small-C compiler.  That's
suitable for someone who is interested in constructing simple, or even
not-so-simple compilers.  However, most compilers of recent design of
which I am aware use at least two passes internally, with the first
pass generating a DAG (Directed Acyclic Graph which is kind of like a
tree structure) from the source file and the second pass traversing
the DAG to generate the code.  Small-C is a one-pass compiler where the
code generation is done during the parsing of the source code.

If you're interested in the more modern approach, there's _A
Retargetable C Compiler: Design and Implementation_ by Fraser and
Hanson.  This book discusses the internal workings of the lcc compiler
which is freely available around.  It's more recent than the DDJ
book, but I don't know if it's still in print or not.  I got my copy
from a used bookstore several years ago.

Another book that might be interesting is Holub's _Compiler Design in
C_ which I've never bought although I've wanted to on a number of
occasions.  (Maybe this year.  However the second edition of _Unix
Network Programming_ is ahead of it on the list.)

On the other hand, a book like _A Compiler Generator for Microcomputers_
by Rechenberg and Mossenbock (with apologies--I don't know how to get
umlaut's over the o's in the second name) discusses not only how the
coco software is used, but also how it works.  (Coco is a lex/yacc
equivalent originally for Modula-2.  I mention it because it is not C
oriented and because the description language has many advantages when
compared to yacc.)

Although I can understand the desire to do it manually once, just to
learn how it's done, there is nothing particularly noble or particularly
beneficial in most cases to building your own parser.  While the
program structure can closely match the syntactical structure of the
language, simplifying the program's design, things like error recovery
quickly become troublesome.  I just finished writing a compiler for a
display description language and although my first inclination was to
build my own parser, it quickly became apparent that bison was a more
appropriate tool because it includes mechanisms for reporting and
handling errors.



Thu, 12 Jun 2003 00:12:01 GMT  
 Writing compiler
The monthly comp.compilers FAQ lists a number of compiler books
(2/3 of the way into the doc):

http://www.landfield.com/faqs/compilers/faq/

The best list of compiler texts (including Amazon's ratings) that I've seen is:

http://www.softpanorama.org/Bookshelf/compilers.shtml#News

There's also:

http://www.flathill.com/compilers/
http://www.compilerconnection.com/books/books.htm

    Randy

Quote:

> I specifically asked for a text on how to write compilers, not a program to
> do it for me. Damn now I got some bread stuck in my keyboard.

--
Randy Crawford

http://www.engin.umich.edu/labs/cpc


Thu, 12 Jun 2003 14:46:26 GMT  
 Writing compiler


Quote:
> Any good texts on writing compilers?

You might start here:

   http://iecc.com/compilers/crenshaw

Jim Lawless                 | MailSend - Cmd line e-mail sender

http://www.radiks.net/jimbo | BPL - Batch Prog. Language

Sent via Deja.com
http://www.deja.com/



Sat, 14 Jun 2003 23:19:34 GMT  
 Writing compiler

Quote:

> Any good texts on writing compilers?

Pat Terry's book "Compilers and Compiler Generators" is available online:

  http://www.scifac.ru.ac.za/compilers/

and it can also be downloaded from there in various formats. It's also
available on the BURKS CDs together with some compiler-writing tools:

 http://burks.bton.ac.uk/burks/progging/progtech/#compilers

-----------------------------------------------------------------

 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------



Fri, 27 Jun 2003 20:05:16 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Help writing compiler

2. Possible to write compiler to Java VM?

3. What language(s) are used by DEC to write compilers for the VAX?

4. Possible to write compiler to Java VM? (I volunteer to summarize)

5. Writing an Eiffel compiler as a course project?

6. Writing an Eiffel compiler

7. Anyone ever write a compiler in LabVIEW

8. looking for C compiler written in Forth

9. Newsgroups for C++ Programmers Using Borland's Compiler Writing Programs for windows 95

10. A simple SSA-based compiler written in Dylan

11. Pascal Compiler Written in Oberon

12. Other languages compilers written in Oberon

 

 
Powered by phpBB® Forum Software