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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <cassert>
// using namespace std;
// #define int long long
#define REP(i,s,n) for (int i = (s); i < (n); ++i)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) for (int i = n; i >= 0; i--)
#define INF (int)(1e9+7)
#define space " "
#define ENDL "\n"
#define MAXL 100

int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};

inline void init() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
}
// ??¢?????¬??????????????????????????????????????????!!
inline void nextInt(int *a) { scanf("%d", a); }
inline void nextInt(int *a, int *b) { scanf("%d %d", a, b); }
inline void nextInt(int *a, int *b, int *c) { scanf("%d %d %d", a, b, c); }
inline void nextDouble(double *a) { scanf("%lf", a); }
inline void nextDouble(double *a, double *b) { scanf("%lf %lf", a, b); }
inline void nextDouble(double *a, double *b, double *c) { scanf("%lf %lf %lf", a, b, c); }
inline void nextLine(char *c) { scanf("%s", c); getchar();/*?????????????????¨???*/ }
inline std::string nextLine() {
    std::string s = ""; char str[MAXL+1];
    scanf("%s", str); getchar(); /*?????????????????¨???*/
    return s += str;
}
inline void print(double *a, int n = 0) {
    std::cout << std::fixed << std::setprecision(n) << *a << ENDL;
}
inline void print(int a) {
    printf("%d\n", a);
}

/************************End of template*************************/

int main() {
    std::string s;
    bool flag = true;
    std::stack<char> st, ini;

    init();

    while (true) {
        s = "";
        st = ini;
        flag = true;
        std::getline(std::cin, s);
        //std::cout << "[" << s << "]" << ENDL;
        if (s[0] == '.') break;

        rep(i,s.size()) {
            if (s[i] == '(' || s[i] == '[') {
                st.push(s[i]);
            } else if (s[i] == ')') {
                if (st.empty()) {
                    flag = false;
                    break;
                } else if (st.top() == '(') {
                    st.pop();
                } else {
                    flag = false;
                    break;
                }
            } else if (s[i] == ']') {
                if (st.empty()) {
                    flag = false;
                    break;
                } else if (st.top() == '[') {
                    st.pop();
                } else {
                    flag = false;
                    break;
                }
            }
        }

        if (flag) std::cout << "yes" << ENDL;
        else std::cout << "no" << ENDL;
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
  int l, f;
  string S;
  stack<int> T;
  while (getline(cin, S), S != ".") {
    l = S.size();
    f = 1;
    for (int i = 0; i < l; ++i) {
      if (S[i] ==  '(' || S[i] == '[') T.push(S[i]);
      else if (S[i] == ')') {
	if (T.empty() || T.top() != '(') {
	  cout << "no" << endl;
	  f = 0;
	  break;
	}
	T.pop();
      }else if (S[i] == ']') {
	if (T.empty() || T.top() != '[') {
	  cout << "no" << endl;
	  f = 0;
	  break;
	}
	T.pop();
      }
    }
    if (f) {
      if (T.empty()) {
	cout << "yes" << endl;
      } else {
	cout << "no" << endl;
      }
    }
    while (!T.empty()) T.pop();
  }
  return 0;
}