Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site watdaisy.UUCP Path: utzoo!watmath!watnot!watdaisy!mpdevine From: mpdevine@watdaisy.UUCP (Michel P. Devine) Newsgroups: net.lang Subject: Cute little liar program (for hackers only) Message-ID: <7772@watdaisy.UUCP> Date: Thu, 12-Jun-86 21:16:54 EDT Article-I.D.: watdaisy.7772 Posted: Thu Jun 12 21:16:54 1986 Date-Received: Sun, 15-Jun-86 03:49:59 EDT Distribution: net Organization: U of Waterloo, Ontario Lines: 84 During my CS340 lectures (a second course in Data Structures), I alluded to some of the unusual features of some programming languages, in particular Pascal and Fortran. On the spur of the moment (as an (spectacularly unsuccessful) attempt at stimulating class participation), I suggested an exercise: write a program which prints the identity of the compiler used to compile it. In the following script session, both liar.f and liar.p are links to the data file liar (which is the program in question). Script started on Mon Jun 9 11:52:23 1986 % % ls -l liar* -rw-r--r-- 1 mpdevine 440 Jun 9 11:50 liar lrwxr-xr-x 1 mpdevine 4 Jun 9 11:45 liar.f@ -> liar lrwxr-xr-x 1 mpdevine 4 Jun 9 11:45 liar.p@ -> liar % % pc liar.p % a.out Compiled by a Pascal compiler % % f77 liar.f liar.f: MAIN liar: % a.out Compiled by a FORTRAN compiler % If you like challenges, you can mark this article for later reference, otherwise skip quickly to the supplied solution (hold on to your hat!). % cat liar program liar (* c *) (output); (* c *) begin writeln (* 1 ( *,* ) 'Compiled by a FORTRAN compiler' c *) (' Compiled by a Pascal compiler'); end . (* c column 72 ^ *) % script done on Mon Jun 9 11:53:42 1986 Nice, no? The main tricks to notice are: a) FORTRAN ignores all characters beyond the 72nd column (these were originally used for sequence numbers). b) '(*' and '*)' are Pascal's comment delimiters. In FORTRAN, the character 'c' in the first column comments out the whole line, while a '1' in column 6 indicates a continuation of the previous statement. c) 'write' is a valid FORTRAN statement, while 'writeln' is correct Pascal. d) Pascal requires a period after the 'end' keyword (which is found at column 73!!!) Also, notice the blanks around the logical unit and format specification: there are necessary to avoid nested comments (for Pascal, of course). For some FORTRAN compilers, *any* character in column 1 as the effect of commenting out the line and therefore a simpler (ah!) solution would be: program liar (output); begin writeln (* 1 ( *,* ) 'Compiled by a FORTRAN compiler' *) (' Compiled by a Pascal compiler'); end . A further challenge: write a similar program for C and Pascal. ps. Before any comments are made about the good government money I wasted doing this silliness, I should point out that only 10 minutes were required to write and debug the program. -- -------------------------------------------------------------------------------- mpdevine%watdaisy@waterloo.csnet (519) 884-7123 Michel P. Devine mpdevine%watdaisy%waterloo.csnet@csnet-relay.arpa CS Dept., U. Waterloo {allegra,decvax,inhp4,utzoo}!watmath!watdaisy!mpdevine Waterloo, Ont. N2L3G1