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<bits/stdc++.h>
using namespace std;

bool isok(int f,int e,const string& s){
    if(f>=e)
        return true;
    int x=-1;
    for(int i=f;i<e;i++){
        if(s[i]=='(' || s[i]=='['){
            x=i;
            break;
        }
        if(s[i]==')' || s[i]==']')
            return false;

    }
    if(x==-1)
        return true;

    char rig;
    if(s[x]=='(')
        rig=')';
    else
        rig=']';
    int y=-1;
    int cnl=1;int cnr=0;
    for(int i=x+1;i<e;i++){
        if(s[i]==rig)cnr++;
        if(s[i]==s[x])cnl++;
        if(cnr==cnl){
            y=i;
            break;
        }


    }
    if(y==-1)
        return false;
    return isok(x+1,y,s) && isok(y+1,e,s);
}
int main(){
    string s;
    while(getline(cin,s),s!="."){
        string res=(isok(0,s.size(),s) ? "yes":"no");
        cout<<res<<endl;

    }
    return 0;
}

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

string str;

bool is_balance(){

  stack<char> s;

  for(int i=0 ; str[i] != '.' ; i++){
    if(str[i] == '(' || str[i] == '['){
      s.push(str[i]);
    }else if(str[i] == ')'){
      if(s.empty() || s.top() != '('){
	return false;
      }
      s.pop();
    }else if(str[i] == ']'){
      if(s.empty() || s.top() != '['){
	return false;
      }
      s.pop();
    }
  }

  if(s.empty()){
    return true;
  }

  return false;
  
}
int main(){

  while(1){

    getline(cin,str);

    if(str == ".")break;
    
    if(is_balance()){
      cout <<"yes"<<endl;
    }else{
      cout <<"no"<<endl;
    }
    
  }


  return 0;
}

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

string s;
int sum1,sum2;

int Search2(int indx);


int Search1(int indx){


  while(indx < (int)s.size()){

    if(s[indx]==']'){
      return -1;
    }

    else if(s[indx]=='('){
      indx = Search1(indx+1);
      sum1++;
      if(indx==-1 ) return -1;
    }

    else if(s[indx]=='['){
      indx = Search2(indx+1);
      sum2++;
      if(indx == -1) return -1;
    }

    else if(s[indx]==')'){
      sum1--;
      return indx;
    }


    indx++;

  }

  return indx;

}

int Search2(int indx){

  while(indx < (int)s.size() ){
    
    if(s[indx]==')'){
      return -1;
    }
    
    else if(s[indx]=='['){
      indx = Search2(indx+1);
      sum2++;
      if(indx==-1) return -1;
    }

    else if(s[indx]=='('){
      indx = Search1(indx+1);
      sum1++;
      if(indx==-1) return -1;
    }

    else if(s[indx] == ']'){
      sum2--;
      return indx;
    }

    indx++;
  }

  return indx;
}





int main(){

  while(getline(cin,s)){

    if(s=="."){ break; }

    sum1= sum2= 0;

    if( Search1(0) == -1 || (sum1!=0 || sum2!=0) )
      cout << "no" << endl;
    else
      cout << "yes" << 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 == ')')
	{
		if(vec.empty()){
			isBalance=false;
		}else{
			char tmp = vec.back();
			vec.pop_back();
			if(tmp != '('){ isBalance=false; }
			RPAR++;
		}

	}
	if(word == '['){ LBRACE++; vec.push_back(word); }
	if(word == ']')
	{
		if(vec.empty()){
			isBalance=false;
		}else{
			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 <iostream>
#include <stack>
using namespace std;
int main()
{
  for(;;){
    string s;
    getline(cin,s);
    if(s == ".") break;
    stack<char> q;
    bool balanced = true;
    for(int i=0; i<s.length(); i++){
      switch(s[i]){
      case '(':
      case '[':
        q.push(s[i]);
        break;
      case ')':
        if(q.empty() || q.top() != '('){
          balanced = false;
          break;
        }else{
          q.pop();
        }
        break;
      case ']':
        if(q.empty() || q.top() != '['){
          balanced = false;
          break;
        }else{
          q.pop();
        }
        break;
      }
    }
    if(balanced && q.empty()) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}

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

int main(){
  
  string str;

  while(1){

    getline(cin, str);
    if(str[0] == '.') break;

    vector<int> v;

    for(int i = 0; i < (int)str.size(); i++){
      if(str[i] == '('){
	v.push_back(0);
      }else if(str[i] == ')'){
	v.push_back(1);
      }else if(str[i] == '['){
	v.push_back(2);
      }else if(str[i] == ']'){
	v.push_back(3);
      }
    }

    int cnt = 0, judge = 0, o[2] = {}, p[2] = {};

    for(int i = 0; i < (int)v.size(); i++){
      if(v[i] == 0){
	for(int j = i+1; j < (int)v.size(); j++){
	  if(v[j] == 1){
	    for(int l = i+1; l < j; l++){
	      if(v[l] == 0) o[0]++;
	      if(v[l] == 1) o[1]++;
	      if(v[l] == 2) p[0]++;
	      if(v[l] == 3) p[1]++;
	      //   cout << o[0] << o[1] << p[0] << p[1] << endl;
	    }
	    
	    if(o[0] == o[1] && p[0] == p[1]){
	      v[i] = 7;
	      v[j] = 7; 
	      o[0] = o[1] = p[0] = p[1] = 0;
	    }
	    o[0] = o[1] = p[0] = p[1] = 0;
	  }
	}
      }else if(v[i] == 2){
	for(int j = i+1; j < (int)v.size(); j++){
	  if(v[j] == 3){
	    for(int l = i+1; l < j; l++){
	      if(v[l] == 0) o[0]++;
	      if(v[l] == 1) o[1]++;
	      if(v[l] == 2) p[0]++;
	      if(v[l] == 3) p[1]++;
	    }
	    
	  }
	  if(o[0] == o[1] && p[0] == p[1]){
	    v[i] = 7;
	    v[j] = 7;
	  }
	  o[0] = o[1] = p[0] = p[1] = 0;
	}
      }
      /*   for(int k = 0; k < (int)v.size(); k++){
	cout << v[k];
      }
      cout << endl;
      */
      }
     
    
    for(int i = 0; i < (int)v.size(); i++){
      if(v[i] == 7){
    	cnt++; 
      }
    }

    if(cnt == (int)v.size()){
      cout << "yes" << endl;
      cnt = 0;
    }else if(cnt != (int)v.size()) cout << "No" << endl;
    // cout << cnt << " " << (int)v.size() << endl;
  }
  
  return 0;
}