Self-Printing Programs

Similar documents
Procedural Programming

8. Characters, Strings and Files

Intermediate Programming, Spring 2017*

C mini reference. 5 Binary numbers 12

C programming basics T3-1 -

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

today cs3157-fall2002-sklar-lect05 1

Contents. A Review of C language. Visual C Visual C++ 6.0

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

ECE551 Midterm Version 1

ECE551 Midterm Version 2

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

ESC101N: Fundamentals of Computing End-sem st semester

File I/O. Arash Rafiey. November 7, 2017

Fundamentals of Programming & Procedural Programming

DSV Library for Lisp

1 The CTIE processor INTRODUCTION 1

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

Standard C Library Functions

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

PRINCIPLES OF OPERATING SYSTEMS

Chapter 8 Character Arrays and Strings

Parsing INI Files in Lisp

CSCI 171 Chapter Outlines

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

20 Dynamic allocation of memory: malloc and calloc

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Signature: ECE 551 Midterm Exam

Computer Programming Unit v

fopen() fclose() fgetc() fputc() fread() fwrite()

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

ONE DIMENSIONAL ARRAYS

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

File Handling. 21 July 2009 Programming and Data Structure 1

C Programming. Unit 9. Manipulating Strings File Processing.

File IO and command line input CSE 2451

mith College Computer Science CSC231 C Tutorials Fall 2017 Introduction to C Dominique Thiébaut

A Crash Course in C. Steven Reeves

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

Chapter 8 C Characters and Strings

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lecture 8: Structs & File I/O


File Access. FILE * fopen(const char *name, const char * mode);

LAB7 : Characters and Strings

Chapter 14 - Advanced C Topics

Final CSE 131B Spring 2005

ECE551 Midterm Version 1

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Preview from Notesale.co.uk Page 2 of 79

Introduction to file management

ECE264 Spring 2014 Exam 2, March 11, 2014

C Programming Language

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Detecting Errors in the Dual Port TPU RAM (DPTRAM) Module

CSE 303 Midterm Exam

File (1A) Young Won Lim 11/25/16

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

C: How to Program. Week /May/28

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

ENG120. Misc. Topics

mywbut.com 12 File Input/Output

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Engineering program development 7. Edited by Péter Vass

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

ECE264 Spring 2013 Exam 1, February 14, 2013

Lecture 8: Readin, Writin & Sortin. Wednesday, 04 March 2009

C Basics And Concepts Input And Output

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

Solutions to Assessment

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Chapter 8: Character & String. In this chapter, you ll learn about;

CSC209H1S Day Midterm Solutions Winter 2010

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

What is recursion. WAP to find sum of n natural numbers using recursion (5)

High Performance Programming Programming in C part 1

Characters and Strings

Final Intro to C Review

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

What we have learned so far

C Characters and Strings

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Appendix A Developing a C Program on the UNIX system

COP 3223 Final Review

Procedural Programming & Fundamentals of Programming

Computers Programming Course 10. Iulian Năstac

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$13$

6.S096 Lecture 2 Subtleties of C

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Arrays and Pointers in C. Alan L. Cox

Administrative Stuff. More on Strings, File I/O. Last Time. Java API

More on Strings, File I/O. September 8, 2016

COP 3223 Final Review

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

Transcription:

Self-Printing Programs Gene Michael Stover created 2006 March 1 updated Friday, 2006 May 26 Copyright copyright 2006 Gene Michael Stover. All rights reserved. Permission to copy, store, & view this document unmodified & in its entirety is granted. 1 What is this? Someone sent an e-mail to a coworker, saying that a really difficult, almost impossible programming task was to write a program which would print its own source code. Yes, yes yes, I recognize that all of this has been done before. But it hasn t been done before by me. 2 First Solution I thought about it for a minute, then wrote a first C version 1 & a first ms-dog.bat version 2 Those programs both use the same trick to satisfy the problem. On the one hand, I feel that these two programs satisfy the programming task, but I also admit that they use a dirty trick to do it because they require a data file at run-time. I also wrote a first Lisp version 3 It isn t portable; it requires clisp to run. I also could have use the dirty trick which I used in the C version. 3 Second Implementation Days later, I realized that you could do it if the program has an incomplete string representation of its own source code, prints that string & also inserts that string into itself in a key location. That gives you source code which compiles to a program which again prints the incomplete string & substitutes the string into itself, giving you the souce code for that program. It was totally straight-forward in Lisp. (See Appendix D.) 1 For the first C version, see Appendix A. 2 For the first ms-dog version, see Appendix B. 3 For the first Lisp version, see Appendix C. 1

It was more difficult in C because C s printf does not contain a verbatim substituion field like Lisp s format s ~S. So I wrote a C program which produces a program which prints its own source code. In other words, the C program, the first time it s run, reads its source code from a file. It produces the source code for a program which prints its own source code by using the substitute the incomplete string into itself technique. The C program I wrote is in Appendix E. The C program it produces is in Appendix F. 4 What is a self-printing program The programming problem was presented to me as Can you write a program which prints its exact source code? That got the point across, but I suspect a more precise definition of a selfprinting program is... 1. source code S which compiles to an executable program P, 2. when executed, P produces output T, & 3. T is equivalent to S. (If T & S are byte-for-byte identical, then it s a no-brainer that they are equivalent.) A First C Implementation This is the first C implementation. It uses a dirty trick in that it requires the presence of the source code file at run-time. This source code is also at http://cybertiggyr.com/gene/aaa/showself.c. /* * $Header: /home/gene/library/website/docsrc/aaa/rcs/showself.c,v 395.1 2008/04/20 17:25:45 gene E */ #include <stdio.h> int main () FILE *fp; int c; fp = fopen ("showself.c", "r"); while ((c = fgetc (fp))!= EOF) putchar (c); return 0; 2

B First MS-DOG.BAT Implementation This is the first ms-dog.bat implementation. It uses a dirty trick in that it requires the presence of the source code file at run-time. This source code is also at http://cybertiggyr.com/gene/aaa/showself0.bat. @echo off REM This is SHOWSELF0.BAT type showself0.bat REM --- end of file --- C First Lisp Implementation This is the first Lisp implementation. It requires a non-portable feature of clisp. Other Lisps surely have a similar feature in their own non-portable ways, so you could use this same trick for them. This source code is also at http://cybertiggyr.com/gene/aaa/showself.lisp. (defun showself () (first (second (SYMBOL-PLIST showself)))) D Second Lisp Implementation Here is a Lisp program which, in my opinion, satisfies the programming task without using any dirty tricks. It is a self-contained program which prints its own source code. This source code is also at http://cybertiggyr.com/gene/aaa/showself1.lisp. ;;; File: showself1.lisp (defvar *src* ";;; File: showself1.lisp (defvar *src* ~S) (defun showself1 () (format t *src* *src*)) ") (defun showself1 () (format t *src* *src*)) E Second C Implementation, part A Here is a C program which reads its source code from a file & prints a program which prints its own source code even when the file is not present. This program s output, which is the program in in Appendix F, is a program which prints its own source code. 3

This source code is also at http://cybertiggyr.com/gene/aaa/showself1.c. /* File: showself1.c */ #include <stdlib.h> #include <stdio.h> #include <string.h> static char S_src[] = "%ooga%"; S_PrintSub (char *from, char *to) char *p; for (p = from; p < to; ++p) printf ("%c", *p); S_PrintData (char str[]) int i = 0; while (str[i]!= \0 ) switch (str[i]) case " : printf ("\\\""); break; case \\ : printf ("\\\\"); break; case \n : printf ("\\n\"\n \""); break; default: putchar (str[i]); ++i; S_PrintRest (char str[]) char *p; for (p = str; *p!= \0 ; ++p) putchar (*p); int main () char *src, *p; FILE *fp; int i; 4

if (strcmp (S_src, "%ooga%") == 0) /* Must load the source code from the file. */ fp = fopen ("showself1.c", "r"); src = (char *) malloc (10 * 1024); fread (src, 1, 10 * 1024, fp); fclose (fp); else src = S_src; p = strstr (src, "%ooga%"); i = strlen ("%ooga%"); S_PrintSub (src, p); S_PrintData (src); S_PrintRest (p + i); return 0; F Second C Implementation, part B This program was produced by the program in Appendix E. This program is a program which prints its own source code. Unlike my first solutions to the programming task, this one works even if the source code file is not available at run-time. This source code is also at http://cybertiggyr.com/gene/aaa/showself2.c. /* File: showself1.c */ #include <stdlib.h> #include <stdio.h> #include <string.h> static char S_src[] = "/* File: showself1.c */\n" "#include <stdlib.h>\n" "#include <stdio.h>\n" "#include <string.h>\n" "static char S_src[] = \"%ooga%\";\n" "\n" "S_PrintSub (char *from, char *to)\n" "\n" " char *p;\n" " for (p = from; p < to; ++p) printf (\"%c\", *p);\n" "\n" 5

"S_PrintData (char str[])\n" "\n" " int i = 0;\n" " while (str[i]!= \\0 ) \n" " switch (str[i]) \n" " case \" : printf (\"\\\\\\\"\"); break;\n" " case \\\\ : printf (\"\\\\\\\\\"); break;\n" " case \\n : printf (\"\\\\n\\\"\\n \\\"\"); break;\n" " default: putchar (str[i]);\n" " \n" " ++i;\n" " \n" "\n" "S_PrintRest (char str[])\n" "\n" " char *p;\n" " for (p = str; *p!= \\0 ; ++p) \n" " putchar (*p);\n" " \n" "int\n" "main ()\n" "\n" " char *src, *p;\n" " FILE *fp;\n" " int i;\n" " if (strcmp (S_src, \"%ooga%\") == 0) \n" " /* Must load the source code from the file. */\n" " fp = fopen (\"showself1.c\", \"r\");\n" " src = (char *) malloc (10 * 1024);\n" " fread (src, 1, 10 * 1024, fp);\n" " fclose (fp);\n" " else \n" " src = S_src;\n" " \n" " p = strstr (src, \"%ooga%\");\n" " i = strlen (\"%ooga%\");\n" " S_PrintSub (src, p);\n" " S_PrintData (src);\n" " S_PrintRest (p + i);\n" " return 0;\n" ""; 6

S_PrintSub (char *from, char *to) char *p; for (p = from; p < to; ++p) printf ("%c", *p); S_PrintData (char str[]) int i = 0; while (str[i]!= \0 ) switch (str[i]) case " : printf ("\\\""); break; case \\ : printf ("\\\\"); break; case \n : printf ("\\n\"\n \""); break; default: putchar (str[i]); ++i; S_PrintRest (char str[]) char *p; for (p = str; *p!= \0 ; ++p) putchar (*p); int main () char *src, *p; FILE *fp; int i; if (strcmp (S_src, "%ooga%") == 0) /* Must load the source code from the file. */ fp = fopen ("showself1.c", "r"); src = (char *) malloc (10 * 1024); fread (src, 1, 10 * 1024, fp); fclose (fp); else src = S_src; 7

p = strstr (src, "%ooga%"); i = strlen ("%ooga%"); S_PrintSub (src, p); S_PrintData (src); S_PrintRest (p + i); return 0; References 8