Go: package main

import (
	"bufio"
	"fmt"
	"os"
)

var sc = bufio.NewScanner(os.Stdin)

func nextString() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	sc.Buffer(make([]byte, 0), 1000000009)

	for {
		var s string
		s = nextString()
		if s == "." {
			break
		}
		var st []byte
		is_valid := true

		for _, value := range s {
			if value == '(' {
				st = append(st, byte(value))
			}
			if value == '[' {
				st = append(st, byte(value))
			}
			if value == ')' {
				if len(st) == 0 || st[len(st)-1] != '(' {
					is_valid = false
					break
				} else {
					st = st[:len(st)-1]
				}
			}
			if value == ']' {
				if len(st) == 0 || st[len(st)-1] != '[' {
					is_valid = false
					break
				} else {
					st = st[:len(st)-1]
				}
			}
		}
		if len(st) != 0 {
			is_valid = false
		}
		if is_valid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

#define MARU 0
#define KAKU 1

int check(char c) {
  if (c == '(' ||
      c == ')') {
    return MARU;
  }
  if (c == '[' ||
      c == ']' ) {
    return KAKU;
  }
  return -1;
}

int main() {
  string str;
  while(true) {
    getline(cin, str);
    if (str == ".") break;

    string tstr;
    for (int i = 0; i < str.size(); i++) {
      if (check(str[i]) != -1) {
	tstr += str[i];
      }
    }

    if (tstr.size() == 0) {
      std::cout << "yes" << std::endl;
      continue;
    }
    if (tstr[0] == ')' ||
	tstr[0] == ']') {
      std::cout << "no" << std::endl;
      continue;
    }

    // std::cout << tstr << std::endl;
    bool yes = true;
    stack <int> st;
    st.push(check(tstr[0]));
    for (int i = 1; i < tstr.size(); i++) {
      if (st.size()) {
	int val = st.top();
	if (tstr[i] == '(' ||
	    tstr[i] == '[') {
	  st.push(check(tstr[i]));
	}else {
	  if (val != check(tstr[i])) {
	    yes = false;
	    break;
	  }else {
	    st.pop();
	  }
	}
      }else {
	if (tstr[i] == ')' ||
	    tstr[i] == ']') {
	  yes = false;
	  break;
	}else {
	  st.push(check(tstr[i]));
	}
      }
    }

    if (yes) {
      std::cout << "yes" << std::endl;
    }else {
      std::cout << "no" << std::endl;
    }
  }
}

C++: #include<iostream>
#include<string>
using namespace std;
string a;
int saiki(int,int);
int main(){
  int ans;
  while(1){
    a.clear();
    getline(cin,a);
    if(a==".")break;
    ans=saiki(0,0);

    //out 
    if(ans==-1)cout << "no" << endl;
    else cout << "yes" << endl;
  }
  return 0;
}
int saiki(int I,int n){//n:1=( 2=[
  for(int i=I;i<=a.size();i++){
    if(a[i]=='('){
      i=saiki(i+1,1);
      if(i==-1)return -1;
    }
    else if(a[i]=='['){
      i=saiki(i+1,2);
      if(i==-1)return -1;
    }
    else if(a[i]==')'){
      if(n==1)return i;
    }
    else if(a[i]==']'){
      if(n==2)return i;
    }
    else if(a[i]=='.'){
      if(n==0)return 0;
    }
  }
  return -1;
  
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
  string str;
  string tmp, prev, next;
  cin.ignore();
  while(getline(cin, str) && str != "."){
    int p = 0, q = 0;
    bool flag = 1;
    tmp = "";
    
    for(int i = 0 ; i < str.size() ; i++){
      if(str[i] == '[' || str[i] == '(' || str[i] == ']' || str[i] == ')'){
	tmp += str[i];
      }
    }
    
    for(int i = 0 ; i < tmp.size() ; i++){
      if(tmp[i] == '(' && tmp[i+1] == ']') flag = 0;
      if(tmp[i] == '[' && tmp[i+1] == ')') flag = 0;
    }

    for(int i = 0 ; i < tmp.size() ; i++){
      //printf("p = %d, q = %d\n", p, q);
      if(p < 0 || q < 0){
	flag = 0;
	break;
      }
      if(tmp[i] == '(') p++;
      if(tmp[i] == '[') q++;
      if(tmp[i] == ')') p--;
      if(tmp[i] == ']') q--;
    }
    if(p != 0 || q != 0) flag = 0;
    //cout << tmp << endl << endl;
    if(flag == 1) cout << "yes" << endl;
    else cout << "no" << endl;    
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;


char str[104];

int main(){
    while(1){
        stack<int> st;
        bool judge = true;

        fgets(str, 104, stdin); 
        if(str[0]=='.')break;
    
        int i=0;
        while(str[i] != '.'){
            if(str[i]=='(' || str[i]=='['){
                st.push(str[i]);
            }else if(!st.empty()){
                if(str[i]==')'){
                    if(st.top() == '('){
                        st.pop();
                    }else{
                        judge = false;
                    }
                }else if(str[i]==']'){
                    if(st.top() == '['){
                        st.pop();
                    }else{
                        judge = false;
                    }
                }
            }else if(st.empty()){
                if(str[i]==')' || str[i]==']') judge = false;
            }
            if(judge==false){
                break;
            }
            i++;
        }
        if(!st.empty()){
            judge = false;
        }
        if(judge){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }

}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>
#include <stack>

using namespace std;

#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)

const string op = "[(", cl = "])";

int main(){
  int i,j;
  string in;

  while(1){
    getline(cin,in); // cout << in << endl;
    if(in == ".") break;

    bool f = true;
    int N = in.length();
    stack<char> st;

    REP(i,N) REP(j,2){
      if(in[i] == op[j]) st.push(in[i]);
      if(in[i] == cl[j]){
        if(!st.empty() && op[j] == st.top()) st.pop();
        else f = false;
      }
    }

    if(!st.empty()) f = false;
    cout << ((f) ? "yes" : "no") << endl;
  }

  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<stack>
#include<cstring>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

int main(){
	
	while(1){
		char str[111];
		gets(str);
		
		if(str[0]=='.' && strlen(str)==1)break;
		
		int valid = 1;
		stack<char> c;
		int n = strlen(str);
		rep(i,n){
			if(str[i]=='(' || str[i]=='['){
				c.push(str[i]);
			}
			if(str[i]==')' || str[i]==']'){
				if(c.empty()){
					valid=0;
					break;
				}
				char s = c.top();
				c.pop();
				
				if((str[i]==')' && s=='[')||
					(str[i]==']' && s=='(')){
					valid=0;
					break;	
				}
			}
		}
		if(!c.empty())valid=0;
		
		if(valid)puts("yes");
		else puts("no");
	}
}

C++: #include<iostream>
#include<string>
#include<stack>

using namespace std;

int main(void){
  string s;
  stack<char> str;

  while(getline(cin,s) && s!="."){
    bool flag = true;
    string ch;
    for(int i=0;i<s.size();i++)
      if(s[i]=='{' || s[i]=='(' || s[i]=='}' || s[i]==')')ch+=s[i];

    for(int i=0;i<ch.length();i++){
      
      if(str.empty() || ch[i]=='(' || ch[i]=='{')str.push(ch[i]);
      else if(ch[i]==')' && str.top()=='(')str.pop();
      else if(ch[i]=='}' && str.top()=='}')str.pop();
      else {
	flag = false;
	break;
      }
    }

    cout << (flag && str.empty()?"YES":"NO") << endl;

  }

  return 0;
}