import java.io.*;
import java.util.*;

public class Brackets
{
    private static final int ROUND = 1;
    private static final int CURLY = 2;
    private static final int SQUARE = 3;

    private static int numRound;
    private static int numCurly;
    private static int numSquare;

    private static char toTest;

    private static boolean wasError;

    private static Stack store;

    private static Integer wasLast;

    public static void main(String[] args) throws IOException
    {
	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	    
	numRound = 0;
	numCurly = 0;
	numSquare = 0;

	store = new Stack();

	wasError = false;
	wasLast = new Integer(0);
	
	System.out.println("Enter brackets expression to check:");
	String brack = input.readLine();
	
	for (int i=0; i<brack.length(); i++)
	    {
		toTest = brack.charAt(i);
		
		switch(toTest)
		    {
		    case '(' :
			numRound++;
			store.push(new Integer(ROUND));
			break;
		    case '{' :
			numCurly++;
			store.push(new Integer(CURLY));
			break;
		    case '[' :
			numSquare++;
			store.push(new Integer(SQUARE));
			break;
		    case ')' : //------------------- round ------------------
			
			if(store.empty() || numRound<=0)
			    {
				System.out.println("Closing ')' found with no matching opener at column " + i);
				wasError = true;
			    }
			else
			if(wasLast.intValue() != ROUND)
			    {
				System.out.println("Closing bracket ')' does not match last opened bracket at column " +i);
				wasError = true;
			    }

			if(!store.empty())
			    {
				store.pop();
			    }
			numRound--;
			break;
		    case '}' : //--------------------- curly --------------------
			
			if(store.empty() || numCurly<=0)
			    {
				System.out.println("Closing '}' found with no matching opener at column " + i);
				wasError = true;
			    }
			else
			if(wasLast.intValue() != CURLY)
			    {
				System.out.println("Closing bracket '}' does not match last opened bracket at column " +i);
				wasError = true;
			    }

			if(!store.empty())
			    {
				store.pop();
			    }
			numCurly--;
			break;
		    case ']' : //------------------ square -------------------
			
			if(store.empty() || numSquare<=0)
			    {
				System.out.println("Closing ']' found with no matching opener at column " + i);
				wasError = true;
			    }
			else
			if(wasLast.intValue() != SQUARE)
			    {
				System.out.println("Closing bracket ']' does not match last opened bracket at column " +i);
				wasError = true;
			    }

			if(!store.empty())
			    {
				store.pop();
			    }
			numSquare--;
			break;
		    default :
			break;
		    }//end of switch

		if(!store.empty())
		    {
			wasLast = (Integer)store.peek(); // sets value up for next loop
		    }
	    }//end of for

	if(numRound>0)
	    {
		System.out.println("End of expression reached, and there are unclosed '(' brackets");
		wasError = true;
	    }

	if(numCurly>0)
	    {
		System.out.println("End of expression reached, and there are unclosed '{' brackets");
		wasError = true;
	    }


	if(numSquare>0)
	    {
		System.out.println("End of expression reached, and there are unclosed '[' brackets");
		wasError = true;
	    }

	if(!wasError)
	    {
		System.out.println("No bracket errors found");
	    }

    }//end of main
    
}//end of class
