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<algorithm>

using namespace std;

int main(){
  int i, isCorrect;
  string str, _str;

  while(1){
    getline(cin, str);
    if(str == ".") break;

    for(i=0, isCorrect=1; i<str.size(); ++i){
      if(str[i] == '(' || str[i] == '['){
	_str.push_back(str[i]);
      }else if(str[i] == ')'){
	if(_str.size() == 0 || _str[_str.size()-1] != '(')
	  isCorrect = 0;
	else
	  _str.erase(_str.end()-1);
      }else if(str[i] == ']'){
	if(_str.size() == 0 || _str[_str.size()-1] != '[')
	  isCorrect = 0;
	else
	  _str.erase(_str.end()-1);
      }

      if(isCorrect == 0) break;
    }

    if(isCorrect == 1 && _str.size() == 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;

    str.clear();
    _str.clear();
  }

  return 0;
}

C++: #include <iostream>
using namespace std;
bool r(int &i,string S);
bool a(int &i,string S);
bool r(int &i,string S){
    while(i<S.size()){
        switch(S[i]){
            case '(':
                i++;
                if(!r(i,S)) return false;
                break;
            case ')':
                i++;
                return true;
            case '[':
                i++;
                if(!a(i,S)) return false;
                break;
            case ']':
                i++;
                return false;
            default:
                i++;
                break;
        }
    }
    return false;
}

bool a(int &i,string S){
    while(i<S.size()){
        switch(S[i]){
            case '(':
                i++;
                if(!r(i,S)) return false;
                break;
            case ')':
                i++;
                return false;
            case '[':
                i++;
                if(!a(i,S)) return false;
                break;
            case ']':
                i++;
                return true;
            default:
                i++;
                break;
        }
    }
    return false;
}

int main(){
    string str;
    while(true){
        getline(cin,str);
        if(str==".") return 0;
        else str.push_back(']');
        int i=0;
        if(a(i,str)){
            cout<<"yes"<<endl;
        }else{
            cout<<"no"<<endl;
        }
    }
    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 expect(string,int,char);
bool solve(string s){
    for(int i=0;i<s.length();i++){
        if(s[i] == '('){
            return expect(s,i+1,')')!=-1;
        }else if(s[i] == '['){
            return expect(s,i+1,']')!=-1;
        }else{
            return false;
        }
    }
    return true;
}

int expect(string s,int p,char c){
    while(s[p] == '(' or s[p] == '['){
        if(s[p]=='(') p = expect(s,p+1,')');
        else p = expect(s,p+1,']');
        if(p == -1) return -1;
        p++;
    }
    if(s[p] == c){
        return p;
    }else{
        return -1;
    }
}

// ([ (([( [ ] ) ( ) (( ))] )) ])
int main(){
    string s;
    while(getline(cin,s) && s != "."){
        string ss;
        for(int i=0;i<s.length();i++){
            if(s[i] == '(' or s[i] == ')' or
               s[i] == '[' or s[i] == ']') ss += s[i];
        }
        if(solve(ss)) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

C++: #include<iostream>  
#include<cstdio>  
#include<cstring>  
using namespace std;  
void test(int );  
int f,count,count1;  
char ch[101];   
int main(){  
  while(1){  
    int j=0;  
    f=count=count1=0; 
    while(1){  
      scanf("%c",&ch[j]);  
      if(ch[j]=='.')break;  
      j++;   
    }
    if(j==0)break;
    for(int i=0;i<j;i++){  
      if(ch[i]=='[')count++;  
      else if(ch[i]=='(')count1++;  
      else if(ch[i]==']' || ch[i]==')')test(i);  
      if(count1<0||count<0||f==1)break;   
    }  
    if(count==0&&count1==0)cout<<"yes"<<endl;  
    else cout<<"no"<<endl;    
    char a; 
    scanf("%c",&a);  
  }  
  return 0;  
}

void test(int x){  
  f=0;  
  int count2=0;  
  if(ch[x]==')'){  
    count1--;  
    for(int i=x-1;i>=0;i--)  
      if(ch[i]=='('){  
	for(int j=x-1;j>i;j--)  
	  if(ch[j]=='[')count2++;  
	  else if(ch[j]==']')count2--;  
	break;  
      }  
  }  
  else if(ch[x]==']'){  
    count--;  
    for(int i=x-1;i>=0;i--)  
      if(ch[i]=='['){  
	for(int j=x-1;j>i;j--)  
	  if(ch[j]=='(')count2++;  
	  else if(ch[j]==')')count2--;  
	break;  
      }  
  }  
  if(count2>0)f=1;  
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(){
	string s;
	while(getline(cin,s)){
		if(s==".") break;
		int a[200][200],k1=0,k2=0,b=0,c1=0,c2=0,c3=0,c4=0;
		for(int i=0;i<200;i++){
			for(int j=0;j<200;j++){
				a[i][j]=0;
			}
		}
		a[100][100]=1;
		int len=s.size();
		for(int i=0;i<len;i++){
			if(s[i]=='('){
				k1++;
				a[100+k1][100+k2]++;
				c1++;
			}
			else if(s[i]==')'){
				a[100+k1][100+k2]=0;
				k1--;
				if(a[100+k1][100+k2]==0){
					b++;
				}
				c2++;
			}
			else if(s[i]=='['){
				k2++;
				a[100+k1][100+k2]++;
				c3++;
			}
			else if(s[i]==']'){
				a[100+k1][100+k2]=0;
				k2--;
				if(a[100+k1][100+k2]==0){
					b++;
				}
				c4++;
			}
		}
		if(b==0&&c1==c2&&c3==c4){
			cout<<"yes"<<endl;
		}
		else{
			cout<<"no"<<endl;
		}
		string s(200,'a');
	}
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP((i), 0, (n))
#define REP(i, a, n) for(int i=(a); i<(n); i++)

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

        stack<char> st;
        bool flag = true;
        for(char c : s) {
            if(c == '(' || c == '[') {
                st.push((c=='('? ')':']'));
            } else if(c == ')' || c == ']') {
                if(st.empty() || st.top() != c) {
                    flag = false;
                    break;
                }
                st.pop();
            }
        }
        
        cout << (flag? "yes":"no") << endl;
    }
}

C++: #include<stdio.h>
#include<string.h>
#include<stack>
int main()
{
	char*p,s[128];
	while(fgets(s,128,stdin),*s-'.')
	{
		std::stack<int>c;
		for(p=s;*p;++p)
		{
			if(strchr("[(",*p))c.push(*p);
			if(strchr("])",*p))if(c.empty()||c.top()!=*p-(*p-')'?2:1)){puts("no");goto E;}
			else c.pop();
		}
		puts("yes");
E:;
	}
	return 0;
}