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.
Hard8ImplementationSimulationStackStringNo attempts yetTime limit2sMemory limit512 MBAn 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.
{ and }. The sequence may be empty.int or float followed by a comma separated list of names, terminated by a semicolon. There must be at least one name.=, followed by an expression, terminated by a semicolon. Assignment does no type checking.+, -, * and /. A term is a name or an integer. Integers and floats mix without restriction, and there are no parentheses.int and float may not be used as names.} 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.int and float must 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.
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.
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.