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
andBP
(case-insensitive). There may be no more than one base register in an address. - The index registers are
SI
andDI
(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 toa+DI+3
- An expression can contain any number of consecutive
+
and-
operators. Thus,a+++++3
,BX+-+-+-+5[a]
,[a++++++++----------+-+---+++----+3]
anda[[[3]]]
(which is equivalent toa+++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.
[BX]+[SI]+4 [BX+5]+v [BX+SI] [bP+3+2+1+0+1+2] [Bp+dI] v[BX][SI] [si][bx] [a] a[2]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
Input
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.Output
For each address printOK
if it is valid or ERROR
if it isn't. Sample
Input | Output |
---|---|
[BX]+[SI]+4 [BX][BP] | OK ERROR |