#!/usr/bin/ruby

$base_found = false
$index_found = false
$var_found = false
$pos_var = false
$expression_start = true
$mem_chr = nil
$good = true
$sign_present = false

def resetVars()
	$sign_present = false
	$base_found = false
	$index_found = false
	$var_found = false
	$pos_var = false
	$expression_start = true
	$mem_chr = nil
	$good = true
end

def checkSquareBrackets(line)
	count = 0
	line.each_char do |char|
		if char == '['
			count += 1
		elsif char == ']'
			count -= 1
		end
		if count < 0
			return false
		end
	end
	if count!=0
		return false
	end
	return true
end

def checkChar(char)
	return char == '+' || char == '-' || char == '[' || char == ']' || (('a'..'z').include? char) || (('0'..'9').include? char) || (('BDIPXS').include? char)
end

def ok()
	puts "OK"
end

def error()
	puts "ERROR"
	$good = false
end

$stdin.each do |line|
	line = line.chomp
	if line.length == 0
		break
	end
	if checkSquareBrackets(line)
		resetVars()
		line.each_char do |char|
			if checkChar(char)
				if $expression_start && (char == '+' || char == '-')
					error()
					break
				end
				if char == ']' && $sign_present
					error()
					break
				end
				if ('a'..'z').include? char.downcase
					if $mem_chr
						reg = $mem_chr + char.downcase
						if reg == 'bp' || reg == 'bx'
							if $base_found
								error()
								break
							else
								$base_found = true
							end
						elsif reg == 'si' || reg == 'di'
							if $index_found
								error()
								break
							else
								$inedx_found = true
							end
						else
							error()
							break
						end
						$pos_var = false
					else
						$pos_var = true
					end
				else
					if $pos_var
						$pos_var = false
						if $var_found
							error()
							break
						else
							$var_found = true
						end
					end
				end
				$mem_chr = (('a'..'z').include? char.downcase) ? char.downcase : nil
				$expression_start = char == '['
				$sign_present = char == '+' || char == '-'
			else
				error()
				break
			end
		end
		if $good && $pos_var && $var_found
			error()
		end
		if $good && $sign_present
			error()
		end
		if $good
			ok()
		end
	else
		error()
	end
end