Pascal Program Lengths
Time limit1sMemory limit128 MB
Count the scored tokens (reserved words, identifiers, constants, parentheses, brackets, and listed operators) in each Turbo Pascal program, skipping comments and strings, and print the member's name with the total.
- Level
Medium6 of 10
- Topics
- String, Implementation, Simulation, String matching
- Solved
- No attempts yet
Problem
A local computer users' group publishes a quarterly newsletter, and each issue contains a small Turbo Pascal programming problem for members to solve. Members submit their solutions to the editor, and the member who submits the shortest solution wins a prize.
The length of a program is measured in units. The unit count is the total number of occurrences of:
- reserved words,
- identifiers,
- constants,
- left parentheses
(, - left brackets
[, and - the operators
+,-,*,/,=,<,>,<=,>=,<>,@,^, and:=.
Comments are ignored, as is every other symbol that does not fall into one of the categories above. The program with the lowest unit count wins; two or more programs with equal unit counts split the prize.
Write a program that reads a series of Turbo Pascal programs and prints the number of units in each.
Input
The input is a series of Turbo Pascal programs. Each program is terminated by a line whose first two columns contain tilde characters (~~), immediately followed by the name of the submitting member. Every program is syntactically correct and uses the standard symbols for comments (braces { }) and subscripts (square brackets [ ]). Input ends at end of file.
Output
For each program, print a single line containing the submitting member's name and the program's unit count, using exactly this format:
Program by NAME contains N units.
Additional notes on Turbo Pascal tokens:
- Identifiers start with an underscore (
_) or a letter (upper- or lower-case), followed by zero or more underscores, letters, or digits. - A string constant is delimited by single forward quotes (
'). Each string lies entirely on one source line. Two consecutive quotes ('') inside a string represent a single'character, so a string consisting of one'character is written''''. The empty string''is allowed. - A numeric constant has the general form
10.56E-15: an integral part (one or more digits, always present), an optional decimal part (a.followed by digits), and an optional exponent (an upper- or lower-caseE, an optional sign+/-, and digits). - A hexadecimal integer constant is a
\$followed by one or more hex digits (0–9,a–f,A–F); for example\$a9F. - The only comment delimiters to recognize are braces
{ }; do not treat(**)as comments. Comments do not nest. - Treat
+and-as operators whenever possible: inx := -3, the-and the3are separate tokens. - A subrange of an ordinal type is written
lower..upper; for example1..10covers the integers from 1 to 10. - Every token not described above consists of a single character.