Comment removal
Time limit1sMemory limit128 MB
Strip Pascal comments and collapse whitespace, honoring single-quote strings where doubled quotes are literals and comments can span lines.
- Level
Medium6 of 10
- Topics
- String, Simulation, Implementation, String matching
- Solved
- No attempts yet
Problem
Given an arbitrary, syntactically correct Pascal program, compress it. (You do not need to know the Pascal programming language to solve this problem; all the information you need is given below.) Apply the following rules to compress the program:
- Replace every sequence of blanks, except those inside a string constant, with a single blank. Also remove all blanks at the beginning of a line and all blanks at the end of a line.
In Pascal, strings are delimited by single quotes (
'). Inside a string, two consecutive single quotes denote one single-quote character. For example:
A string may not cross a line boundary.'This is a string' 'isn''t this fun?' - Replace every comment with a single blank. (If this blank ends up next to another blank, the two are compressed as described above.)
In Pascal, a comment is enclosed either by
(* ... *)or by{ ... }, as in:
A comment may span several lines. Comments may not be nested.(* This is a comment! *) { A comment with a '*)'. } (* A (* comment *) - Remove every totally blank line, i.e. a line that contains only blanks (either originally, or after the comments have been removed).
- The source contains no tab characters and no other non-printing characters (apart from line separators).
- No line of the source is longer than 255 characters.
Input
A single, syntactically correct Pascal program is given on standard input. It contains no tab characters and no other non-printing characters (apart from line separators), and no line is longer than 255 characters.
Output
Output the compressed Pascal program obtained by applying the rules above, and nothing else.