How to write a C++ compiler

S

Simon Kwong

Hi,

I am learning c++ and would like to write a simple C++ compiler. How should
I start?

Please help.

Simon
 
L

Leor Zolman

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
(e-mail address removed)
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
 
M

Mike Wahler

Simon Kwong said:
Hi,

I am learning c++ and would like to write a simple C++ compiler.

No such thing.
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
 
A

Alf P. Steinbach

* "Simon Kwong said:
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".


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/>.
 
D

Dave

Simon Kwong said:
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...).
 
A

Alf P. Steinbach

* "Dave said:
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.
 
E

E. Robert Tisdale

Simon said:
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.
 
M

Mike Wahler

Alf P. Steinbach said:
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
 
C

Claudio Puviani

Alf P. Steinbach said:
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
 
J

Julie

Simon said:
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.
 
D

Dan Cernat

Makhno said:
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.
 
J

Julie

Dan said:
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.
 
C

Claudio Puviani

Julie said:
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/Oo...8230&wbflg=N&page=/booksearch/isbnInquiry.asp.

Claudio Puviani
 
L

Leor Zolman

I see, Bjarne can't seem to make up his mind whether the terms "C++" and
"\"C with Classes\"" are interchangeable.

Why do you think that? His statement:

" 'C with Classes' was a C dialect that became the immediate ancestor
to C++..."

couldn't be a whole lot clearer...


Leor Zolman
BD Software
(e-mail address removed)
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
 
S

Stewart Gordon

Leor Zolman wrote:

Why do you think that? His statement:

" 'C with Classes' was a C dialect that became the immediate ancestor
to C++..."

couldn't be a whole lot clearer...

Finish reading it. Then you'll see that he goes from stating that the
first C++ compiler was in C++, to telling us it was in "C with Classes".

Stewart.
 
L

Leor Zolman

Leor Zolman wrote:



Finish reading it. Then you'll see that he goes from stating that the
first C++ compiler was in C++, to telling us it was in "C with Classes".

I did the first time, and I just did again. I still have no clue what
you're thinking.

Bjarne says he wrote the first version of C++ in "C with Classes".

Leor Zolman says:
"I then wrote the first version (and all the others, for that matter)
of BDS C in 8080 assembly language."

Am I confused whether the terms "C" and "8080 assembly language" are
interchangeable?

Leor Zolman
BD Software
(e-mail address removed)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top