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 <algorithm>
#include <stack>
#include <string>

using namespace std;

int main()
{
	stack<char> s;

	while (true){
		char str[128];
		fgets(str, sizeof(str), stdin);

		if (str[0] == '.'){
			break;
		}

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

		if (flag){
			puts("Yes");
		}
		else {
			puts("No");
		}
	}

	return (0);
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>

#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>

#include <numeric>
#include <algorithm>
#include <functional>

#include <cctype>

#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(unsigned int i=0;i<(n);i++)
#define tr(it,container) for(typeof(container.begin()) it = container.begin(); \
                                                  it != container.end(); ++it)
#define mp(a,b) make_pair((a),(b))

typedef long long ll;
typedef complex<double> P;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};
const double EPS = 1e-9;
const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
const int daysleap[] = {31,29,31,30,31,30,31,31,30,31,30,31};

stringstream ss;

// ([ (([( [ ] ) ( ) (( ))] )) ])
int main(){
    string s;
    while(getline(cin,s) && s != "."){
        string ss;
        stack<char> S;
        bool ok = true;
        for(int i=0;i<s.length();i++){
            switch(s[i]){
                case '(':
                    S.push(')');
                    break;
                case '[':
                    S.push(']');
                    break;
                case ')':
                    if(!S.empty() and S.top() == ')'){
                        S.pop();
                    }else{
                        ok = false;
                    }
                    break;
                case ']':
                    if(!S.empty() and S.top() == ']'){
                        S.pop();
                    }else{
                        ok = false;
                    }
                    break;
            }
        }
        if(ok) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

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

using namespace std;

int main() {
	string a;
	const string b = "()[]";
	while (getline(cin, a)) {
		if (a.size() == 1 && a[0] == '.') {
			break;
		}
		int count[4] = {0};
		bool hantei = true;
		int kari = 0;
		for (int i = 0; i < a.size(); i++) {
			int j;
			for (j = 0; j < 4; j++) {
				if (a[i] == b[j]) {
					count[j]++;
					break;
				}
			}
			if (j == 0) {
				kari = 1;
			} else if (j == 2) {
				kari = 2;
			}
			if (kari == 2 && j == 1) {
				hantei = false;
				break;
			} else if (kari == 1 && j == 3) {
				hantei = false;
				break;
			}
			if (count[0] < count[1]) {
				hantei = false;
				break;
			} else if (count[2] < count[3]) {
				hantei = false;
				break;
			}
		}

		if (hantei && (count[0] == count[1] && count[2] == count[3])) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	}
	return 0;
}

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

bool isYes(char input[]);

int main()
{
	char Input[10000][101];
	int i=0;
	while(true){
		cin.getline(Input[i], sizeof(Input[i]));
		if(Input[i][0] == '.') break;
		i++;
	}
	for(int j=0; j<i; j++){
		if(isYes(Input[j])){
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	}
	return 0;
}

bool isYes(char Input[])
{
	int prep[50], prepnum=0, p=3;
	for(int i=0; Input[i]!='.'; i++){
		if(Input[i] == '('){
			if(p != 3){
				prep[prepnum] = p;
				prepnum++;
			}
			p=0;
		} else if(Input[i] == '['){
			if(p != 3){
				prep[prepnum] = p;
				prepnum++;
			}
			p=1;
		} else if(Input[i] == ')'){
			if(p!=0){
				return false;
			} else {
				if(prepnum==0) p=3;
				else {
					p = prep[prepnum-1];
					prepnum--;
				}
			}
		} else if(Input[i] == ']'){
			if(p!=1){
				return false;
			} else {
				if(prepnum==0) p=3;
				else {
					p = prep[prepnum-1];
					prepnum--;
				}
			}
		}
	}
	return true;
}

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

int main(){

  string str,z,x;

  while(1){

    stack < string > a;
    int count = -1,d;
    z = "0";
    a.push("0");
    d = a.size();

    getline(cin,str);

    if(str == ".") break;

    for(int i=0;i<str.size();i++){

      if((a.size() == d && str[i] == ')') || (a.size() == d && str[i] == ']')){
	count = 0;
        break;
      }

      if(str[i] == '(' || str[i] == '['){
	x = str[i];
	a.push(x);
	z = a.top();
      }

      else if((z == "(" && str[i] == ')') || (z == "[" && str[i] == ']')){
	a.pop();
	z = a.top();
      }

      else if((z == "(" && str[i] == ']') || (z == "[" && str[i] == ')')){
	count = 0;
	break;
      }

    }
    
    if(count == 0 || a.size() != d) cout << "no" << endl;
    else cout << "yes" << endl;
  }
}

C++: #include <stdio.h>
int main(){
  int i,ib;
  char a,s[100];
  while(1){
    i=0;
    scanf("%c",&a);
    if(a=='.')break;
    else if(a=='('){
      s[i]='(';
      i++;
    }else if(a==')'){
      s[i]=')';
      i++;
    }else if(a=='['){
      s[i]='[';
      i++;
    }else if(a==']'){
      s[i]=']';
      i++;
    }
    while(1){
      scanf("%c",&a);
      if(a=='.'){
	if(i==0){
	  printf("Yes\n");
	  break;
	}else {
	  printf("No\n");
	  break;
	}
      }else if(a=='('){
	s[i]='(';
	i++;
      }else if(a==')'){
	if(s[i-1]=='(')i--;
	else {
	  s[i]=')';
	  i++;
	}
      }else if(a=='['){
	s[i]='[';
	i++;
      }else if(a==']'){
	if(s[i-1]=='[')i--;
	else {
	  s[i]=']';
	  i++;
	}
      }
    }
    scanf("%c",&a);
  }
  return 0;
}

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

int main(){
  string s;
  while(1){
    getline(cin,s);
    if(s.size()==1&&s[0]=='.')break;
    string tmp;
    bool ok=true;
    for(int i=0;i<s.size();i++){
      if(s[i]=='(')tmp.push_back('(');
      if(s[i]=='[')tmp.push_back('[');

      if(s[i]==')'&&tmp[tmp.size()-1]=='('&&tmp.size()!=0){
	tmp.pop_back();
      }else if(s[i]==')'&&tmp[tmp.size()-1]=='['){
	ok=false;
	break;
      }

      if(s[i]==']'&&tmp[tmp.size()-1]=='['&&tmp.size()!=0){
	tmp.pop_back();
      }else if(s[i]==']'&&tmp[tmp.size()-1]=='('){
	ok=false;
	break;
      }
    }
    if(ok&&tmp.size()==0)cout<<"yes"<<endl;
    else cout<<"no"<<endl;
    
  }
  return 0;
}