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(void){
  string s;
  stack<char> str;

  while(getline(cin,s) && s!="."){

    string ch;
    for(int i=0;i<s.length;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]);
      
      if(ch[i]==')' && str.top()=='(')str.pop();
      if(ch[i]=='}' && str.top()=='}')str.pop();
    }

    cout << str.empty()?"YES":"NO" << endl;

  }

  return 0;
}

C++: /* The Balance of the World */
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;


int main(void){
  string s;
  int k,l,i;
  stack<char> st;
  int flag;
  while(getline(cin,s)){
    if(s.length()==1&&s[0]=='.') break;
    while(!st.empty()) st.pop();
    flag=0;
    for(i=0;i<s.length();i++){
      if(s[i]=='('||s[i]=='['){
	st.push(s[i]);
      }
      if(s[i]==')'||s[i]==']'){
	if(st.empty()) flag=1;
	if(!st.empty()){
	  if(s[i]==')'){
	    if('('!=st.top()) flag=1;
	  }
	  if(s[i]==']'){
	    if('['!=st.top()) flag=1;
	  }
	  st.pop();
	}
      }
    }
    if(flag) cout<<"no"<<endl;
    else cout<<"yes"<<endl;
  }
  return 0;
}

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

int main(){
  char w;
  int c[2];  // (  ) [ ]

  while(1){
    cin>>w;
    
    for(int i=0;i<2;i++){
      c[i]=0;    
    }
    while(1){
      if(w=='.') break;

      switch(w){
      case '(':
	c[0]++;
	break;
	
      case ')':
	c[0]--;	
	break;
	
      case '{':
	c[1]++;
      break;
      
      case '}':
	c[1]--;
	break;
      }
      if(c[1]<0||c[0]<0){ c[0]=1;break;}
      scanf("%c",&w);
     
    }
    if((c[0]==0) && (c[1]==0) ) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  } 
  
  return 0;
}

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

using namespace std;

int main(){
  char match[256];
  string str;

  match['('] = ')';
  match['['] = ']';

  while(getline(cin, str), str != "."){
    stack<char> br;
    bool ans = true;

    for(int i = 0; ans && i < str.size(); i++){
      switch( str[i] ){
      case '(':
      case '[':
        br.push(str[i]);
        break;
      case ')':
      case ']':
        if(br.empty()){
          ans = false;
        }else{
          char c = br.top(); br.pop();
          if(match[c] == str[i]){
          }else{
            ans = false;
          }
        }
        break;
      default:
        break;
      }
    }

    ans = ans && br.empty();

    puts(ans ? "yes" : "no");
  }

  return 0;
}

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

struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* cpp template {{{ */

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) (v).rbegin(), (v).rend()
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) REPS(i, 0, n)
#define REP1(i,n) for (ll i = 1; i <= (ll)(n); ++i)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto&& i : n)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; cin >> x; return x;}
inline ll IN() {ll x; cin >> x; return x;}
inline vs split(const string& t, char c) {vs v; stringstream s(t); string b; while(getline(s, b, c)) v.eb(b); return v;}
template <typename T> inline bool chmin(T& a, const T& b) {if (a > b) a = b; return a > b;}
template <typename T> inline bool chmax(T& a, const T& b) {if (a < b) a = b; return a < b;}
template <typename T, typename S> inline void print(const pair<T, S>& p) {cout << p.first << " " << p.second << endl;}
template <typename T> inline void print(const T& x) {cout << x << '\n';}
template <typename T, typename S> inline void print(const vector<pair<T, S>>& v) {for (auto&& p : v) print(p);}
template <typename T> inline void print(const vector<T>& v, string s = " ") {rep(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");}

signed main() 
{
  string s;
  while(getline(cin,s),s!=".")
  {
    stack<char> t;
    bool judge=true;
    rep(i,s.size())
    {
      if(s[i]=='('||s[i]=='[') t.push(s[i]);
      else if((s[i]==')'||s[i]==']'))
      {
        if(t.empty())
        {
          judge=false;
          break;
        }
        else if(s[i]==')'&&t.top()=='(') t.pop();
        else if(s[i]==']'&&t.top()=='[') t.pop();
      } 
    }
    if(!t.empty()) judge=false;
    if(judge) print("yes");
    else print("no");

  }

  return 0;
}

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

using namespace std;

int main(){
	int maru=0,kaku=0;

	string str;
	while(getline(cin,str)){
		if(str.at(0)=='.')break;
		maru=0;
		kaku=0;
		int flag=0;
		stack<int> s;

		for(int i=0;i<str.size();i++){
			if(str.at(i)=='('){
				maru++;
				s.push(1);
			}
			else if(str.at(i)==')' && maru>0){
				if(s.top()==1){
					maru--;
					s.pop();
				}
				else{
					cout<<"no"<<endl;
					flag=1;
				}
			}
			if(str.at(i)=='['){
				kaku++;
				s.push(2);
			}
			else if(str.at(i) ==']' &&kaku>0){
				if(s.top()==2){
					kaku--;
					s.pop();
				}
				else{
					cout<<"no"<<endl;
					flag=1;
				}
			}
			if(flag==1)break;
		}

		if(maru==0&&kaku==0&&flag==0){
			cout<<"yes"<<endl;
		}
		else{
			cout<<"no"<<endl;
		}
	}

	return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int h,i;
  int ln;
  char c[256];
  string s;
  for(;;){
    fgets(c,256,stdin);
    s=c;
    ln=s.length();
    if(ln==2)
      break;
    for(i=0;i<ln;i++){
      if(s[i]!='('&&s[i]!=')'&&s[i]!='['&&s[i]!=']'){
	s.erase(i,1);
	ln--;
	i--;
      }
    }
    do{
      h=0;
      for(i=1;i<ln;i++){
	if(0){
	}else if(s[i-1]=='('&&s[i]==')'){
	  s.erase(i-1,2);
	  ln-=2;
	  h=1;
	  break;
	}else if(s[i-1]=='['&&s[i]==']'){
	  s.erase(i-1,2);
	  ln-=2;
	  h=1;
	  break;
	}
      }
    }while(h);
    if(ln)
      cout<<"no"<<endl;
    else
      cout<<"yes"<<endl;
  }
  return 0;
}