FORCAL
Time limit1sMemory limit128 MB
Read text lines in blocks and scan each line into FORCAL tokens (identifiers, literals, symbols, keywords), printing TOKEN ERROR and skipping the block on an invalid string.
- Level
Medium5 of 10
- Topics
- String, Implementation, Simulation
- Solved
- No attempts yet
Problem
FORCAL is a small programming language popular with programmers interested in compiler construction, and especially with students taking a compiler-construction course. Its syntax is defined as follows.
- The only data type is the integer.
- Every identifier is declared implicitly and is at most 32 characters long. An identifier is made of letters, digits, and underscores (
_), and at least one of its characters is not a digit. - A literal is a string of at most 8 digits.
- A comment begins with
--and continues to the end of the line on which it starts. - There are two kinds of statements:
- Assignment:
identifier := expression, where an expression is built from identifiers, literals, the operators+and-, and parentheses, by the following rules:- every identifier and every literal is an expression;
- if
aandbare expressions, thena + b,a - b,+a,-a, and(a)are expressions.
- Input / output:
read(list of identifiers)andwrite(list of expressions), where the items of a list are separated by commas.
- Assignment:
begin,end,read, andwriteare reserved words.- Every statement ends with a semicolon (
;). - FORCAL is case-insensitive; for example,
BegINis the same keyword asbeGin. - A FORCAL token is an identifier, a literal, one of the symbols
+-():=;,, or a reserved word.
Notes:
- The assignment operator
:=is a single token. - Spaces, tabs, and line breaks are allowed between tokens.
- No part of a comment is a token.
- Two consecutive tokens that are both identifiers, literals, or reserved words must be separated by a space, a tab, or a line break.
- No token may contain a space, a tab, or a line break.
Write a program that reads lines of text and recognizes the FORCAL tokens in them.
Input
The input consists of several blocks of lines. Each block is a sequence of text lines and is terminated by a single empty line.
Output
For every input line the program produces output as follows. A non-empty line that is not being skipped is scanned from left to right, and each recognized FORCAL token is printed on its own line, written exactly as it appears in the input (letter case is preserved). As soon as the scanner meets a string that is neither a FORCAL token, a comment, nor whitespace (a space or a tab), it prints the line TOKEN ERROR and skips the rest of the current block. Every empty line, and every line that is skipped because its block already produced a TOKEN ERROR, is written to the output as a single empty line. Because each block ends with an empty line, this places at least one empty line after every block of output.