 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Simon Kwong Guest
|
Posted: Wed Mar 03, 2004 8:25 pm Post subject: How to write a C++ compiler |
|
|
Hi,
I am learning c++ and would like to write a simple C++ compiler. How should
I start?
Please help.
Simon
|
|
| Back to top |
|
 |
Leor Zolman Guest
|
Posted: Wed Mar 03, 2004 8:50 pm Post subject: Re: How to write a C++ compiler |
|
|
On Thu, 4 Mar 2004 04:25:27 +0800, "Simon Kwong" <sktw (AT) hotmail (DOT) com> wrote:
| Quote: | Hi,
I am learning c++ and would like to write a simple C++ compiler. How should
I start?
Please help.
|
Start by writing a simple <something simpler than C++> compiler. The first
compiler I wrote (back in 1978) was for a subset of BASIC that generated
intermediate code and then interpreted it. I was able to cannibalize a few
pieces of that when I tackled writing a small C compiler the next year.
After that I swore off writing compilers for the rest of my life
-leor
Leor Zolman
BD Software
[email]leor (AT) bdsoft (DOT) com[/email]
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Wed Mar 03, 2004 8:51 pm Post subject: Re: How to write a C++ compiler |
|
|
"Simon Kwong" <sktw (AT) hotmail (DOT) com> wrote
| Quote: | Hi,
I am learning c++ and would like to write a simple C++ compiler.
|
No such thing.
| Quote: | How should
I start?
|
With extensive research. Look up terms 'lexical' 'analysis',
'parsing', 'grammar', etc.
BTW why do you feel you'll be capable of writing a C++
compiler before you've actually learned (very well) how to
*use* the language?
Also note that there's no requirement to *use* C++ (or any other
particular language) to write a C++ compiler. (Although you'll
almost certainly need to learn assembly language for the platform(s)
you intend to target.)
-Mike
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Mar 03, 2004 8:55 pm Post subject: Re: How to write a C++ compiler |
|
|
* "Simon Kwong" <sktw (AT) hotmail (DOT) com> schriebt:
| Quote: |
I am learning c++ and would like to write a simple C++ compiler.
|
A C++ compiler is definitely a non-trivial piece of software.
If you're learning C++ then you have no chance of creating a C++ compiler
in C++.
It's like: "I'm learning science and would like to make a moon rocket".
| Quote: | How should I start?
|
Many universities offer courses where you get to create a compiler.
The "Dragon book",
<url: http://www.amazon.com/exec/obidos/tg/detail/-/0201100886/002-2510887-2360043?v=glance>
is a good place to start for learning about writing compilers.
For learning C++, on the other hand, it very much depends on what you already
know. If you are proficient in some other programming language then the
recommended book is "Accelerated C++" by Koenig and, darn, memory failure. Moi?
Otherwise take a look at e.g. "You can do it!" by Francis Glassborow. Not that
I've read the book but Francis is a very competent person who writes very clearly
and honestly. See <url: http://www.spellen.org/youcandoit/>.
|
|
| Back to top |
|
 |
Dave Guest
|
Posted: Wed Mar 03, 2004 9:09 pm Post subject: Re: How to write a C++ compiler |
|
|
"Simon Kwong" <sktw (AT) hotmail (DOT) com> wrote
| Quote: | Hi,
I am learning c++ and would like to write a simple C++ compiler. How
should
I start?
Please help.
Simon
|
Compilers in general, and C++ compilers in particular, are among the most
complex pieces of software out there. If what you implement is truly C++ in
its entirety, you will have created a *very* non-trivial piece of software.
A small subset of C++ would be much more manageable. Specifically, I would
leave out inheritance, templates, namespaces and exceptions for sure and
would pick-and-choose other stuff to leave out. I suggest starting by
taking a university course in compiler construction. Make sure it's a
course that involves a project (any decent course will). After such a
course, you will realize that you are still a long way from being able to
write a C++ compiler.
As a point of interest and curiosity, can anybody think of problem domains
more complex than full-fledged optimizing compilers for extremely robust
languages such as C++? Of course, "complex" is subjective, but I suppose a
reasonable interpretation would be that in addition to being large (i.e. a
large number of lines of code are necessary) an in-depth knowledge of
several areas of Computer Science is required (formal languages, parsing,
classic algorithms / data structures, architecture, etc...).
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Mar 03, 2004 9:24 pm Post subject: Re: How to write a C++ compiler |
|
|
* "Dave" <better_cs_now (AT) yahoo (DOT) com> schriebt:
| Quote: |
As a point of interest and curiosity, can anybody think of problem domains
more complex than full-fledged optimizing compilers
|
It's difficult, but yes, I'd say a real-time operating system would qualify.
In the other direction, perhaps the OP is interested in something _easier_
than a full-fledged optimizing compiler.
The first useful language I made was a hierarchical thing for defining function
keys on Hewlett Packard text terminals. It was part of a generalized help
system. A "program" consisted of a simple text file with nested definitions,
which were interpreted once and "compiled" into internal data structures. Of
course, today we'd just use someone else's XML parser or such. But I think for
someone learning C++ even using someone else's XML parses might be challenge
enough, and it might actually be easier to just write the interpreter directly.
|
|
| Back to top |
|
 |
E. Robert Tisdale Guest
|
Posted: Wed Mar 03, 2004 9:32 pm Post subject: Re: How to write a C++ compiler |
|
|
Simon Kwong wrote:
| Quote: | I am learning c++ and would like to write a simple C++ compiler.
How should I start?
|
Excellent idea!
Send email to Bjarne Stroustrup.
Ask him for any good leads on text books
for compiler design and implementation
that feature a simple C++ compiler design.
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Wed Mar 03, 2004 9:36 pm Post subject: Re: How to write a C++ compiler |
|
|
"Alf P. Steinbach" <alfps (AT) start (DOT) no> wrote
| Quote: | * "Simon Kwong" <sktw (AT) hotmail (DOT) com> schriebt:
For learning C++, on the other hand, it very much depends on what you
already
know. If you are proficient in some other programming language then the
recommended book is "Accelerated C++" by Koenig and, darn, memory failure.
Moi? |
Andrew Koenig & Barbara Moo.
www.acceleratedcpp.com
I'll second Alf's recommedation of this great book.
-Mike
|
|
| Back to top |
|
 |
Claudio Puviani Guest
|
Posted: Wed Mar 03, 2004 11:45 pm Post subject: Re: How to write a C++ compiler |
|
|
"Alf P. Steinbach" <alfps (AT) start (DOT) no> wrote
| Quote: | * "Dave" <better_cs_now (AT) yahoo (DOT) com> schriebt:
As a point of interest and curiosity, can anybody think of problem domains
more complex than full-fledged optimizing compilers
It's difficult, but yes, I'd say a real-time operating system would qualify.
|
Having worked on both, I can tell you without hesitation that writing a
real-time operating system is far less complex than writing a compiler for a
non-trivial language. Once you get the scheduler and IPC right, the rest falls
in place, though what's left is time consuming. I don't see any small subset of
C++ that would allow all the other features to become trivial to implement.
Claudio Puviani
|
|
| Back to top |
|
 |
Makhno Guest
|
Posted: Wed Mar 03, 2004 11:59 pm Post subject: Re: How to write a C++ compiler |
|
|
| Quote: | Also note that there's no requirement to *use* C++ ...
to write a C++ compiler.
|
That's a relief. Would've been tricky writing the first one otherwise.
|
|
| Back to top |
|
 |
Julie Guest
|
Posted: Thu Mar 04, 2004 12:02 am Post subject: Re: How to write a C++ compiler |
|
|
Simon Kwong wrote:
| Quote: |
Hi,
I am learning c++ and would like to write a simple C++ compiler. How should
I start?
Please help.
Simon
|
If you are set on writing a C++ compiler, you might want to look at an open
source compiler, and slowly modify it until you get the hang of the language
and the compiler, then start on your own.
Conversely, something that may prove as much (if not more) of a learning
experience is to write a C++ front-end that takes (simple) C++ code as input,
and outputs C code. This is how C++ was originally implemented. This would
give you a pretty good immersion into C++, you would be able to understand the
implementation details of the language, and you wouldn't have to get tied to a
particular architecture or worry about machine code generation, linking, and
similar that have little to do w/ the language.
|
|
| Back to top |
|
 |
Dan Cernat Guest
|
Posted: Thu Mar 04, 2004 2:14 am Post subject: Re: How to write a C++ compiler |
|
|
"Makhno" <root (AT) 127 (DOT) 0.0.1> wrote
| Quote: | Also note that there's no requirement to *use* C++ ...
to write a C++ compiler.
That's a relief. Would've been tricky writing the first one otherwise.
AFAIK the first Pascal compiler was written in Pascal. They compiled it by |
hand. I guess it wasn't very complicated. Could be a urban legend, though.
|
|
| Back to top |
|
 |
Julie Guest
|
Posted: Thu Mar 04, 2004 2:31 am Post subject: Re: How to write a C++ compiler |
|
|
Dan Cernat wrote:
| Quote: |
"Makhno" <root (AT) 127 (DOT) 0.0.1> wrote in message
news:c25rlj$3gi$1 (AT) newsg4 (DOT) svr.pol.co.uk...
Also note that there's no requirement to *use* C++ ...
to write a C++ compiler.
That's a relief. Would've been tricky writing the first one otherwise.
AFAIK the first Pascal compiler was written in Pascal. They compiled it by
hand. I guess it wasn't very complicated. Could be a urban legend, though.
|
Bootstrapping. Not the quickest way to work on a compiler I presume, but
arguably a good way to test your code and compiler at the same time.
From what I recall, a few of the one-man-compiler-teams back in the 80's used
this method -- Walter Bright and Zortech C/C++ iirc, et al.
|
|
| Back to top |
|
 |
Claudio Puviani Guest
|
Posted: Thu Mar 04, 2004 5:05 pm Post subject: Re: How to write a C++ compiler |
|
|
"Julie" <julie (AT) aol (DOT) com> wrote
| Quote: | Dan Cernat wrote:
"Makhno" <root (AT) 127 (DOT) 0.0.1> wrote in message
news:c25rlj$3gi$1 (AT) newsg4 (DOT) svr.pol.co.uk...
Also note that there's no requirement to *use* C++ ...
to write a C++ compiler.
That's a relief. Would've been tricky writing the first one otherwise.
AFAIK the first Pascal compiler was written in Pascal. They compiled it by
hand. I guess it wasn't very complicated. Could be a urban legend, though.
Bootstrapping. Not the quickest way to work on a compiler I presume, but
arguably a good way to test your code and compiler at the same time.
From what I recall, a few of the one-man-compiler-teams back in the 80's used
this method -- Walter Bright and Zortech C/C++ iirc, et al.
|
In the late 1970s, Drs Yuen and Chung bootstrapped their Tiny Pascal by writing
a limited version in Microsoft BASIC and then rewriting the compiler in Tiny
Pascal. They also had a BASIC p-code to Z80 translator and a native Z80 p-code
interpreter. The whole adventure is chronicled in the excellent, but no longer
published, "The Byte Book of Pascal".
A few copies still seem to be in circulation in various used book stores:
http://search.barnesandnoble.com/OopBooks/oopResultsIsbn.asp?wid=48775220&origean=9780070378230&wbflg=N&page=%2Fbooksearch%2FisbnInquiry%2Easp.
Claudio Puviani
|
|
| Back to top |
|
 |
KTC Guest
|
Posted: Thu Mar 04, 2004 11:36 pm Post subject: Re: How to write a C++ compiler |
|
|
"Makhno" <root (AT) 127 (DOT) 0.0.1> for some reason wrote:
| Quote: | Also note that there's no requirement to *use* C++ ...
to write a C++ compiler.
That's a relief. Would've been tricky writing the first one
otherwise.
|
The first C++ compiler was written using C++ !
http://www.research.att.com/~bs/bs_faq2.html#bootstrapping
--
Experience is a good school but the fees are high.
- Heinrich Heine
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|