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 <vector>
#include <string>
#include <cstring>
#include <math.h>
#include <cmath>
#include <limits.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <functional>
#include <stack>
#include <stdio.h>
#include <ios>     // std::left, std::right
#include <iomanip>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;i++)

const int inf = 1001001000;
//const int mod = 1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int digitlen(long long i){
    long long t = i;
    int count = 0;
    while(t){
        count += 1;
        t /= 10;
    }
    return count;
}

vector<int> cumsum(vector<int> arr){
    vector<int> ret;
    int l = arr.size();
    ret.push_back(arr[0]);
    for(int i = 1; i < l; i++){
        ret.push_back(ret[i-1] + arr[i]);
    }
    return ret;
}

vector<int> eratosthenes(int n){
    int arr[n+1];
    rep(i,0,n+1){
        arr[i] = 1;
    }
    arr[0] = 0; arr[1] = 0;
    for(int i = 2; i < n; i++){
        if(arr[i]==1){
            for(int j = 2*i; j < n+1; j+=i){
                arr[j] = 0;
            }
        }
    }
    
    vector<int> ret;
    for(int i = 2; i<n+1; i++){
        if(arr[i] == 1){
            ret.push_back(i);
        }
    }
    return ret;

}


int main(){
    while(true){
        string s;
        getline(cin, s);
       
        if(s.size() == 1 && s[0] == '.') return 0;

        stack<char> stk;

        bool ischecked = false;

        for(auto e: s){
            if(e == '(' || e == '['){
                stk.push(e);
            }

            if(e == ')'){
                if(stk.size() < 1 || stk.top() != '('){
                    cout << "no" << endl;
                    ischecked = true;
                    break;
                }else{
                    stk.pop();
                }
            }

            if(e == ']'){
                if(stk.size() < 1 || stk.top() != '['){
                    cout << "no" << endl;
                    ischecked = true;
                    break;
                }else{
                    stk.pop();
                }

            }
        }
        if(!ischecked){
            if(stk.size() == 0){
                cout << "yes" << endl;
            }else{
                cout << "no" << endl;
            }
        }

    }
}

C++: import java.util.*;

class Main{
	final static char[][] brackets = {{'(', ')'}, {'[', ']'}};
	
	Stack<Character> s;
	String input;
	int flag;	//????????????????????????????????°???
	
	Main(){
		s = new Stack<Character>();
		input = null;
		flag = 0;
	}
	
	public static void main(String[] args){
		while( new Main().Main() );
		return;
	}
	
	//???????????¶??????????????????false????????????
	boolean Main(){
		Scanner scan = new Scanner(System.in);
		try{
			input = scan.nextLine();
		}catch( NoSuchElementException nsee ){
			System.out.println("Error");
			return false;
		}
		//???????????¶?????§?????????
		if( input.length() == 1 ){ return false; }
		for( int i = 0; i < input.length(); i++ ){
			for( int j = 0; j < brackets.length; j++ ){
				if( input.charAt(i) == brackets[j][0] ){
					s.push(brackets[j][0]);
					break;
				}else if( input.charAt(i) == brackets[j][1] ){
					try{
						if( s.pop() != brackets[j][0] ){
							System.out.println("no");
							return true;
						}
					}catch( EmptyStackException ese ){
						//??????????????¬??§???????????????????????¬??§????????¨????????????
						System.out.println("no");
						return true;
					}
					break;
				}
			}
		}
		if( s.isEmpty() ){
			System.out.println("yes");
		}else{
			System.out.println("no");
		}
		return true;
	}
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

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

int main(){
    while(1){
        string s;
        getline(cin, s);
        if(s == ".") return 0;

        stack<char> st;
        int n = s.size();

        bool valid = true;
        for(int i=0; i<n; i++){
            if(s[i] == '(' || s[i] == '['){
                st.emplace(s[i]);
            }

            if(s[i] == ')'){
                if(st.size() && st.top() == '('){
                    st.pop();
                }
                else{
                    valid = false;
                }
            }
            else if(s[i] == ']'){
                if(st.size() && st.top() == '['){
                    st.pop();
                }
                else{
                    valid = false;
                }
            }
        }

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

C++: /*
  The Balance of the World
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1173&lang=jp
*/

#include <iostream>
#include <string>
#include <vector>

using namespace std;


int main(void){
  while(1){
    string s;
    vector<char> v;
    getline(cin, s);
    if(s[0] == '.') break;

    int ptr = 0;
    bool correct = true;
    while(correct && ptr < s.length()){
      switch( s[ptr] ){
        case '(' :
          v.push_back('(');
          ptr++;
          break;
        case '[' :
          v.push_back('[');
          ptr++;
          break;
        case ')' :
          if( !v.empty() && v.back() == '('){
            v.pop_back();
            ptr++;
          }else{
            correct = false;
          }
          break;
        case ']' :
          if( !v.empty() && v.back() == '['){
            v.pop_back();
            ptr++;
          }else{
            correct = false;
          }
          break;
        default:
          ptr++;
          break;
      }
    }

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

  return 0;
}