410667: GYM104072 E Language

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Languagetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard output

Captain Nix is passionate about languages. Programming languages especially. Now there's this new cool programming language called AGM (A Great programming not Markup language) with the following rules:

A valid instruction is either of the following:

  1. ; – The null instruction which does nothing
  2. BEG; – Marks the beginning of the program. Having any instruction before this one results in an error.
  3. END; – Marks the end of the program. Having any instruction after this one results in an error.
  4. $<name>; – Declare a variable with name $<name> and default value 0; Redeclaration of a variable results in an error.
  5. $<name> := <expression>; – Variable with name $name takes the value of the evaluated expression. If the variable is not declared, the result is error.
  6. <name>; – a label is declared with name <name>. Redeclaring a label results in an error (eg. redeclaring the BEG tag).
  7. GOTO <name>; – Jumps to the instruction following the tag <name>. Trying to jump to an undeclared tag results in an error.
  8. PRINT <expression>; – Prints the result of the expression
  9. BZ (<expression>) <instruction> – If the expression evaluates to $$$0$$$ then execute the instruction. In this particular case, the instruction cannot be a label declaration. Such a case will result in an error.
  10. BG (<expression>) <instruction> – If the expression evaluates to a value strictly greater than $$$0$$$, then execute the instruction. In this particular case, the instruction cannot be a label declaration. Such a case will result in an error.

A <name> is defined as follows:

  • is a string which starts with an letter
  • the following characters are alphanumeric (letters, decimals, underscores)
  • are used for variable names and label names
  • a variable name including $ must be at most $$$32$$$ characters in length. trying to access or declare a variable with a longer name will result in an error.
  • a label name must be at most $$$32$$$ characters in length. Trying to access or declare a label with a longer name will result in an error.

An Expression (<expression>) is defined recursively as follows:

  • Expression $$$\rightarrow$$$ (Expression) OR Expression <binary_operator> Expression OR Integer (eg. ($a + 2) * 4 / -$b)
  • Integer $$$\rightarrow$$$ integer_value OR <variable> OR <unary_operator><variable> (eg. 12 or -$v_2)

An <unary_operator> is either of the characters in this set {+, -, $$$\sim$$$}. A binary operator is any of the characters in this set {+, -, *, /, %, **, ^, &, |}.

The list of operators in them of descending priority is:

  • () (paranthesis)
  • ** (the power operator)
  • + - $$$\sim$$$ (positive, negative, bitwise not)
  • * / % (multiplication, division, modulo)
  • + - (addition, subtraction)
  • & (bitwise and)
  • ^ (bitwise xor)
  • | (bitwise or)

The list of keywords: ['BEG', 'END', 'BN', 'BZ', 'GOTO', 'PRINT']

Please note that:

  • Leading and Trailing whitespaces are ignored
  • Everything after the first ; on every line is ignored (like a comment)
  • Instructions not ending in ; will result in an error
  • The BEG; and END; instructions are inherently label definitions. Thus, you can use BEG and END in the GOTO command.
  • Variable scope is global. Variables can only be used after declaration.
  • Between operands, operators and keywords any number of whitespaces can be used, as they do not affect the execution of the program.
  • Empty lines (i.e. containing only whitespaces) are ignored and are not considered instructions.
  • A nonempty line, which does not fit the definition of any of the possible instructions will result in an error.
  • The language only accepts $$$32$$$ bit signed integers. Any explicit value (integer_value field) should fall in the limits of this data type. Otherwise, the execution results in an error.
  • If any intermediary or final result or an expression overflows is off-limits (needs more than $$$32$$$ bits to be represented), the respective value will be cropped like in any other C-like language (i.e. a bitwise and (&) with $$$(2^{32} - 1)$$$ will be applied).
  • Any division by $$$0$$$ results in an error.
  • /, % represent integer division (i.e. only the whole part of the result) and remainder after integer division.
  • If the final result of an expression is non integer execution terminates with error.
  • $$$\sim$$$ is the tilde symbol.

Captain Nix has some programs written in this amazing programming language and wonders what they do. Can you help out?

Input

The input files will contain characters on at most $$$100$$$ lines, representing the source code. It is guaranteed that there will be at most $$$10^3$$$ characters in the input and there will be at most $$$10^4$$$ instructions executed.

Output

Output the result of each print instruction on a separate line if there no error of any kind in the code (syntax or runtime error). Otherwise output a single line containing the text "error" with none of the potential output.

ExamplesInput
PRINT $_5a;
GOTO BEG
$b = 10 */- 7;
END;
Output
error
Input
BEG;

PRINT 100 ** 2 * (3 ^ 1);

END;
Output
20000
Input
BEG;
$a;
$b;
$c;
$a := 0;
$b := 1;
start_loop;
$c := $a + $b;
BG ($c - 100) GOTO END;
PRINT $c;
$a := $b;
$b := $c;
GOTO start_loop;
END;
Output
1
2
3
5
8
13
21
34
55
89
Note

The first example outputs fibonacci terms less than 100.

The second example outputs the result of $$$100^2 \times (3 \oplus 1)$$$.

The third example has multiple errors. Not starting with BEG, improper variable name, use of undeclared variables, jump to non-declared labels, use of the wrong assignment operator, wrong expression formatting, instruction not ending with ; .

加入题单

上一题 下一题 算法标签: