2AM Rule

The engineers working at MindCoding wrote the website in x86 assembly to make it run faster. Since megeve, one of MindCoding's software engineers, distrusts the long-established Turbo Assembler 5.0, he decided that he will write a better, leaner, faster x86 assembler.

Yesterday, megeve rewrote a large part of his assembler. Unfortunately, he introduced quite a few bugs this way. As the second round of the MindCoding National Contest is today. megeve is now frantically working to fix the bugs.

One of the most problematic sections of the assembler is the syntax checker. Today, you will help megeve by writing a program that checks if a given memory address is valid.

A memory address in x86 assembly is a mathematical expression containing registers, variables, constants and operators which follows these rules:

  • There are two kinds of registers: base registers and index registers
  • The base registers are BX and BP (case-insensitive). There may be no more than one base register in an address.
  • The index registers are SI and DI (case-insensitive). There may be no more than one index register in an address.
  • Variables are represented by one lowercase letter of the Latin alphabet. There may be no more than one variable in an address.
  • Constants are nonnegative integers.
  • The operators are +, -, and []. The last operator is special — anywhere in the expression, [subexpression] is equivalent to +subexpression. Thus, a[DI+3] is equivalent to a+DI+3
  • An expression can contain any number of consecutive + and - operators. Thus, a+++++3, BX+-+-+-+5[a], [a++++++++----------+-+---+++----+3] and a[[[3]]] (which is equivalent to a+++3) are all valid.
  • A (sub)expression may not start or end with a + or a - operator. Thus, +3, a[+5], 5+BX-, a[BX-] are invalid.
  • An expression may not contain any characters other than +, -, [, ], a-z, 0-9, BDIPSX. Any other character renders the expression invalid.
Examples of valid addresses:
Examples of invalid addresses:
V[4]     ; Variables are lowercase
a[2*2]   ; There is no * operator
[a+2*2]  ; Same here
v[AX]    ; AX can't appear in an offset speciffication
[BX][BP] ; BX and BP can't appear at the same time
[BX+BP]  ; Same here
[BX+BX]  ; The registers or variables can't appear more than once
[a+b]    ; Same here
[ a ]    ; Spaces are disallowed
(1)      ; The ( and ) characters are also disallowed


Each line of the input contains a memory address. Each line is at most 100 characters long. The file contains at most 100 lines, and the file ends with a newline character.


For each address print OK if it is valid or ERROR if it isn't.



Sponsors Gold