Need help regarding building a small C++ to C compiler.

S

Student

Hi All,
I have an assignment for my Programming language project to create a
compiler that takes a C++ file as input and translate it into the C
file. Here I have to take care of inheritance and operator overloading
and virtual functions. I mean it should not hamper the C++ meaning. In
frank and simple terms i need to implement a small C++ compiler in C++,
and i want the intermediate representation to be C. Please help me in
this.
~thanks and regards
 
T

TB

Student skrev:
Hi All,
I have an assignment for my Programming language project to create a
compiler that takes a C++ file as input and translate it into the C
file. Here I have to take care of inheritance and operator overloading
and virtual functions. I mean it should not hamper the C++ meaning. In
frank and simple terms i need to implement a small C++ compiler in C++,
and i want the intermediate representation to be C. Please help me in
this.
~thanks and regards

See Lex and Yacc/Bison (or Spirit) as a start.
 
N

Neil Cerutti

I have an assignment for my Programming language project to create a
compiler that takes a C++ file as input and translate it into the C
file. Here I have to take care of inheritance and operator
overloading and virtual functions. I mean it should not hamper the
C++ meaning. In frank and simple terms i need to implement a small
C++ compiler in C++, and i want the intermediate representation to
be C. Please help me in this.

Could you choose something any more challenging? ;-)

Stop by comp.compilers for help.
 
J

Jerry Coffin

@z34g2000cwc.googlegroups.com>,
(e-mail address removed) says...
Hi All,
I have an assignment for my Programming language project to create a
compiler that takes a C++ file as input and translate it into the C
file. Here I have to take care of inheritance and operator overloading
and virtual functions. I mean it should not hamper the C++ meaning. In
frank and simple terms i need to implement a small C++ compiler in C++,
and i want the intermediate representation to be C. Please help me in
this.

Whether this is at all feasible depends on what you mean
by "small C++ to C Compiler". If you mean a compiler for
a small subset of C++, then it might be feasible. If you
mean a small compiler for C++, you're dreaming -- a C++
compiler is a BIG project.

Making the compiler feasible will depend heavily upon
creating a subset of C++ that's reasonable to attempt.
C++ features vary from the truly trivial (e.g. // as a
comment delimiter) while others are exceptionally
difficult (e.g. supporting exported templates).
 
D

Dietmar Kuehl

TB said:
See Lex and Yacc/Bison (or Spirit) as a start.

Bad advice! C++ is not at all context free. The people behind gcc
battled bison for quite a while to try and parse C++. It never really
worked. I think, all contemporary and mostly conforming C++ compilers
use a recursive descent parser.
 
J

Jim Langston

Student said:
Hi All,
I have an assignment for my Programming language project to create a
compiler that takes a C++ file as input and translate it into the C
file. Here I have to take care of inheritance and operator overloading
and virtual functions. I mean it should not hamper the C++ meaning. In
frank and simple terms i need to implement a small C++ compiler in C++,
and i want the intermediate representation to be C. Please help me in
this.
~thanks and regards

See my answer posted in alt.comp.lang.learn.c-c++
 
J

Jeff Flinn

Dietmar said:
Bad advice! C++ is not at all context free. The people behind gcc
battled bison for quite a while to try and parse C++. It never really
worked. I think, all contemporary and mostly conforming C++ compilers
use a recursive descent parser.

IIUC, boost::spirit qualifies then. Also see boost::wave a fully implemented
preprocessor.

Jeff Flinn
 
D

Dietmar Kuehl

Jeff said:
IIUC, boost::spirit qualifies then. Also see boost::wave a fully
implemented preprocessor.

Nope. Boost::spirit uses an LL-parser. Even though an LL-parser might
use recursive decent, it still requires that the grammar is context
free which is not the case in C++. I'm actually not aware of any
parser generator which supports non-context-free grammars.
 
G

Greg Comeau

I have an assignment for my Programming language project to create a
compiler that takes a C++ file as input and translate it into the C
file. Here I have to take care of inheritance and operator overloading
and virtual functions. I mean it should not hamper the C++ meaning. In
frank and simple terms i need to implement a small C++ compiler in C++,
and i want the intermediate representation to be C. Please help me in
this.

I have now read a few dozen messages in what seems like a
dozen newgroups. Unfortunately, you have not IMO fully
told us all that you're seeking.

I am also surprised that nobody seems to have mentioned
Lippman's "Inside the C++ Object Model"
(see http://www.comeaucomputing.com/booklist for ISBN info)
which discusses some of the issues involved in such a
translation, and some actual model suggestions of what
can be sought and implemented.

I am further surprised at how easy and trivial some of
the posters are making the assignment. This project is not
just about macro substituting the keyword class and replacing
it with struct. And the overloading part is not just about
text substituting void foo() into void foo(void).

You mention "i need to implement a small C++ compiler in
C++" however, to "take care of inheritance and operator
overloading and virtual functions" is quite a tall order
by itself. So it's unclear where "small" comes in.
To do it, means have to have written something of the scale
of a C compiler already (this does not mean you need to start
with a C compiler but that something will need to exist
underlying that accepts basic types, basic etc), with all
it's anomalies, subtleties, semantics, etc. And then
add on tons of stuff.

Implementing virtual functions implies a whole slew of
issues be established first. Function overloading is
hardly understandable no less implementable correctly,
and also implies a whole different slew of issues be
understood and implemented first.

Trying to do what it sounds like you're requesting before
end of June is not practical, unless you really are limiting
the features, like no templates, no exceptions, no namespaces,
no preprocessor (which implies no includes, etc), no comments,
no multiple inheritance, no RTTI, and no doubt hundreds of
other 1/2 or non-implementations of other features,
no bool/wchar_t/etc, no conditional declarations, most things
for classes missing (no initializers, member lists, mutable,
explicit, covariant return types, accessibility, class new/delete,
etc.), no typesafe linkage, no new style casts, no nested classes,
no friends, completely different lookup rules (which competes
with getting overloading working), and the list goes on and on.

Contrary to some posts in the numerous NGs you posted your
message in, a full blown compiler is a multi-man year effort.
Even w/o all the above exclusions, it's still a tall order to
start from scratch and still get simple inheritance and simple
operator overloading (sic) working by June.

So, what is the subset you're seeking? Did the teacher give
out an exact list of the subset? Or is this for say a Phd
where you get to define what the project is about?

Lastly, but not least, doing a project of this nature is not
something you just sit down and do, and is not something that
you just sit down and start anywhere. It has its own set of
implications as well: organization, grammars, design,
understanding the subset, etc etc etc.
 
W

Walter Bright

Greg Comeau said:
Contrary to some posts in the numerous NGs you posted your
message in, a full blown compiler is a multi-man year effort.
Even w/o all the above exclusions, it's still a tall order to
start from scratch and still get simple inheritance and simple
operator overloading (sic) working by June.

Back when I decided to write a C compiler, a mentor of mine took me to see
the local C guru for advice. He looked at me like I was a bug squashed on
his shoe, and asked me "who the **** do you think you are, thinking you can
write a C compiler?"

I'm reminded of Gimli's statement: "Certainty of death, small chance of
success, what are we waiting for?"

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
 
M

Mark Randall

Walter Bright said:
Back when I decided to write a C compiler, a mentor of mine took me to see
the local C guru for advice. He looked at me like I was a bug squashed on
his shoe, and asked me "who the **** do you think you are, thinking you
can write a C compiler?"

Funny thing is... not that I have wrote a 'C' compiler, im working on a semi
compilable scripting language... but C++/OOP makes writing it significantly
easier IMO.
 
G

Gerry Quinn

I am further surprised at how easy and trivial some of
the posters are making the assignment. This project is not
just about macro substituting the keyword class and replacing
it with struct. And the overloading part is not just about
text substituting void foo() into void foo(void).

Nobody claimed it was *just* that.

What I said was that getting the basics up and running should not be
very difficult, and I showed how one might start going about it.
Trying to do what it sounds like you're requesting before
end of June is not practical, unless you really are limiting
the features, like no templates, no exceptions, no namespaces,
no preprocessor (which implies no includes, etc), no comments,
no multiple inheritance, no RTTI, and no doubt hundreds of
other 1/2 or non-implementations of other features,

But he has already implicitly stated that he is NOT interested in
implementing all those features.

What he wants, as far as I can see, is a primitive version of
Stroustrup's original "C with Classes", that will translate a limited
subset of C++ programs into C.

- Gerry Quinn
 
G

Greg Comeau

Back when I decided to write a C compiler, a mentor of mine took me to see
the local C guru for advice. He looked at me like I was a bug squashed on
his shoe, and asked me "who the **** do you think you are, thinking you can
write a C compiler?"

I'm reminded of Gimli's statement: "Certainty of death, small chance of
success, what are we waiting for?"

Yes, the challenge should not always be the deterent, as impossible
as something often sounds. Often that's a reason to do it! :)
I still hope the guy is looking at a good subset though :)
 
G

Greg Comeau

Nobody claimed it was *just* that.

What I said was that getting the basics up and running should not be
very difficult, and I showed how one might start going about it.

Well, I for one have no problem saying it is difficult,
even if the guy is working with some sort of reasonable subset.
But he has already implicitly stated that he is NOT interested in
implementing all those features.

It's unclear, because soomething like virtual has a cascade of
implicit statements behind it too, and so on.
What he wants, as far as I can see, is a primitive version of
Stroustrup's original "C with Classes", that will translate a limited
subset of C++ programs into C.

Which could still makes it a complicated and non-trivial assignment.
So it needs to be even lots more primitive than that. Hence my
request to him to define what the parameters of the assignment are.
 
W

Walter Bright

Mark Randall said:
Funny thing is... not that I have wrote a 'C' compiler, im working on a
semi compilable scripting language... but C++/OOP makes writing it
significantly easier IMO.

I agree that OOP is a natural fit for implementing compilers.

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
 
W

Walter Bright

Greg Comeau said:
Yes, the challenge should not always be the deterent, as impossible
as something often sounds. Often that's a reason to do it! :)
I still hope the guy is looking at a good subset though :)

When I was 7 years old, I decided to build a real, full size, working
airplane in the garage out of scrap wood. I still recall the look of
amusement on my father's face, but he never tried to discourage me. I had a
lot of happy hours in the garage attempting to do it <g>.

I think a lot of things are accomplished by young people simply because it
never occurs to them that it is impossible.
 
B

BobR

Walter Bright wrote in message ...
When I was 7 years old, I decided to build a real, full size, working
airplane in the garage out of scrap wood.

Radial, jet or lawnmower engined? <G>

I was a jet-mech, but, I love the sound and smell of a radial recip when it
starts up (ether).
Jet? Just smells like kerosene and blows you 100 yards down the taxiway!.
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top