By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Mar 7 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
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.

--
TB @ SWEDEN
Mar 7 '06 #2

P: n/a
On 2006-03-07, Student <On*********@mail2student.com> wrote:
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.

--
Neil Cerutti
Scouts are saving aluminum cans, bottles, and other items to be
recycled. Proceeds will be used to cripple children. --Church
Bulletin Blooper
Mar 7 '06 #3

P: n/a
In article <1141747437.056257.215800
@z34g2000cwc.googlegroups.com>,
On*********@mail2student.com 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).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 7 '06 #4

P: n/a
TB wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 7 '06 #5

P: n/a
"Student" <On*********@mail2student.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
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++
Mar 8 '06 #6

P: n/a
Dietmar Kuehl wrote:
TB wrote:
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.


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

Jeff Flinn
Mar 8 '06 #7

P: n/a
Jeff Flinn wrote:
Dietmar Kuehl wrote:
TB wrote:
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.


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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 8 '06 #8

P: n/a
In article <11**********************@z34g2000cwc.googlegroups .com>,
Student <On*********@mail2student.com> wrote:
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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Mar 11 '06 #9

P: n/a

"Greg Comeau" <co****@panix.com> wrote in message
news:du*********@panix2.panix.com...
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
Mar 12 '06 #10

P: n/a
>Walter Bright" <wa****@nospamm-digitalmars.com> wrote in message
news:Qf******************************@comcast.com ...
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.

--
- Mark Randall

"We're Systems and Networks"
"It's our job to know"
Mar 12 '06 #11

P: n/a
In article <du*********@panix2.panix.com>, co****@panix.com says...
In article <11**********************@z34g2000cwc.googlegroups .com>,
Student <On*********@mail2student.com> wrote:
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 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

Mar 12 '06 #12

P: n/a
In article <Qf******************************@comcast.com>,
Walter Bright <wa****@nospamm-digitalmars.com> wrote:

"Greg Comeau" <co****@panix.com> wrote in message
news:du*********@panix2.panix.com...
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?"


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 :)
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Mar 12 '06 #13

P: n/a
In article <MP************************@news1.eircom.net>,
Gerry Quinn <ge****@DELETETHISindigo.ie> wrote:
In article <du*********@panix2.panix.com>, co****@panix.com says...
In article <11**********************@z34g2000cwc.googlegroups .com>,
Student <On*********@mail2student.com> wrote:
>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 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.


Well, I for one have no problem saying it is difficult,
even if the guy is working with some sort of reasonable subset.
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.


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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Mar 12 '06 #14

P: n/a

"Mark Randall" <ma****@gmail.com> wrote in message
news:uG**************@TK2MSFTNGP14.phx.gbl...
Walter Bright" <wa****@nospamm-digitalmars.com> wrote in message
news:Qf******************************@comcast.com ...
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.


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

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
Mar 12 '06 #15

P: n/a

"Greg Comeau" <co****@panix.com> wrote in message
news:dv**********@panix2.panix.com...
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.
Mar 12 '06 #16

P: n/a

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

--
Bob R
POVrookie
Mar 14 '06 #17

P: n/a
The following articles describe a mapping from C++ to C. They should
help in your compiler project.

http://www.eventhelix.com/RealtimeMa...erformance.htm

http://www.eventhelix.com/RealtimeMa...rformance2.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Interaction Modeling
Tool

Mar 14 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.