A program which would print out it's own source code - possible ornot?

P

Pascal Bourguignon

Alf P. Steinbach said:
* Pascal J. Bourguignon:

quine.

I know, but the OP still doesn't know. Let's try to make him try to
write it!

--
__Pascal Bourguignon__ http://www.informatimago.com/

HEALTH WARNING: Care should be taken when lifting this product,
since its mass, and thus its weight, is dependent on its velocity
relative to the user.
 
P

Pascal Bourguignon

Puppet_Sock said:
That's old. The trick is to get the compiler
to spit out the entire source code from
messages produced during compilation.

Then it wouldn't be the program that would print out its own source
code, it would be the compiler.

--
__Pascal Bourguignon__ http://www.informatimago.com/

HEALTH WARNING: Care should be taken when lifting this product,
since its mass, and thus its weight, is dependent on its velocity
relative to the user.
 
I

Ioannis Vranos

red said:
Allow me to correct Pascal's code:

int main()
{
// add something here to print its own source.
// this is left as an exercise to the reader
return 0;
}

Non-portably and as a first thought I can think of this for my system:


#include <cstdlib>

int main()
{
std::system("cat main.cc");
}


It outputs:

[john@localhost src]$ ./foobar-cpp
#include <cstdlib>

int main()
{
std::system("cat main.cc");
}

[john@localhost src]$
 
I

Ioannis Vranos

Ioannis said:
Non-portably and as a first thought I can think of this for my system:


#include <cstdlib>

int main()
{
std::system("cat main.cc");
}


It outputs:

[john@localhost src]$ ./foobar-cpp
#include <cstdlib>

int main()
{
std::system("cat main.cc");
}

[john@localhost src]$


This can be improved my specifying int main(int argc, char **argv),
taking as an argument the source file. The program requirements doesn't
specify that arguments are prohibited.

But regarding portability, that isn't portable too.
 
L

Laurent D.A.M. MENTEN

(e-mail address removed) a écrit :
Is it possible to write a program which would print out it's own
source code, using C++?

I am not very confident with this but I think there are debugging
options or formats that embedded the source code in the executable... If
a programm is able to display these informations it surely can be
executed againsts itself.
 
P

Puppet_Sock

Then it wouldn't be the program that would print out its own source
code, it would be the compiler.

You know, I never get the memos. When did the memo
go around that we were all supposed to be idiots?

Me: The trick is to get the compiler to do it.
You: Then it would be the compiler doing it.
Me: (Does my Benny Hill imitation, slapping you
repeatedly on the back of the head like
B.H. did that little bald guy.)
Socks
 
M

Matt

I cannot see why it would be impossible to write a decompiler that was
carefully coded to decompile itself to its own source.
Is there some computation theory that I am missing?
 
M

Mark Holland

I cannot see why it would be impossible to write a decompiler that
was carefully coded to decompile itself to its own source.
Is there some computation theory that I am missing?

Appologies if this has already been answered... yes it is possible,
and such a program is called a quine.
http://en.wikipedia.org/wiki/Quine_(computing)

One of the examples from that page is a C program, which should also
be a valid C++ program.

As to the question about the decompiler... I don't see why it would be
impossible in theory. Of course, the higher-level the decompiled code
you want (e.g. functions, classes etc..) the more complicated your
compiler would be. And so you'd be more likely to write code using
more classes and language features which would make your job harder.

Mark
 
K

kwikius

But it's not what's specified.

From the O.P....

"Is it possible to write a program which would print out it's own
source code, using C++? "


As explained above I've even thrown in some very useful extra
functionality!! :)

regards
Andy Little
 
L

Lionel B

Then it wouldn't be the program that would print out its own source
code, it would be the compiler.

Right. So if you ran a compiled executable of a program that "prints out
its own source code" then I guess it wouldn't be the program that prints
out its own source code, it would be the run-time system.
 
M

Matt

I looked at some of those examples of a quine the other day. It looked like
there was a constraint where you could not have any input files. So, a
decompliler that took itself as input would not be legal.
The other thing I noticed is they seem to cheat by using printf. Why is it
legal to use external sources in libraries and not also print the source for
those libraries?
 
I

Ioannis Vranos

Matt said:
>
I looked at some of those examples of a quine the other day. It looked like
there was a constraint where you could not have any input files. So, a
decompliler that took itself as input would not be legal.
The other thing I noticed is they seem to cheat by using printf. Why is it
legal to use external sources in libraries and not also print the source for
those libraries?


Interesting question. However any standard library headers #included, in
reality they can be like a switch, with no actual header file existing.
 
P

Pascal Bourguignon

Ioannis Vranos said:
Interesting question. However any standard library headers #included,
in reality they can be like a switch, with no actual header file
existing.

Normally, I don't consider using libraries such as stdio or iostream
as cheating, since they're part of programming in C or C++. But I
don't mind using C or C++ compilers for what they are, some passable
portable assemblers, and writting raw assembly code we don't need any
header, since we always have access to the instructions of the POSIX
virtual machine, namely write(2) which is all we need to "print" some
text.


Here is a simplistic solution. When you edit it, you replace the
contents of quine[] by the new sources, escaping " and \ and putting
each line in a string. Then you update the literal 19 correponding to
the number of the first line of the initializer of quine[].

----------------------------quine.c++----------------------------------
// No header, no library.
// But we'll use the OS to "print" our sources.
extern "C"{ int write(int fd,const char* buffer,int size); }
int len(const char* str){
int l=0; while((*str)!=0){++str;++l;}
return(l);}
void escape(char* dst,const char* src){
int i=0;
for(int j=0;src[j]!=0;++j){
switch(src[j]){
case '"': case '\\': dst[i++]='\\'; // fallthru
default: dst[i++]=src[j]; }}
dst=0;}
int main(void){
char left []=" \"";
char right []="\",*";
char newline[]="*";
char buffer[128];
char* quine []={
"// No header, no library.",
"// But we'll use the OS to \"print\" our sources.",
"extern \"C\"{ int write(int fd,const char* buffer,int size); }",
"int len(const char* str){",
" int l=0; while((*str)!=0){++str;++l;} ",
" return(l);}",
"void escape(char* dst,const char* src){",
" int i=0;",
" for(int j=0;src[j]!=0;++j){",
" switch(src[j]){",
" case '\"': case '\\\\': dst[i++]='\\\\'; // fallthru",
" default: dst[i++]=src[j]; }}",
" dst=0;}",
"int main(void){",
" char left []=\" \\\"\";",
" char right []=\"\\\",*\";",
" char newline[]=\"*\";",
" char buffer[128];",
" char* quine []={",
" };",
" right[2]=10;",
" newline[0]=10;",
" for(int i=0;i<sizeof(quine)/sizeof(quine[0]);++i){",
" if(i==19){",
" for(int j=0;j<sizeof(quine)/sizeof(quine[0]);++j){",
" write(1,&left[0],len(left));",
" escape(buffer,quine[j]);",
" write(1,buffer,len(buffer));",
" write(1,&right[0],len(right));",
" }",
" }",
" write(1,quine,len(quine));",
" write(1,&newline[0],len(newline));",
" }",
" return(0);",
"}",
};
right[2]=10;
newline[0]=10;
for(int i=0;i<sizeof(quine)/sizeof(quine[0]);++i){
if(i==19){
for(int j=0;j<sizeof(quine)/sizeof(quine[0]);++j){
write(1,&left[0],len(left));
escape(buffer,quine[j]);
write(1,buffer,len(buffer));
write(1,&right[0],len(right));
}
}
write(1,quine,len(quine));
write(1,&newline[0],len(newline));
}
return(0);
}
----------------------------quine.c++----------------------------------

g++ quine.c++ -o quine && ./quine > quine.out && diff quine.c++ quine.out


--
__Pascal Bourguignon__ http://www.informatimago.com/

"What is this talk of "release"? Klingons do not make software
"releases". Our software "escapes" leaving a bloody trail of
designers and quality assurance people in its wake."
 

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
474,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top