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 <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) cerr << #x << "=" << static_cast<bitset<16> >(x) << endl;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
using namespace std;
typedef long long  ll;
typedef pair<int, int> P;
const int MAX = 2*123500;

/** Problem1173 : The Balance of the World **/
int main()
{
	string s; int t;
	
	while (1) {
		getline(cin, s);
		if (s==".") break;
		
		stack<int> stak;
		bool flg=true;
		for (int i=0; i<s.length(); i++) {
			switch (s[i]) {
				case '[' :
					stak.push(1);
					break;
					
				case ']' :
					if (!stak.size()) {flg=false; break;}
					t = stak.top(); stak.pop();
					if (t!=1)
						flg=false;
					break;
					
				case '(' :
					stak.push(0);
					break;
					
				case ')' :
					if (!stak.size()) {flg=false; break;}
					t = stak.top(); stak.pop();
					if (t!=0)
						flg=false;
					break;
			}
		}
		cout << (flg&&!stak.size()?"yes":"no") << endl;
	}
	return 0;
}

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

int main()
{
  string s;
  while(getline(cin,s)){
    if(s == ".")
      break;
    stack<char> b;
    bool flag = true;
    for(int i = 0; i < s.size(); i++){
      if(s[i] == '('){
        b.push('(');
      }else if(s[i] == '['){
        b.push('[');
      }else if(s[i] == ')'){
        if(b.empty() || b.top() != '('){
          cout << "no" << endl;
          flag = false;
          break;
        }
        b.pop();
      }else if(s[i] == ']'){
        if(b.empty() || b.top() != '['){
          cout << "no" << endl;
          flag = false;
          break;
        }
        b.pop();
      }
    }
    if(b.empty() && flag)
      cout << "yes" << endl;
  }
}

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

using namespace std;

int main(void){
  string str;

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

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

    }
    cout << (flag && s.empty()?"YES":"NO") << endl;
  }
  
  return 0;
}

C++: 
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1173&lang=jp

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<numeric>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;

int LPAR, RPAR, LBRACE, RBRACE;
bool isBalance=true;
vector<char> vec; 

void init()
{
	LPAR=0, RPAR=0, LBRACE=0, RBRACE=0;
	isBalance=true;
	vec.clear();
}

void check(char word)
{				
	if(word == '('){ LPAR++; vec.push_back(word); }
	if(word == ')')
	{ 
		char tmp = vec.back();
		vec.pop_back();
		if(tmp != '('){ isBalance=false; }
		RPAR++; 
	
	}
	if(word == '['){ LBRACE++; vec.push_back(word); }
	if(word == ']')
	{
		char tmp = vec.back();
		vec.pop_back();
		if(tmp != '['){ isBalance=false; }
		RBRACE++; 
	}
}

int main() {

	string line;

	while(true)
	{
		init();
		getline(cin, line);
		//dump(line); //DEBUG
		if(line == "."){ break; }

		rep(i,line.size())
		{
			check(line[i]);
			if(RPAR > LPAR){ isBalance=false; }
			if(RBRACE > LBRACE){ isBalance=false; }
			
			if(LPAR && line[i] == ')'){ LPAR--; RPAR--;}
			if(LBRACE && line[i] == ']'){ LBRACE--; RBRACE--; }
		}

		if(!LPAR && !RPAR && !LBRACE && !RBRACE)
		{
			//Do Nothing...
		}else{
			isBalance = false;
		}

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

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (1) {
    char S[1000000];
    cin.getline(S, sizeof(S));
    if (S[0] == '.' && strlen(S) == 1) break;
    stack<int> s;
    s.push(-1);
    int flag = 0;
    for (int i = 0; i < strlen(S); i++) {
      if (S[i] == '(' || S[i] == '[') {
        s.push(S[i]);
      }
      if (S[i] == ')') {
        
        if (s.top() == '(') {
          s.pop();
        }
        else {
          flag = 1;
          break;
        }
      }
      if (S[i] == ']') {
        if (s.top() == '[') {
          s.pop();
        }
        else {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0 && s.top() == -1) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}