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 <stack>
using namespace std;
string s;
bool solve() {
  stack<char> stk;
  for(int i=0; i!=(int)s.length(); ++i) {
    if(s[i] == '[' || s[i] == '(') stk.push(s[i]);

    else if(s[i] == ']') {
      if(stk.empty()) return 0;
      if(stk.top() != '[') return 0;
      stk.pop();
    }
    else if(s[i] == ')') {
      if(stk.empty()) return 0;
      if(stk.top() != '(') return 0;
      stk.pop();
    }
  }
  return stk.empty();
}

int main() {
  while(getline(cin, s) && s != ".") {
    cout << (solve() ? "yes" : "no") << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<algorithm>
using namespace std;
string s;
bool input(){
  if(getline(cin,s)){
    if(s==".")return false;
    return true;
  }
  return false;
}
bool solve(){
  stack<char> st;
  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())return false;
      char tmp = st.top();st.pop();
      if(s[i]==')'){
        if(tmp != '(')return false;
      }else{
        if(tmp != '[')return false;
      }
    }
  }
  return true;
}

int main(){
  while(input()){
    if(solve()){
      cout<<"yes"<<endl;
    }else{
      cout<<"no"<<endl;
    }
  }
}

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

int main()
{

	string text;

	getline(cin,text);
	while(text[0] != '.'){
		int ans=1 ,brac[50]={0} ,n=0;
		for(int i=0 ; text[i]!='.' && i<100 ; i++){
			//test	cout<<(int)text[i]<<" ";
			if(text[i]=='('){
				brac[n]=1;
				n++;
			}
			if(text[i]==')'){
				if(n==0) ans=0;
				else if(brac[n-1]!=1) ans=0;
				n--;
				brac[n]=0;
			}
			if(text[i]=='['){
				brac[n]=2;
				n++;
			}
			if(text[i]==']'){
				if(n==0) ans=0;
				else if(brac[n-1]!=2) ans=0;
				n--;
				brac[n]=0;
			}
			if(ans==0) break;
		}
		if(ans==0) cout<<"no"<<endl;
		else if(ans==1) cout<<"yes"<<endl;
		getline(cin,text);
	}
}

C++: // AOJ 1173

#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main(void)
{
	string str;
	while (1){
		getline(cin, str);
		if (str.length() == 1 && str[0] == '.') break;
		stack <char> st;
		st.push('a');
		for (int i = 0; i < str.length(); i++){
			if (str[i] == '(' || str[i] == '['){
				st.push(str[i]);
			}
			if (str[i] == ')'){
				if (st.top() == '('){
					st.pop();
				}
			}
			if (str[i] == ']'){
				if (st.top() == '['){
					st.pop();
				}
			}
		}
		if (st.size() == 1){
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	
	return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <vector>

#include <stdio.h>
#include <string.h>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

bool check(const string &S) {
    vector<char> stack;
    bool ok = true;

    for (int i = 0; i < (int) S.size(); i++) {
        char c = S[i];
        if (c == '(') {
            stack.push_back('(');
        } else if (c == '[') {
            stack.push_back('[');
        } else if (c == ')') {
            if (stack.empty()) {
                ok = false;
                break;
            }
            char prev = stack.back();
            stack.pop_back();
            if (prev != '(') {
                ok = false;
                break;
            }
        } else if (c == ']') {
            if (stack.empty()) {
                ok = false;
                break;
            }
            char prev = stack.back();
            stack.pop_back();
            if (prev != '[') {
                ok = false;
                break;
            }
        } else {
            // do nothing
        }
    }

    if (!stack.empty()) {
        ok = false;
    }

    return ok;
}

int main(void) {
    string S;
    char buf[128];
    int len;

    while (true) {
        fgets(buf, 128, stdin);
        len = strnlen(buf, 128);
        buf[len - 1] = '\0'; // remove newline
        S = string(buf);

        // cout << S << endl;
        if (S == ".") {
            break;
        }

        if (check(S)) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    return 0;
}

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

using namespace std;

void solve()
{
	string str;
	while(getline(cin, str))
	{
		if(str == ".")
		{
			break;
		}
		int parentheses = 0;
		int box_brackets = 0;
		bool ok_flag = true;
		for(int i = 0; i < str.size(); ++i)
		{
			if(str[i] == '(')
			{
				++parentheses;
			}
			else if(str[i] == '[')
			{
				++box_brackets;
			}
			else if(str[i] == ')')
			{
				--parentheses;
				if(parentheses < 0)
				{
					cout << "no" << endl;
					ok_flag = false;
					break;
				}
			}
			else if(str[i] == ']')
			{
				--box_brackets;
				if(box_brackets < 0)
				{
					cout << "no" << endl;
					ok_flag = false;
					break;
				}
			}
		}
		if(parentheses != 0 || box_brackets != 0)
		{
			ok_flag = false;
			cout << "no" << endl;
		}
		if(ok_flag)
		{
			cout << "yes" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}