473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
Jul 22 '05 #1
18 3133
On Thu, 4 Mar 2004 04:25:27 +0800, "Simon Kwong" <sk**@hotmail.com> wrote:
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
Simon


Leor Zolman
BD Software
le**@bdsoft.com
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
Jul 22 '05 #2
"Simon Kwong" <sk**@hotmail.com> wrote in message
news:c2**********@hfc.pacific.net.hk...
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
Jul 22 '05 #3
* "Simon Kwong" <sk**@hotmail.com> schriebt:

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

Jul 22 '05 #4

"Simon Kwong" <sk**@hotmail.com> wrote in message
news:c2**********@hfc.pacific.net.hk...
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...).
Jul 22 '05 #5
* "Dave" <be***********@yahoo.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.

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.

Jul 22 '05 #6

"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
* "Simon Kwong" <sk**@hotmail.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
Jul 22 '05 #7
Simon Kwong wrote:
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.

Jul 22 '05 #8
"Alf P. Steinbach" <al***@start.no> wrote
* "Dave" <be***********@yahoo.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
Jul 22 '05 #9
> 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.
Jul 22 '05 #10
Simon Kwong wrote:

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.
Jul 22 '05 #11

"Makhno" <ro**@127.0.0.1> wrote in message
news:c2**********@newsg4.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.
Jul 22 '05 #12
Dan Cernat wrote:

"Makhno" <ro**@127.0.0.1> wrote in message
news:c2**********@newsg4.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.
Jul 22 '05 #13
"Julie" <ju***@aol.com> wrote
Dan Cernat wrote:

"Makhno" <ro**@127.0.0.1> wrote in message
news:c2**********@newsg4.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/Oop...nInquiry%2Easp.

Claudio Puviani
Jul 22 '05 #14
KTC
"Makhno" <ro**@127.0.0.1> for some reason wrote:
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_f...#bootstrapping

--
Experience is a good school but the fees are high.
- Heinrich Heine
Jul 22 '05 #15
KTC wrote:
"Makhno" <ro**@127.0.0.1> for some reason wrote: <snip> The first C++ compiler was written using C++ !

http://www.research.att.com/~bs/bs_f...#bootstrapping


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

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #16
On Fri, 05 Mar 2004 17:30:16 +0000, Stewart Gordon <sm*******@yahoo.com>
wrote:
KTC wrote:
"Makhno" <ro**@127.0.0.1> for some reason wrote:

<snip>
The first C++ compiler was written using C++ !

http://www.research.att.com/~bs/bs_f...#bootstrapping


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
le**@bdsoft.com
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
Jul 22 '05 #17
Leor Zolman wrote:

<snip>
http://www.research.att.com/~bs/bs_f...#bootstrapping


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


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.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #18
On Fri, 05 Mar 2004 18:17:22 +0000, Stewart Gordon <sm*******@yahoo.com>
wrote:
Leor Zolman wrote:

<snip>
http://www.research.att.com/~bs/bs_f...#bootstrapping

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


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?
Stewart.


Leor Zolman
BD Software
le**@bdsoft.com
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
Jul 22 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Anton Gavrilov | last post by:
Hi all, I seek your advice on where to start if I want to write a compiler for a toy C-like language I invented (or, rather, am in the process of inventing). Yes, yes, I know I'm crazy and the...
7
by: Randy Yates | last post by:
Hi, I work in an embedded environment in which we often use a mix of C and assembly code. Thus a recurring requirement is to be able to take a C header file with structure definitions as input...
5
by: Confused User | last post by:
I am working on device that utilizes a Motorola 68HC16 microcontroller. I am using an old unsupported piece of crap Whitesmith's / Intermetrics / Tasking compiler. The embedded compiler business...
34
by: zhuyin.nju | last post by:
can some one tell that how to use pure c to write OO programs? thanks in advanced!
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
0
by: brm6546545 | last post by:
I am trying to get some C code to work on .net C++ compiler int writeDatagram(int sock, char *buf, int length) { return write(sock, buf, length); } void killConnection(int sockd) {...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
4
by: AshokG | last post by:
Hi, In .NET there is attribute named ObsoleteAttribute which is derived from System.Attribute Class. This attribute when applied to a target (class or method etc.) gives a compiler...
7
by: Bryan Parkoff | last post by:
Can you please advise me how to write two separated objects? Sometimes, two objects have the same variable name and function name, but they have different algorithm to perform. The C++ Compiler...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.