{****************************************************************************** STRIP PROGRAM COMMENTS Execute the program: strcom infile outfile Strips all the comments of the input Pascal program. Each comment is replaced with a single space, and any eolns occuring in a comment will also be removed. The replacement with a space is required because comments are valid separators, so removing the comment entirely could create errors. Ignores all characters in strings. Does not understand a '\'' construct. ******************************************************************************} program strcom(infile, outfile); var infile, outfile: text; c: char; incomment: boolean; instring: boolean; begin reset(infile); rewrite(outfile); incomment := false; instring := false; while not eof(infile) do begin while not eoln(infile) do begin read(infile, c); if incomment then begin { in commented code } { check for comment terminator } if c = '}' then incomment := false else if (c = '*') and (infile^ = ')') then begin get(infile); incomment := false end end else begin { not in comment } if instring then begin { in quote } write(outfile, c); { output quoted character } if c = '''' then instring := false { exit quote mode } end else begin { straight program text } { activate quoting mode } if c = '''' then begin write(outfile, c); { output quoted character } instring := true end else begin { not quote } if c = '{' then begin write(outfile, ' '); { replace with single space } incomment := true; end else if (c = '(') and (infile^ = '*') then begin get(infile); write(outfile, ' '); { replace with single space } incomment := true end else write(outfile, c) { just output normally } end end end end; readln(infile); if not incomment then writeln(outfile); instring := false { reset any unterminated strings } end end.