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 <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>

using namespace std;

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;

const int INF = INT_MAX / 4;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); i++ )
#define UREP( i, m, n ) for ( ui i = (ui)( m ); i < (ui)( n ); i++ )
#define EACH( v, c ) for ( __typeof( c.front() ) &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define RALL( c ) (c).rbegin(), (c).rend()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

int main()
{
	while ( true )
	{
		string line;
		getline( cin, line );

		if ( line == "." )
		{
			return 0;
		}

		bool yes = true;
		char c;
		stack<char> st;

		for ( char &c : line )
		{
			if ( c == '(' || c == '[' )
			{
				st.push( c );
			}
			else if ( c == ')' || c == ']' )
			{
				int c2 = ( c == ')' ? '(' : '[' );

				if ( !st.empty() && st.top() == c2 )
				{
					st.pop();
				}
				else
				{
					yes = false;
				}
			}
		}

		if ( yes && st.empty() )
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
	}				

	return 0;
}

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

using namespace std;

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

  while(getline(cin,s) && s!="."){
    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(ch[i]=='(' || ch[i]=='{')str.push(s[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;
}

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

bool balance(string s, int b, int e)
{
  for(int i = b; i < e; i++){
    if(s.at(i) == '('){
      for(int j = i + 1; j < e; j++){
	if(s.at(j) == ')'){
	  string sub = s.substr(i + 1, j - i - 1);
	  if(balance(sub, 0, sub.size())){
	    i = 0;
	    continue;
	  }
	}
      }
      if(i)
	return false;
      else
	return true;
    }else if(s.at(i) == '['){
      for(int j = i + 1; j < e; j++){
	if(s.at(j) == ']'){
	  string sub = s.substr(i + 1, j - i - 1);
      	  if(balance(sub, 0, sub.size())){
	    i = 0;
	    continue;
	  }
	}
      }
      if(i)
	return false;
      else
	return true;
    }else if(s.at(i) == ')'){
      for(int j = 0; j < i; j++){
	if(s.at(j) == '('){
	  string sub = s.substr(j, i - j);
	  if(balance(sub, 0, sub.size())){
	    i = 0;
	    continue;
	  }
	}
      }
      if(i)
	return false;
      else
	return true;
    }else if(s.at(i) == ']'){
      for(int j = 0; j < i; j++){
	if(s.at(j) == '['){
	  string sub = s.substr(j, i - j);
	  if(balance(sub, 0, sub.size())){
	    i = 0;
	    continue;
	  }
	}
      }
      if(i)
	return false;
      else
	return true;
    }
  }
  return true;
}
       
int main()
{
  string s;
  while(getline(cin, s)){
    if(balance(s, 0, s.size()))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=300003,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        string S;
        getline(cin,S);
        stack<char> ST;
        bool ok=true;
        if(S.size()==1&&S[0]=='.'){
            //cout<<"yes"<<endl;
            break;
        }
        
        for(int i=0;i<S.size();i++){
            if(S[i]=='('||S[i]=='['){
                ST.push(S[i]);
            }else if(S[i]==')'){
                if(ST.size()>=1&&ST.top()=='('){
                    ST.pop();
                }else ok=false;
            }else if(S[i]==']'){
                if(ST.size()>=1&&ST.top()=='['){
                    ST.pop();
                }else ok=false;
            }
        }
        
        if(ok&&ST.empty()) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
}

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

using namespace std;
string s;
int main() {
G:
	while (getline(cin, s) && s != ".") {
		stack<char> p;
		for (int i = 0;i < s.length();i++) {
			if (s[i] == '(' || s[i] == '[')p.push(s[i]);
			if (s[i] == ')') { if (p.empty() || p.top() != '(') { cout << "no" << endl;goto G; }p.pop(); }
			if (s[i] == ']') { if (p.empty() || p.top() != '[') { cout << "no" << endl;goto G; }p.pop(); }
		}
		cout << (p.empty() ? "yes" : "no") << endl;
	}
	return 0;
}

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

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;


int main() {
	string str;
	stack<int> st;

	while(getline(cin, str) && str != ".") {
		st.push(2);

		FOR(i, 0, str.size()) {
			char c = str[i];

		if(c == '(') {
			st.push(0);
		} else if(c == '[') {
			st.push(1);
		} else if(c == ')') {
			if(st.top() != 0) {
				cout << "no" << endl;
			}
			st.pop();
		} else if(c == ']') {
			if(st.top() != 1) {
				cout << "no" << endl;
		}
			st.pop();
			}
		}

		if(st.top() == 2) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}

	}
}