package chapter4;
import java.util.Scanner;
public class Bracket {
/**
* @括号匹配a{b[c(d)e]f}
*/
public static void main(String[] args) {
String input;
while(true){
Scanner sc = new Scanner(System.in);
input = sc.nextLine();
if(input.equals(""))
break;
BraketChecker theChecker = new BraketChecker(input);
theChecker.check();
}
}
}
class StackII{
private int maxSize;
private char[] stackArray;
private int top;
public StackII(int maxSize){
this.maxSize = maxSize;
stackArray = new char[this.maxSize];
top = -1;
}
public void push(char value){
stackArray[++top] = value;
}
public char pop(){
return stackArray[top--];
}
public char peek(){
return stackArray[top];
}
public boolean isEmpty(){
return (top==-1);
}
}
class BraketChecker{
private String input;
public BraketChecker(String in){
input = in;
}
public void check(){
int maxSize = input.length();
StackII theStack = new StackII(maxSize);
for(int i=0;i<maxSize;i++){
char ch = input.charAt(i);
switch(ch){
case '{':
case '(':
case '[':
theStack.push(ch); break;
case '}':
case ')':
case ']':
if(!theStack.isEmpty()){
char temp = theStack.pop();
if( (ch=='{' && temp!='}') || (ch=='(' && temp!=')') || (ch=='[' && temp!=']') )
System.out.println("error:"+ch+"at"+i);
}
else
System.out.println("error:"+ch+"at"+i); break;
default:break;
}
}
if(!theStack.isEmpty())
System.out.println("error:missing right delimiter");
}
}