On Thu, 4 Mar 2004 04:25:27 +0800, "Simon Kwong" <sktw@hotmail.c om> wrote:
[color=blue]
>Hi,
>
>I am learning c++ and would like to write a simple C++ compiler. How should
>I start?
>
>Please help.[/color]
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
[color=blue]
>Simon
>[/color]
"Simon Kwong" <sktw@hotmail.c om> wrote in message
news:c25eis$sej $1@hfc.pacific. net.hk...[color=blue]
> Hi,
>
> I am learning c++ and would like to write a simple C++ compiler.[/color]
No such thing.
[color=blue]
> How should
> I start?[/color]
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.)
* "Simon Kwong" <sktw@hotmail.c om> schriebt:[color=blue]
>
> I am learning c++ and would like to write a simple C++ compiler.[/color]
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".
[color=blue]
> How should I start?[/color]
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=glanc e>
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 "Accelerate d 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/>.
"Simon Kwong" <sktw@hotmail.c om> wrote in message
news:c25eis$sej $1@hfc.pacific. net.hk...[color=blue]
> Hi,
>
> I am learning c++ and would like to write a simple C++ compiler. How[/color]
should[color=blue]
> I start?
>
> Please help.
>
> Simon
>
>[/color]
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...).
* "Dave" <better_cs_now@ yahoo.com> schriebt:[color=blue]
>
> As a point of interest and curiosity, can anybody think of problem domains
> more complex than full-fledged optimizing compilers[/color]
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.
"Alf P. Steinbach" <alfps@start.no > wrote in message
news:40464411.4 03807468@news.i ndividual.net.. .[color=blue]
> * "Simon Kwong" <sktw@hotmail.c om> schriebt:
>
> For learning C++, on the other hand, it very much depends on what you[/color]
already[color=blue]
> know. If you are proficient in some other programming language then the
> recommended book is "Accelerate d C++" by Koenig and, darn, memory failure.[/color]
Moi?
Simon Kwong wrote:
[color=blue]
> I am learning c++ and would like to write a simple C++ compiler.
> How should I start?[/color]
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.
"Alf P. Steinbach" <alfps@start.no > wrote[color=blue]
> * "Dave" <better_cs_now@ yahoo.com> schriebt:[color=green]
> >
> > As a point of interest and curiosity, can anybody think of problem domains
> > more complex than full-fledged optimizing compilers[/color]
>
> It's difficult, but yes, I'd say a real-time operating system would qualify.[/color]
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.
Simon Kwong wrote:[color=blue]
>
> Hi,
>
> I am learning c++ and would like to write a simple C++ compiler. How should
> I start?
>
> Please help.
>
> Simon[/color]
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.
"Makhno" <root@127.0.0.1 > wrote in message
news:c25rlj$3gi $1@newsg4.svr.p ol.co.uk...[color=blue][color=green]
> > Also note that there's no requirement to *use* C++ ...
> > to write a C++ compiler.[/color]
>
> That's a relief. Would've been tricky writing the first one otherwise.
>[/color]
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.
Dan Cernat wrote:[color=blue]
>
> "Makhno" <root@127.0.0.1 > wrote in message
> news:c25rlj$3gi $1@newsg4.svr.p ol.co.uk...[color=green][color=darkred]
> > > Also note that there's no requirement to *use* C++ ...
> > > to write a C++ compiler.[/color]
> >
> > That's a relief. Would've been tricky writing the first one otherwise.
> >[/color]
> 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.[/color]
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.
"Julie" <julie@aol.co m> wrote[color=blue]
> Dan Cernat wrote:[color=green]
> >
> > "Makhno" <root@127.0.0.1 > wrote in message
> > news:c25rlj$3gi $1@newsg4.svr.p ol.co.uk...[color=darkred]
> > > > 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.
> > >[/color]
> > 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.[/color]
>
> 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.[/color]
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".
"Makhno" <root@127.0.0.1 > for some reason wrote:
[color=blue][color=green]
>> Also note that there's no requirement to *use* C++ ...
>> to write a C++ compiler.[/color]
>
> That's a relief. Would've been tricky writing the first one
> otherwise.
>
>[/color]
Comment