1984 ACM East Central Regional Programming Contest

Problem #2: Constant Comments

Source file: prob2.pas
Input file: prob2.in
Output file: prob2.out

One way to indicate comments in a Pascal program is by using the matched pair of symbols (* and *). There can be no characters (including blanks, line feeds, and carriage returns) between each asterisk and its parenthesis. Any character enclosed within these comment delimiters is part of the comment. Of course, we cannot enclose the character pair *) within a comment since this symbol marks the end of a comment. Pascal allows other comment delimiters, but we will not use them in this problem.

Pascal programmers can place comments practically anywhere within a program. A comment can be on a line by itself it can share a line with other comments and parts of statements, or it can spread across several lines.

Pascal also allows strings. Strings are any characters enclosed within single quote marks. For instance, 'This is a Pascal string'. Strings can occur in symbolic constant definitions, assignment statements, as operands in relational expressions, and as actual arguements in procedure and function calls. Most frequently, they occur in calls to write or writeln procedures when we wish to output the phrase enclosed in the single quotes. If we want to express the single quote character within a string we use two consecutive single quotes. For example, the contraction for do not is expressed at the string 'don''t'. The double quote character has no special meaning in Pascal.

Care must be taken when searching for comments because they are no longer comments when they occur within strings. For instance, (* this is a comment *) is a comment, on the other hand '(* this is not a comment *)' isn't a comment, it is a string.

You are to write a program that examines a Pascal program and prints all valid comments found in the program. Only the comments are to be printed. No other parts of the program are to be printed. The comments are to be printed exactly as they appear in the program. Indentation and spacing inside and outside the comments must be maintained. That is, if a comment starts in column 15 of the program and runs through column 50, then when you output this comment it must also appear in those same columns. If an input line contains no comments, just ignore it.

No line will be longer than 72 columns. The program will contain no tab characters or other unusual characters that might fool you. The end of input data is marked by the system end-of-file mark.

Sample Input

(*****************************************)
(* This program shows 9 uses of comments *)
(*****************************************)
PROGRAM sample(input,output);
  CONST left = '(*'; right = '*)';
BEGIN  (* sample *)
  (***********************
   * Illustrate Comments *
   ***********************)
  writeln('This is an empty comment', left, right);      (* #1 *)
  writeln('This comment encloses one space (* *)');      (* #2 *)
  writeln('Single quote '' -- Double quote "');          (* #3 *)
END.  (* sample *)

Corresponding Output

(*****************************************)
(* This program shows 9 uses of comments *)
(*****************************************)
       (* sample *)
  (***********************
   * Illustrate Comments *
   ***********************)
                                                         (* #1 *)
                                                         (* #2 *)
                                                         (* #3 *)
      (* sample *)