Struct in C and C++

  • Thread starter vishnupriya.sureshbabu
  • Start date
V

Victor Bazarov

Markus said:
Greg wrote:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for
one fact: the first C++ compiler (CFront) did not actually
compile C++ source code (including its own) directly - rather it
first translated C++ source code into C and then compiled the
translated sources with a C compiler. Pretty simple, isn't it?

[...]

The very first version of Cfront was probably written in plain C.

Exactly my point. Thank you!
It
would just add one C with classes feature. The next version would
already make use of this one feature to implement the next feature.
Rinse repeat... This is called bootstrapping.
[..]

V
 
K

Kai-Uwe Bux

Markus said:
Victor said:
Greg said:
Victor Bazarov wrote:
Greg wrote:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first
translated C++ source code into C and then compiled the translated
sources with a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are
pretty much the same thing. So, how could it have been written in
C++? What would be used to translate its own code from C++ "into
C"? And if it wasn't used itself, what was used? And if, in fact,
some other thing was used, what do you call it if not "C++
translator"? And what was that thing written in? Catch my drift?
There is no "chicken and egg" problem. The "chicken and egg"
conundrum exists when there is circular dependency. There is none
here. First C++ compiler/translator was most likely written in C
(or C With Classes or some other predecessor of the "real C++").
After than each next version of a C++ compiler is written in C++
probably (because the C++ compiler programmers know C++ the best),
and any new advanced features the next version implements are simply
not used to write it (because the compiler does not support them).
That's just evolution of tools. For example, what debugger is used
to debug a debugger?

The first C++ compiler was written in C++. It compiled itself. From
Wikipedia:

"As Cfront was written in C++, it was a challenge to bootstrap on a
machine without a C++ compiler/translator. Along with the Cfront C++
sources,

...let's call them 'a'..
a special "half-preprocessed" version of the C code

...let's call it 'b'...
resulting
from compiling Cfront with itself

[huh! very interesting!]
was also provided. This C code was
to be compiled with the native C compiler, and the resulting
executable could then be used to compile the Cfront C++ sources."

I wasn't aware (although I now think it was unfounded) that Wikipedia
contains such convoluted and misleading "explanations".

How could Cfront compile itself?

struct Cfront {
CfrontSources a;
HalfProcessedC b;

Cfront() : a(typed_up()), b(Cfont().compile(Cfront())) {}
};

Take a look at the constructor initialiser list. Notice the recursion?
It's infinite!

The very first version of Cfront was probably written in plain C. It
would just add one C with classes feature. The next version would
already make use of this one feature to implement the next feature.
Rinse repeat... This is called bootstrapping.

Eventually you end up with a fully fledged C++ compiler that compiles
itself.

Close, but why guess? Turns out, there is a first-hand account:

<quote "http://public.research.att.com/~bs/bs_faq.html#bootstrapping">
Which language did you use to write C++?

The first C++ compiler (Cfront) was written in C++. To build that, I first
used C to write a "C with Classes"-to-C preprocessor. "C with Classes" was
a C dialect that became the immediate ancestor to C++. That preprocessor
translated "C with Classes" constructs (such as classes and constructors)
into C. It was a traditional preprocessor in that it didn't undestand all
of the language, left most of the type checking for the C compiler to do,
and translated individual constructs without complete knowledge. I then
wrote the first version of Cfront in "C with Classes".

Cfront was a traditional compiler that did complete syntax and semantic
checking of the C++ source. For that, it had a complete parser, built
symbol tables, and built a complete internal tree representation of each
class, function, etc. It also did some source level optimization on its
internal tree representation of C++ constructs before outputting C. The
version that generated C, did not rely on C for any type checking. It
simply used C as an assembler. The resulting code was uncompromisingly
fast. For more information, see D&E.
</quote>


Best

Kai-Uwe Bux
 
G

Greg

Markus said:
Victor said:
Greg said:
Victor Bazarov wrote:
Greg wrote:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first
translated C++ source code into C and then compiled the translated
sources with a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are
pretty much the same thing. So, how could it have been written in
C++? What would be used to translate its own code from C++ "into
C"? And if it wasn't used itself, what was used? And if, in fact,
some other thing was used, what do you call it if not "C++
translator"? And what was that thing written in? Catch my drift?
There is no "chicken and egg" problem. The "chicken and egg"
conundrum exists when there is circular dependency. There is none
here. First C++ compiler/translator was most likely written in C
(or C With Classes or some other predecessor of the "real C++").
After than each next version of a C++ compiler is written in C++
probably (because the C++ compiler programmers know C++ the best),
and any new advanced features the next version implements are simply
not used to write it (because the compiler does not support them).
That's just evolution of tools. For example, what debugger is used
to debug a debugger?

The first C++ compiler was written in C++. It compiled itself. From
Wikipedia:

"As Cfront was written in C++, it was a challenge to bootstrap on a
machine without a C++ compiler/translator. Along with the Cfront C++
sources,

...let's call them 'a'..
a special "half-preprocessed" version of the C code

...let's call it 'b'...
resulting
from compiling Cfront with itself

[huh! very interesting!]
was also provided. This C code was
to be compiled with the native C compiler, and the resulting
executable could then be used to compile the Cfront C++ sources."

I wasn't aware (although I now think it was unfounded) that Wikipedia
contains such convoluted and misleading "explanations".

How could Cfront compile itself?

struct Cfront {
CfrontSources a;
HalfProcessedC b;

Cfront() : a(typed_up()), b(Cfont().compile(Cfront())) {}
};

Take a look at the constructor initialiser list. Notice the recursion?
It's infinite!

The very first version of Cfront was probably written in plain C. It
would just add one C with classes feature. The next version would
already make use of this one feature to implement the next feature.
Rinse repeat... This is called bootstrapping.

Eventually you end up with a fully fledged C++ compiler that compiles
itself.

What Wikipedia describes is not the bootstrapping process but what was
delivered to people so they could compile their own Cfront compiler.
The half preprocessed stuff would probably compile into a Cfront
version that would not implement all features but enough to compile the
Cfront C++ source.

But you've completely forgotten about portability. Bootstrapping is
fine for a single platform but what happens when you want to bring the
only C++ compiler in the world to platform X? After all, the C++
language was meant to be portable, so it would make sense to design a
portable C++ compiler. But how can that be done easily when the
compiler itself is written in C++?

As I noted earlier, this is a classic chicken-and-egg problem: you have
the source code to a C++ compiler written in C++ but no other C++
compiler available with which to compile it on platform X. But because
CFront relied on C++ to C translation, it turns out that a C++ compiler
was not strictly needed to solve the problem. The solution employed was
to use a C compiler to compile the translated CFront sources in order
to build a C++ compiler on platform X that could then compile itself.

Greg
 
B

Bo Persson

Greg said:
..

But you've completely forgotten about portability. Bootstrapping is
fine for a single platform but what happens when you want to bring
the
only C++ compiler in the world to platform X? After all, the C++
language was meant to be portable, so it would make sense to design
a
portable C++ compiler. But how can that be done easily when the
compiler itself is written in C++?

As I noted earlier, this is a classic chicken-and-egg problem: you
have
the source code to a C++ compiler written in C++ but no other C++
compiler available with which to compile it on platform X. But
because
CFront relied on C++ to C translation, it turns out that a C++
compiler
was not strictly needed to solve the problem. The solution employed
was
to use a C compiler to compile the translated CFront sources in
order
to build a C++ compiler on platform X that could then compile
itself.

And now I just wonder how the first C compiler arrived to platform X?


Bo Persson
 
V

Victor Bazarov

Greg said:
[...]
As I noted earlier, this is a classic chicken-and-egg problem: you
have the source code to a C++ compiler written in C++ but no other C++
compiler available with which to compile it on platform X. But because
CFront relied on C++ to C translation, it turns out that a C++
compiler was not strictly needed to solve the problem. The solution
employed was to use a C compiler to compile the translated CFront
sources [...]

Here we go again... "Translated CFront sources": translated *by what*?
And what do you call that thing?

V
 
N

Noah Roberts

Victor said:
Greg said:
[...]
As I noted earlier, this is a classic chicken-and-egg problem: you
have the source code to a C++ compiler written in C++ but no other C++
compiler available with which to compile it on platform X. But because
CFront relied on C++ to C translation, it turns out that a C++
compiler was not strictly needed to solve the problem. The solution
employed was to use a C compiler to compile the translated CFront
sources [...]

Here we go again... "Translated CFront sources": translated *by what*?
And what do you call that thing?

Well, except for the very first platform X you could just run CFront on
platform Y to get C sources and compile them on platform X. Don't know
if that is how they did it but you get the picture.

It's the first platform X that poses the problem.
 
V

Victor Bazarov

Noah said:
Victor said:
Greg said:
[...]
As I noted earlier, this is a classic chicken-and-egg problem: you
have the source code to a C++ compiler written in C++ but no other
C++ compiler available with which to compile it on platform X. But
because CFront relied on C++ to C translation, it turns out that a
C++ compiler was not strictly needed to solve the problem. The
solution employed was to use a C compiler to compile the translated
CFront sources [...]

Here we go again... "Translated CFront sources": translated *by
what*? And what do you call that thing?

Well, except for the very first platform X you could just run CFront
on platform Y to get C sources and compile them on platform X. Don't
know if that is how they did it but you get the picture.

It's the first platform X that poses the problem.

So, on the platform Y, CFront wasn't "written in C++", was it? It was
"written" then in C. And even if they were translated to C, they were
translated *somewhere*, right? So, there a C++ compiler/tranlsator had
to exist already. Now, how did that compiler/translator come into
existence? So, we simply need to eliminate multiple platforms from the
discussion. On the platform where CFront first appeared, what was it
written in? C with Classes (CwC), most likely. Now, the first CwC
compiler was written in C. First C compiler was written in B, and so on.
The first assembly language that translated symbolic instructions into
machine code was written probably in machine code directly...

V
 
R

raylopez99

The Colossus 'puter looks suspiciously like a Jacquard loom
(http://en.wikipedia.org/wiki/Jacquard_loom), which some historians of
technology consider the precursor to digital computers and may have
been the inspiration to Charles Babbage's Difference Engine!

Watch out: there's a bug in your Fruit of the Loom!

RL
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top