Dynamic Declaration Language (DDL)
Time limit1sMemory limit128 MB
Simulate a tiny language with dynamic variable declarations, jumps, and increments, printing a repeated-declaration or undeclared-reference error each time one occurs.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, String, Hash map
- Solved
- No attempts yet
Problem
DDL is a very simple programming language in which variables are declared dynamically at run time. Every variable in DDL is a signed integer in the range to . A DDL program contains up to five kinds of statements. Each statement occupies its own program line, and the first statement is on line 1.
Dcl <id>Dclis the keyword for a declaration statement.idis a single (case-sensitive) letter naming a DDL variable. For example, whenDcl xexecutes correctly, it allocates memory for variablexand sets its value to zero.
<id> = <ic>- An assignment statement, where
idis a DDL variable andicis a literal integer constant in the range to . For example, whenx = 2000executes correctly, it changes the value ofxto 2000. There may be one or more blank characters around=, but no tab characters.
- An assignment statement, where
Goto <label>orGoto <id> <label>Gotois the keyword for an unconditional or conditional jump.labelis a program line number. For example,Goto 5transfers execution to line 5. WhenGoto x 5executes correctly, it transfers the flow to line 5 if and only if , and to the next line otherwise.labelis guaranteed to be within the range of the program's line numbers.
Inc <id>orDec <id>IncandDecare the keywords for increment and decrement. For example, whenInc xexecutes correctly it adds 1 to the value ofx, andDec ysubtracts 1 from the value ofy.
EndEndis the keyword for the end statement; executing it stops the program.
The keywords of the DDL language are case-insensitive.
Error conditions
When one of the following erroneous statements is encountered during execution, an error message appears on its own line of the output. Each error message has the form <label> <space> <error code>, where label is the line number of the erroneous statement, the space is a single blank character, and error code is a positive integer defined below.
Dcl xis erroneous ifxhas not been referenced (used in an assignment, jump, increment, or decrement) since the last time aDcl xstatement (declaring the same variable) was executed — unless this is the firstDcl xstatement being executed. In this case a repeated-declaration error message<label> 1is generated. Execution then transfers to the statement on the next line, and any previously and correctly executed declaration ofxremains valid.- Any other statement that references a variable
x(in an assignment, jump, increment, or decrement) is erroneous if noDcl xhas previously been executed correctly. In this case an undeclared-reference error message<label> 2is generated, and execution continues from the next line.
Input
The first line of the input contains a single integer , the number of DDL programs that follow (). The first line of each program contains a single integer, the number of statements in that program, which is between and . There are no blank lines between programs. The statements of each DDL program are given one per line, with no blank lines between them. Statements are not labeled explicitly; instead they are labeled implicitly by their line number, starting from 1 for the first statement of each program. Programs contain no syntax errors, are guaranteed to terminate, and cause no overflow or underflow during execution. On each program line, tokens (such as Goto, =, and so on) are separated by at least one blank character, and there may be leading or trailing blank characters on a line.
Output
For each input DDL program, the first line of your output is the program number, followed by the error messages the program generates, in the order they are generated, one message per line. There must be no blank lines between error messages. Programs are numbered starting from 1.