#include #include #include #include using namespace std; char upc(char c){ if(c>='a'&&c<='z') c=c-'a'+'A'; return c; } char s[150]; int ind; bool was_index; bool was_base; bool was_variable; bool term(){ if(upc(s[ind]) == 'B' && (upc(s[ind+1])=='X'||upc(s[ind+1])=='P') ){ if(was_base) return false; else{ ind+=2; was_base=true; return true; } } else if( (upc(s[ind]) == 'S' || upc(s[ind])=='D') && upc(s[ind+1])=='I'){ if(was_index) return false; else{ ind+=2; was_index=true; return true; } } else if(s[ind]>='a'&&s[ind]<='z'){ if(was_variable) return false; else{ ind++; was_variable=true; return true; } } else if(isdigit(s[ind])){ while(isdigit(s[ind])) ++ind; return true; } else return false; } bool expr(){ int got_term1=false; if(s[ind]=='['){ ++ind; if(!expr()) got_term1=false; else if(s[ind]!=']') got_term1=false; else{ ++ind; got_term1=true;} } else got_term1=term(); if(!got_term1) return false; else{ bool got_op=false; while(s[ind]=='+'||s[ind]=='-'){ got_op=true; ++ind; } if(s[ind]=='[') got_op=true; if(!got_op) return true; else return expr(); } } int main() { while(cin.getline(s,150) && strlen(s)>0){ ind=0; was_index=was_base=was_variable=false; if(expr()&&s[ind]=='\0') cout<<"OK\n"; else cout<<"ERROR\n"; } }