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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, -1, 0, 1 };

int nextInt() {int a; cin >> a; return a;}
char nextChar() {char a; cin >> a; return a;}
double nextDouble() {double a; cin >> a; return a;}
string nextString() {string a; cin >> a; return a;}

void inputVector(vector<int> &v, int &n) {rep(i,n){v.push_back(nextInt());}}
void inputVector(vector<double> &v, int &n) {rep(i,n){v.push_back(nextDouble());}}
void inputVector(vector<string> &v, int &n) {rep(i,n){v.push_back(nextString());}}

signed main() {
    string s;
    while (getline(cin, s)) {
	if (s == ".") break;

	stack<char> st;
	rep(i, s.size()) {
	    if (s[i] == '(' || s[i] == '[') {
		st.push(s[i]);
	    } else if (s[i] == ')') {
		if (st.size() > 0 && st.top() == '(') {
		    st.pop();
		} else {
		    cout << "no" << endl;
		    break;
		}
	    } else if (s[i] == ']') {
		if (st.size() > 0 && st.top() == '[') {
		    st.pop();
		} else {
		    cout << "no" << endl;
		    break;
		}
	    }
	    if (i == s.size() - 1) {
		cout << (st.size() ? "no" : "yes") << endl;
	    }
	}
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <stack>
using namespace std;

int main(){

	while(1){
        stack<char> s;
		int p=0, q=0, cont=0;
		string input;

		getline(cin, input);

		if(input[0] == '.' && input[1] == '\0') break;

		for(int i=0;i < input.length() ;i++){
			if(input[i] == '.') break;
			switch(input[i]){
				case '[': s.push(input[i]);
				break;
				case '(': s.push(input[i]);
				break;
				case ']':
                          if(s.empty()){ cont = -1; break;}
                          if(s.top() != '['){cont = -1; break;}
                          s.pop();
				break;
				case ')': q--;
                          if(s.empty()){ cont = -1; break;}
                          if(s.top() != '('){cont = -1; break;}
                          s.pop();
				break;
			}
		}
		if(cont == -1) cout << "no" << endl;
		else cout << "yes" << endl;

	}
	return 0;
}

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

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;

int main(){
  char s[128];
  while(fgets(s, sizeof(s), stdin)){
    rep(i, sizeof(s)) if(s[i] == '\n') s[i] = '\0';
    if(strcmp(s, ".") == 0) break;
    bool f = true;
    stack<char>st;
    for(int i = 0; s[i] != '\0'; ++i){
      if(s[i] == '(' || s[i] == '[') st.push(s[i]);
      else if(s[i] == ')' || s[i] == ']'){
	if(st.empty()){ f = false;break;}
	else{
	  if(s[i] == ')' && st.top() == '(') st.pop();
	  else if(s[i] == ']' && st.top() == '[') st.pop();
	  else {f = false;break;}
	}
      }
    } 
    if(f && st.empty()) puts("yes");
    else puts("no");
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

string s;

int main(){
	while(getline(cin,s)&&s!="."){
		vector<int> vi;
		bool ok=true;
		rep(i,s.size()){
			if(!ok)continue;
			if(s[i]=='['){
				vi.push_back(0);
			}
			else if(s[i]=='('){
				vi.push_back(1);
			}
			else if(s[i]==']'){
				if(vi.size()==0)ok=false;
				else if(vi.back()!=0)ok=false;
				else vi.pop_back();
			}
			else if(s[i]==')'){
				if(vi.size()==0)ok=false;
				else if(vi.back()!=1)ok=false;
				else vi.pop_back();
			}
		}
		if(ok)cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
}

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

int main(){
  vector < char > moji;
  string str;
  int i;
  while(getline(cin,str)){
    int flag = 0;
    int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
    if( str == "." ) break;
    
    int len = str.size();

    moji.clear();
      
    for( i = 0; i < len; i++ ){
      if( str[i] == '(' || str[i] == ')' || str[i] == '[' || str[i] == ']'){
	if( str[i] == '(' ) cnt1++;
	if( str[i] == ')' ) cnt2++;
	if( str[i] == '[' ) cnt3++;
	if( str[i] == ']' ) cnt4++;
	moji.push_back(str[i]);
      }
    }
   
    if( cnt1 != cnt2 ) flag = 1;
    if( cnt3 != cnt4 ) flag = 1;
    
    int len2 = moji.size();
        
    for( i = 0; i < len2-1; i++ ){
      if( moji[0] == ')' ) flag = 1;
      if( moji[0] == ']' ) flag = 1;
      
      if( moji[i] == '[' && moji[i+1] == ')' ) flag = 1;
      if( moji[i] == '(' && moji[i+1] == ']' ) flag = 1;
    }

    if( flag == 0 ) cout << "yes" << endl;
    else cout << "no" << endl;

  }
  return 0;
}