Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a self reproducing code (prints the source on exec)?

I have seen a lot of C/C++ based solutions to this problem where we have to write a program that upon execution prints its own source.

some solutions --

http://www.cprogramming.com/challenges/solutions/self_print.html

Quine Page solution in many languages

There are many more solutions on the net, each different from the other. I wonder how do we approach to such a problem, what goes inside the mind of the one who solves it. Lend me some insights into this problem... While solutions in interpreted languages like perl, php, ruby, etc might be easy... i would like to know how does one go about designing it in compiled languages...

like image 623
Abhishek Mishra Avatar asked Sep 19 '08 21:09

Abhishek Mishra


People also ask

How do you write quine?

A common trick is to jump start the quine by writing a program to read a textfile and output an array of numbers. Then you modify it to use a static array, and run the first program against the new (static array) program, producing an array of number that represents the program.

What creates a copy of itself and execute it?

A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".

Is it possible to write C program which it print self?

Given the task is to print the written C program itself. We have to write a C program which will print itself. So, we can use file system in C to print the contents of the file of which we are writing the code, like we are writing the code in “code 1.


2 Answers

Aside from cheating¹ there is no difference between compiled and interpreted languages.

The generic approach to quines is quite easy. First, whatever the program looks like, at some point it has to print something:

 print ... 

However, what should it print? Itself. So it needs to print the "print" command:

 print "print ..." 

What should it print next? Well, in the mean time the program grew, so it needs to print the string starting with "print", too:

 print "print \"print ...\"" 

Now the program grew again, so there's again more to print:

 print "print \"print \\\"...\\\"\"" 

And so on. With every added code there's more code to print. This approach is getting nowhere, but it reveals an interesting pattern: The string "print \"" is repeated over and over again. It would be nice to put the repeating part into a variable:

 a = "print \"" print a 

However, the program just changed, so we need to adjust a:

 a = "a = ...\nprint a" print a 

When we now try to fill in the "...", we run into the same problems as before. Ultimately, we want to write something like this:

 a = "a = " + (quoted contents of a) + "\nprint a" print a 

But that is not possible, because even if we had such a function quoted() for quoting, there's still the problem that we define a in terms of itself:

 a = "a = " + quoted(a) + "\nprint a" print a 

So the only thing we can do is putting a place holder into a:

 a = "a = @\nprint a" print a 

And that's the whole trick! Anything else is now clear. Simply replace the place holder with the quoted contents of a:

 a = "a = @\nprint a" print a.replace("@", quoted(a)) 

Since we have changed the code, we need to adjust the string:

 a = "a = @\nprint a.replace(\"@\", quoted(a))" print a.replace("@", quoted(a)) 

And that's it! All quines in all languages work that way (except the cheating ones).

Well, you should ensure that you replace only the first occurence of the place holder. And if you use a second place holder, you can avoid needing to quote the string.

But those are minor issues and easy to solve. If fact, the realization of quoted() and replace() are the only details in which the various quines really differ.


¹ by making the program read its source file

like image 117
vog Avatar answered Sep 23 '22 06:09

vog


There are a couple of different strategies to writing quines. The obvious one is to just write code that opens the code and prints it out. But the more interesting ones involve language features that allow for self-embedding, like the %s-style printf feature in many languages. You have to figure out how to embed something so that it ends up resolving to the request to be embedded. I suspect, like palindromes, a lot of trial and error is involved.

like image 21
aaronsw Avatar answered Sep 25 '22 06:09

aaronsw