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<iostream>
#include<string>
#include<stack>

using namespace std;

int main(){
  bool flg=false,bflg;
int i;
  while(!flg){
    /*行ループ*/
    string s;
    stack<char> box;
    char ch;
    getline(cin,s);

    if(s==".")flg=true;
    else {
      /*区画ループ*/
      bflg=true;
      box.push('s');

      for(i=0;i<s.size();i++){
        ch=s.at(i);
        if(ch=='('||ch=='[')box.push(ch);
        else if(ch==')'){
          if(box.top()=='(')box.pop();
          else{
            bflg=false;
            break;
          }
        }
        else if(ch==']'){
          if(box.top()=='[')box.pop();
          else{
            bflg=false;
            break;
          }
        }
        else if(ch=='.'){
          break;
        }
      }
      if(bflg==true && box.top()=='s')cout<<"yes"<<endl;
      else cout<<"no"<<endl;
    }
  }
}

C++: /*
 * The Balance of the World
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1173&lang=jp
 */

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;

int main() {
  string s;
  while(getline(cin, s), s != ".") {
    stack<char> balance;
    REP(i, s.length()){
      switch (s[i]) {
      case '[':
        balance.push('[');
        break;
      case '(':
        balance.push(')');
        break;
      case ']':
        if(balance.empty() || balance.top() != '[') {
          cout << "no" << endl;
          goto END;
        }
        balance.pop();
        break;
      case ')':
        if(balance.empty() || balance.top() != ')') {
          cout << "no" << endl;
          goto END;
        }
        balance.pop();
        break;
      }
    }
    cout << "yes" << endl;
END:;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
int main(){
  string str;
  string tmp;
  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;

    
    stack<char> st;
    
    for(int i = 0 ; i < tmp.size() ; i++){
      if(tmp[i] == '(' || tmp[i] == '[') st.push(tmp[i]);
      else if(tmp[i] == ')'){
	if(st.empty() || st.top() != '('){ flag = 0; break;}
	else st.pop();
      }
      else if(tmp[i] == ']'){
	if(st.empty() || st.top() != '['){ flag = 0; break;}
	else st.pop();
      }
    }
    
    //cout << tmp << endl;
    //printf("p = %d, q = %d\n", p, q);*/
    if(flag) cout << "yes" << endl;
    else cout << "no" << endl;    
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
using namespace std;

int main() {
	string a;
	while (getline(cin,a)) {
		vector<int>s;
		for (int b = 0; b < a.length(); b++) {
			if (a[b] == '(' || a[b] == '[') { s.push_back(a[b]); }
			else if (a[b] == ')') {
				if (s.size() == 0 || s[s.size() - 1] !='(') { goto l; }
				s.pop_back();
			}
			else if (a[b] == ']') {
				if (s.size() == 0 || s[s.size() - 1] != '[') { goto l; }
				s.pop_back();
			}
		}
		if (s.size() != 0)goto l;
		cout << "yes" << endl;
		goto r;
	l:;
		cout << "no" << endl;
	r:;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<stack>
#include<string>
using namespace std;
#define REP(i, b, e) for ( int i = b; i < (int)e; i++ )
#define rep(i, n) REP(i, 0, n)

bool check(string line){
  stack<char> S;
  char ch;
  for ( int i = 0; i < line.size()-1; i++ ){
    if ( line[i] == '(' ){
      S.push('(');
    } else if ( line[i] == '[' ){
      S.push('[');
    } else if ( line[i] == ')' ){
      if ( S.size() == 0) return false;
      ch = S.top(); S.pop();
      if ( ch != '(' ) return false;
    } else if ( line[i] == ']' ){
      if ( S.size() == 0) return false;
      ch = S.top(); S.pop();
      if ( ch != '[' ) return false;
    } 
  }
  return S.size() == 0;
}

main(){
  string line;
  while(1){
    getline(cin, line);
    if ( line == "." ) break;
    if ( check(line) ) cout << "yes" << endl;
    else cout << "no" << endl;
  }
}

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

#define REP(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int (i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) (V).begin(), (V).end()
#define PRINT(STR) cout << (STR) << endl

typedef long long ll;
using namespace std;

int main() {
	cin.tie(0); //対話型には使えない
	ios::sync_with_stdio(false); //printfと共には使えない
	char stc[1024];
	int counter = 0;
	string str;
	while (str = "", getline(cin, str), str != ".") {
		counter = 0;
		REP(i, str.size()) {
			if (str[i] == '[' || str[i] == '(') {
				stc[counter++] = str[i];
			} else if (str[i] == ']') {
				if (counter == 0 || stc[--counter] != '[') {
					PRINT("no");
					break;
				}
			} else if (str[i] == ')') {
				if (counter == 0 || stc[--counter] != '(') {
					PRINT("no");
					break;
				}
			}
			if (i == str.size() - 1) {
				PRINT("yes");
			}
		}
	}
	return 0;
}