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>
#include <vector>
#include <stack>
#include <queue>
#include <cstdio>
#define F(i,n) for(int i=0;i<(int)(n);i++)
#define INF 100000000
#define ll long long

using namespace std;
typedef pair<int, int> P;

int main() {
	string a;
	int ans,i,j,p=0;
	stack<int> s;
	while (1) {
		ans = 0;
		i = 0;
		while (s.size()!=0) {
			s.pop();
		}
		getline(cin, a);
		F(j,n) {
			if (i == 0 && a[j] == '.') {
				p = 1;
				break;
			}
			if (a[j] == '.') {
				if (s.size()!=0 ){
					ans = 1;
					break;
				}
				break;
			}
			if (a[j] == '(')s.push(1);
			if (a[j] == '[')s.push(2);
			if (a[j] == ')') {
				if (s.size() == 0) {
					ans = 1;
					break;
				}
				if (s.top() == 1) {
					s.pop();
				}
				else {
					ans = 1;
				}
			}
			if (a[j] == ']') {
				if (s.size() == 0) {
					ans = 1;
					break;
				}
				if (s.top() == 2) {
					s.pop();
				}
				else {
					ans = 1;
					break;
				}
			}
			i = 1;

		}
		if (p == 1)break;
		if (ans == 0)cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

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

using namespace std;

int check(char c){
  if(c == '[' || c == '(') return 2;
  if(c == ']' || c == ')') return 1;
  return 0;
}

int isOK(char c, char in){
  if(in == ']' && c == '[') return 1;
  if(in == ')' && c == '(') return 1;
  // cout << " " << c << " " << in << endl;
  return 0;
}

int main(){

  string in;
  while(getline(cin,in)){
    if(in == ".") break;
    stack<char> S;
    int status = 1;
    for(int i = 0; in[i] != '.'; i++){
      int j = check(in[i]);
      if(j == 2){
	S.push(in[i]);
	//	cout << " " << in[i] << endl;
      }
      if(j == 1) {
	if(S.size() == 0){
	  status = 0;
	  break;
	}
	if(!isOK(S.top(),in[i])){
	  status = 0;
	  break;
	}
	S.pop();
      }
    }
    if(status) cout << "yes" << endl;
    else cout << "no" << endl;
  }
  return 0;
}

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

std::string str;

bool calc(int p){
	for(int i=p+1;i<str.size();i++){
		if(str[i]=='('){
			if(calc(i))return true;
			else return false;
		}
		if(str[i]==')'){
			if(str[p]=='(')return true;
			else return false;
		}
		if(str[i]=='['){
			if(calc(i))return true;
			else return false;
		}
		if(str[i]==']'){
			if(str[p]=='[')return true;
			else return false;
		}
	}
	return false;
}

int main(){
	while(1){
		int l=0,l2=0;
		std::getline(std::cin,str);
		if(str==".")break;
		for(int i=0;i<str.size();i++){
			if(str[i]=='(')l++;
			if(str[i]==')')l--;
			if(str[i]=='[')l2++;
			if(str[i]==']')l2--;
			if(l<0||l2<0){
				std::cout<<"no"<<std::endl;
				goto end;
			}
			if(str[i]=='('||str[i]=='['){
				if(!calc(i)){
					std::cout<<"no"<<std::endl;
					goto end;
				}
			}
		}
		if(l==0&&l2==0)std::cout<<"yes"<<std::endl;
		else std::cout<<"no"<<std::endl;
		end:;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int cnt1,cnt2;
string s;
int main(){
	while(1){
		getline(cin,s);
		if(s[0]=='.')break;
		cnt1=cnt2=0;
		for(int i=0;i<s.size();i++){
			if(s[i]=='('){
				cnt1++;
			}else if(s[i]==')'){
				cnt1--;
			}else if(s[i]=='['){
				cnt2++;
			}else if(s[i]==']'){
				cnt2--;
			}
			if(cnt1<0||cnt2<0)break;
		}
		if(cnt1==0&&cnt2==0){
			cout <<"yes" <<endl;
		}else{
			cout <<"no" <<endl;
		}
	}
}

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

int main(){
	string str;
	while(getline(cin,str) && str != "."){
		string c;
		for(int i = 0 ; i < str.size() ; i++)
			if(str[i] == ')' || str[i] == '(' || str[i] == '[' || str[i] == ']')
				c += str[i];
		stack<char> s;
		bool f = true;
		for(int i = 0 ; i < c.length() ; i++){
			if(s.empty() || c[i] == '(' || c[i] == '[')
				s.push(c[i]);
			else if( (s.top() == '(' && c[i] == ')') || (s.top() == '[' && c[i] == ']') ){
				s.pop();
			}else{
				f = false;
				break;
			}
		}
		cout << (f && s.empty() ?"yes":"no") << endl;
	}
}

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

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::stack;

int main(void) {
    string s; 
    stack<char> st; 
    char comp;
    bool check;

    while (getline(cin, s)) {
        if (s[0] == '.') {
            break;
        }
        check = true;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '(' || s[i] == '[') {
                st.push(s[i]); 
            } else if (s[i] == ')' || s[i] == ']') {
                if (st.empty()) {
                    check = false;
                    break;
                }
                comp = st.top();   
                st.pop();
                if ((comp == '(' && s[i] == ']') || (comp == '[' && s[i] == ')')) {
                    check = false;
                    break;
                }
            }
        }
        if (! st.empty()) {
            check = false;
            while (! st.empty()) {
                st.pop(); 
            }
        }
        if (check == true) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
    string s;
    while(getline(cin,s),s!="."){
        string c;
        stack<char> w;
        for(int i=0;i<s.size();i++)if(s[i]==')'||s[i]=='('||s[i]=='['||s[i]==']')c+=s[i];
            for(int i=0;i<c.size();i++){
            if(w.empty()||c[i]=='['||c[i]=='(')w.push(c[i]);
            if(c[i]==']'&&w.top()=='['||c[i]==')'&&w.top()=='(')w.pop();
            else goto fail;
        }
        if(w.empty())cout<<"yes"<<endl;
        else fail:cout<<"no"<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <deque>
#include <cstring>
using namespace std;
bool beginC(char c) {
	return (c == '[' || '(' == c);
}
bool endC(char c) {
	return (c == ']' || ')' == c);
}
bool cuple(char l, char r) {
	return (l == '(' && r == ')') || (l == '[' && r == ']');
}
int size;
bool solve(char str[]) {
	deque<char> deq;
	for (int i = 0; i < size; i++) {
		if (beginC(str[i])) {
			deq.push_front(str[i]);
		}
		else if (endC(str[i])) {
			if (deq.empty()) return false;
			char recent = deq.front();
			if (!cuple(recent, str[i])) {
				return false;
			}
			deq.pop_front();
		}
	}
	return deq.size() == 0;
}

int main() {
	char str_c[128];
	while (1) {
		gets(str_c);
		size = strlen(str_c);
		string str(str_c);
		if (str == ".") return 0;
		cout << (solve(str_c) ? "yes" : "no") <<endl;
	}
	return 0;
}