//Check if parenthesis are balanced or not
import java.io.*;
class stack
{   int c[]=new int[10];
    int top=-1;
    void push(int ind)
    {   c[++top]=ind;
    }
    int pop()
    {   if(isEmpty()) return -1;
        else return c[top--];
        
    }
    boolean isEmpty()
    { return top==-1;
    }
}
public class ParenthesisMatching
{
public static void main(String args[])throws IOException
{   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a equation containing () e.g. (a+b(/(c+d)");
    String eqn=br.readLine();
    stack s=new stack();
    for(int i=0; i<eqn.length();i++)
    {   if(eqn.charAt(i)=='(')
            s.push(i);
        if(eqn.charAt(i)==')')
        {   if(s.isEmpty())
            {   System.out.println("unmatched ( at"+i);
            }
            else
            {   System.out.println(") at "+i+" matched with "+s.pop());
            }
        }
    }//for
    while(!s.isEmpty()) 
    {   System.out.println("unmatched  at "+s.pop());
    }
}//main
}//class
 
