import java.io.*;

//vlastna vynimka
class InvalidCharException extends Exception {
  public InvalidCharException(){}
  public InvalidCharException(String msg){
    super(msg);
  }
  public InvalidCharException(String msg, int charErrorIndex){
    this(msg);
    charErrorPos=charErrorIndex;
  }
  public InvalidCharException(String msg, int charErrorIndex,
    String currString){
    this(msg);
    charErrorPos=charErrorIndex;
    currentString=currString;
  }
  int charErrorPos;
  String currentString;
}

public class treeParser {
  static String NUMERALS="01234567890 ";

  static String getRepeatedChar(char RepeatChar,int count){
    String s="";
    for (int i=0;i<count;i++){
      s=s+RepeatChar;
    }
    return s;
  }

  public static boolean parseParExpression(String x)
  throws InvalidCharException{
    int parcnt=0;
    if (x.charAt(0)!='[') {
      throw new InvalidCharException("Error: missing starting bracket",1,x);
    }
    //skontrolujeme zaciatocnu a koncovu zatvorku
    if (x.charAt(x.length()-1)!=']') {
      throw new InvalidCharException("Error: missing ending bracket",x.length()+1,x);
    }

    for (int i=1;i<x.length()-1;i++){//prvy a posledny znak skontrolujeme zvlast
      if (x.charAt(i)=='[') {
        parcnt++;
      } else {
        if (x.charAt(i)==']') {
          parcnt--;
        } else {
          if (NUMERALS.indexOf(x.charAt(i))==-1){
            throw new InvalidCharException("Error: incorrect character. " +
            "Allowed numerals and [ ] chars", i+1, x);
          }
        }
      }
      if (parcnt==0) {
		  throw new InvalidCharException("Error: recursive list is closed before end",i+1,x);
	  }
    }
    System.out.println();
    if (parcnt>0) {
      //throw new InvalidBracketException("Error: too many [ brackets");
      throw new InvalidCharException("Error: missing ] bracket",x.length()+1,x);
    }
    if (parcnt<0) {
      int prevRBracketPos=0;
      for (int j=1;j<x.length()-1;j++){
        if (x.charAt(j)==']'){
          if (prevRBracketPos==0){
            prevRBracketPos=j;
          } else{
            throw new InvalidCharException("Error: too many ] brackets",j,x);
          }
        }
      }
      //throw new InvalidBracketException("Error: too many ] brackets");
    }
    System.out.println("OK: Valid recursive list expression");
    return true;
  }
  public static void main(String[] args) throws IOException{
    String riadok;
    try {
      if (args.length>0) {
        riadok=args[0];
      }
      else {
      BufferedReader vstup =
        new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter input filename:");
      riadok = vstup.readLine();
      }
      java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(riadok));
      System.out.println("Reading input from file: " + riadok);
      String line;
      while ((line = in.readLine()) != null) {
        System.out.println("Getting input: " + line);
          try{
            parseParExpression(line);
          } catch (InvalidCharException e) {
            System.out.println(e.getMessage() + " at char: " + e.charErrorPos);
            System.out.println(e.currentString);
            System.out.println(getRepeatedChar(' ',e.charErrorPos-1) + '^');
          }
      }
      System.out.println("-EOF-");
    } catch (java.io.IOException e) {
      System.out.print(e.getMessage());
    }
  }
}