What Next
Time limit2sMemory limit512 MB
Given a prefix of an NZPC Speak program cut at an arbitrary point, list the symbols that can legally come next, respecting declarations, masking, and partial names.
- Level
Hard8 of 10
- Topics
- Implementation, Simulation, Stack, String
- Solved
- No attempts yet
Problem
An editor for the language NZPC Speak shows completion hints. It takes the beginning of a program and lists every symbol that can legally be typed next. Write that predictor.
A program in NZPC Speak obeys these rules.
- A program is a block.
- A block is a sequence of declarations and statements enclosed in braces
{and}. The sequence may be empty. - A declaration is
intorfloatfollowed by a comma separated list of names, terminated by a semicolon. There must be at least one name. - A statement is a block or an assignment statement.
- An assignment statement is a name, followed by
=, followed by an expression, terminated by a semicolon. Assignment does no type checking. - An expression is one or more terms joined by the operators
+,-,*and/. A term is a name or an integer. Integers and floats mix without restriction, and there are no parentheses. - A name is made of upper and lower case letters (which count as distinct), digits and underscores. The first character of a name must not be a digit.
- An integer is a sequence of digits.
- The reserved words
intandfloatmay not be used as names. - A name must be declared before it is used, both inside an expression and as the target of an assignment.
- A name declared in an inner block masks the same name from the enclosing blocks, from its point of declaration until the
}that closes the inner block. If a name is declared twice in one block, the later declaration replaces the earlier one from its point of declaration. - Spaces and newlines may appear between tokens. Each of
intandfloatmust be followed by at least one space or newline.
This is a complete program.
{
int abc, abcd, abcdef;
float ab, x, xy, xz;
abc = 12 * xy + 99;
{
float abc;
int xy;
xy = abc + abcd;
}
}
Each input text is the beginning of a program that obeys every rule above, cut off at an arbitrary character. Report the symbols that can come next. A predicted symbol is a punctuation mark or an operator ({, }, ,, ;, =, +, -, *, /), a reserved word (int or float), or a name that is visible at the cut. Integers are never predicted, because there are infinitely many of them.
Let p be the longest run of name characters (letters, digits, underscores) at the very end of the text. p is empty when the text is empty or ends with a space, a newline, or a punctuation mark.
If p is empty, list every symbol that can legally start at the cut.
If p is not empty, the user is partway through typing a name or a reserved word, so list only the symbols that continue it: every reserved word and every visible name that starts with p and that can legally start at the position where p begins. A name that is already spelled out in full still appears, because more characters could still follow it. If an integer is being typed, p starts with a digit, and no name or reserved word starts with a digit, so nothing is listed.
When the symbol being typed is a new name in a declaration, nothing is listed, whether p is empty or not.
Input
The first line holds the number of programs T (1 <= T <= 100). The programs follow one after another. Each program takes 1 to 100 lines and its last line ends with @, which marks the cut and is not part of the program text. Every line holds at most 200 characters.
Output
For each program print a line Problem #, where # is the program number counting from 1, then one line for each predicted symbol. Sort the symbols by this character order.
* , / ; _ { } + - = 0 1 2 3 4 5 6 7 8 9 a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s S t T u U v V w W x X y Y z Z
Compare two symbols character by character from the left using that order, and put a symbol that is a prefix of another one first. Print a name with its type in parentheses right after it, for example xy(float), and use only the name itself in the sort. Print reserved words and punctuation on their own.