in Compiler ~ read.

Quine

Intro to the problem of Quine

According to the definition of Quine, it is a piece of is a non-empty computer program which takes no input and produces a copy of its own source code as its only output ("Wikipedia"). Another prospective to understand this problem is that if we view the compiler as a function that transform a piece of code into binary result on the computer, then a Quine is just the fix point of such function. The reason I noticed this problem is when I was reading the paper of RoTT, and it seems to me that the core logic of such magic is derived from the idea of Quine. Here are the steps I've taken to solve this idea.

first attempt

main(){  
    printf("main(){\n\tprintf(\"\");\n\}");
}

First I thought it's just an issue of print. However, by the time I was trying to do the printf, I don't know what to put in the parameter of printf. It seems to me that it becomes an infinite recursion because I'm always trying to print out the string inside a string which is itself.

second attempt

char *s="hi I'm %s"  
main(){  
    printf(s,s);
}

The second thing I tried is to try to use the %s in a string to print itself. The result turn out to be "hi I'm hi I'm %s". So the result is just replace the %s in the first s with the second s.

third attempt

char *s="char *s=\"%s\";\nmain()\{printf(s,s);\}";  
main(){printf(s,s);}  

Here I'm just try to replace the hi in my previous attempt to something meaningful, aka, a block of my original code. This is already a Quine right ow. However, I'm doing a bit trick here in that I'm carefully avoid using indentations, because otherwise, the first line of string declaration will contain no indents but the function body should have indent. It can be done by adding extra format placeholder to get the actual code style we wanted. But this is not my intension in the sense that I'm just trying to inject a piece of logic into the compiler without caring about the style.

comments powered by Disqus