program nocom(input, output); (* nocom: remove comments from a program Dr. Thomas D. Schneider National Cancer Institute Laboratory of Experimental and Computational Biology Frederick, Maryland 21702-1201 toms@ncifcrf.gov permanent email: toms@alum.mit.edu http://www.lecb.ncifcrf.gov/~toms/ National Cancer Institute Laboratory of Experimental and Computational Biology *) const (* begin module version *) version = 1.10; (* of nocom.p 2000 Dec 4 2000 Dec 4, 1.06: upgrade to mention stdin 2000 Nov 16, 1.06: handle {} 1996 Jul 27, 1.05: previous version origin 1990 May 14 from decom 1988 Dec 14 *) (* end module version *) (* begin module describe.nocom *) (* name nocom: remove comments from a program synopsis nocom(input: stdin; output: out) files input: a Pascal or Delila program with comments. output: the same program with the contents of the comments removed description This program removes comments from a Delila or Pascal source code so that one can, for example, compare two outputs of dbinst. It can also be used to remove comments to allow one to count the number of Delila 'get' statements. The comments are left as 'nibbins' that have no contents. This allows one to see exactly where the comments were. One can remove these using Unix sed: set asterisk = '*' nocom < commented.p |\ sed -e 's/{}//g' | \ sed -e "s/(\$asterisk\$asterisk)//g" | \ cat > commentless.p Note: Generally it is a bad idea to permanently remove comments from a program! see also {Description of what stdin means: } shell.p {Programs mentioned in the description above: } dbinst.p delila.p {Other comment manipulation programs: } decom.p codecomments.p author Thomas Dana Schneider bugs technical notes Some programs have comment starts inside quotes. nocom is now smart enough to avoid changing these. *) (* end module describe.nocom *) var c: char; (* the current character just read *) inquote: boolean; (* true if within a quote *) incomment1: boolean; (* true if within a parenthesis comment *) incomment2: boolean; (* true if within a brackets comment *) p: char; (* the character previous to c *) begin incomment1 := false; incomment2 := false; inquote := false; c := ' '; while not eof(input) do begin while not eoln(input) do begin p := c; read(input,c); if not (incomment1 or incomment2) then write(output,c); if c = '''' then inquote := true else inquote := false; if not inquote then begin if not incomment2 then begin if (p = '(') and (c = '*') then incomment1 := true; if (p = '*') and (c = ')') and incomment1 then begin incomment1 := false; write(output,'*)'); (* close of the comment *) end; end; if not incomment1 then begin if (c = '{') then incomment2 := true; if (c = '}') and incomment2 then begin incomment2 := false; write(output,'}'); (* close of the comment *) end; end end; end; readln(input); if not (incomment1 or incomment2) then writeln(output); end; end.