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

using namespace std;

void solve()
{
	string temp;
	string str;
	while(getline(cin, temp))
	{
		str += temp;
		if(str.size() == 1 && str[0] == '.')
		{
			break;
		}
		if(str[str.size() - 1] == '.')
		{
			bool ok = true;
			stack<int> Stack;
			int n = str.size();
			for(int i = 0; i < n; ++i)
			{
				if(str[i] == '(')
				{
					Stack.push(1);
				}
				else if(str[i] == '[')
				{
					Stack.push(2);
				}
				else if(str[i] == ')')
				{
					if(Stack.empty() || Stack.top() == 2)
					{
						cout << "no" << endl;
						ok = false;
					}
					else if(Stack.top() == 1)
					{
						Stack.pop();
					}
				}
				else if(str[i] == ']')
				{
					if(Stack.empty() || Stack.top() == 1)
					{
						cout << "no" << endl;
						ok = false;
					}
					else if(Stack.top() == 2)
					{
						Stack.pop();
					}
				}
				if(!ok)
				{
					str.clear();
					goto END;
				}
			}
			if(ok)
			{
				cout << "yes" << endl;
			}
			str.clear();
		}
END:
		;
	}
}

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

C++: #include<iostream>
#include<string.h>
using namespace std;
int main(){
    char f[102];char h;int c=0,d=0,l;
    while(1){c=0;d=0;
        cin.getline (f,156);
        if(f[0]=='.')
            return 0;
        for(int i=0;f[i]!='.';i++){
            if(f[i]=='('){
                c++;l=0;
            }
            if(f[i]==')'){
                c--;
                if(l==1){
                    cout<<"no"<<endl;
                    goto next;
                }
            }
            if(f[i]=='['){
                d++;l=1;}
            if(f[i]==']'){
                if(l==0){
                    cout<<"no"<<endl;
                    goto next;
                }
                d--;
                
            }
            if(c<0||d<0){
                cout<<"no"<<endl;
                goto next;
            }
        }
        if(c==0&&d==0)
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;next:;
    }
    return 0;
}

C++: /*
 * Problem     : AOJ1173
 * Title       : The Balance of the World (世界の天秤)
 * Author      : mizo0203
 * Copyright   : Your copyright free
 * Description : 文字列・スタック
 */

#include <stdio.h>
#include <stack>
#include <algorithm>
using namespace std;

char str[101];
stack<char> st;

bool input() {
  scanf("%[^\n]%*c", str);
  if (str[0] == '.')
    return false;
  return true;
}

int main() {
  int i;
  bool f;

  while (input()) {
    f = true;
    while(!st.empty()) st.pop();
    for (i = 0; str[i] != '\0' && f; i++) {
      switch (str[i]) {
      case '(':
        st.push(')');
        break;
      case ')':
        if (!st.empty() && st.top() == str[i])
          st.pop();
        else
          f = false;
        break;
      case '[':
        st.push(']');
        break;
      case ']':
        if (!st.empty() && st.top() == str[i])
          st.pop();
        else
          f = false;
        break;
      default:
        break;
      }
    }
    if (st.empty() && f)
      printf("yes\n");
    else
      printf("no\n");
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

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


int main() {
	string s;
	while (getline(cin, s), (s != ".")) {
		using P = pair<char, int>;
		queue<P> que;
		que.emplace('a', -1);
		bool ok = true;
		for (char c : s) {
			if (c == '('){ if(que.front().first == '(') que.front().second++; else que.emplace('(', 1); }
			if (c == ')'){ if(que.front().first == ')') que.front().second--; else ok = false; }
			if (c == '{'){ if(que.front().first == '{') que.front().second++; else que.emplace('{', 1); }
			if (c == '}'){ if(que.front().first == '}') que.front().second--; else ok = false; }
			if (que.front().second == 0) que.pop();
		}
		cout << (ok && que.size() == 1 ? "yes" : "no") << endl;
	}
}

C++: #include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

#define MARU 0
#define KAKU 1

int check(char c) {
  if (c == '(' ||
      c == ')') {
    return MARU;
  }
  if (c == '[' ||
      c == ']' ) {
    return KAKU;
  }
  return -1;
}

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

    string tstr;
    for (int i = 0; i < str.size(); i++) {
      if (check(str[i]) != -1) {
	tstr += str[i];
      }
    }

    if (tstr.size() == 0) {
      std::cout << "yes" << std::endl;
      continue;
    }
    if (tstr[0] == ')' ||
	tstr[0] == ']') {
      std::cout << "no" << std::endl;
      continue;
    }

    // std::cout << tstr << std::endl;
    bool yes = true;
    stack <int> st;
    st.push(check(tstr[0]));
    for (int i = 1; i < tstr.size(); i++) {
      if (st.size()) {
	int val = st.top();
	if (tstr[i] == '(' ||
	    tstr[i] == '[') {
	  st.push(check(tstr[i]));
	}else {
	  if (val != check(tstr[i])) {
	    yes = false;
	    break;
	  }else {
	    st.pop();
	  }
	}
      }else {
	if (tstr[i] == ')' ||
	    tstr[i] == ']') {
	  yes = false;
	  break;
	}else {
	  st.push(check(tstr[i]));
	}
      }
    }

    if (yes && st.empty()) {
      std::cout << "yes" << std::endl;
    }else {
      std::cout << "no" << std::endl;
    }
  }
}

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

using namespace std;

int main(void){

	char inp;
	stack<char> data;
	int flag,flag2;

	while(1){
		flag=0,flag2=0;
		while(!data.empty()) data.pop();
		while(1){
			inp=getchar();
			switch(inp){
				case '(':
					data.push('(');
					break;
				case '[':
					data.push('[');
					break;
				case ')':
					if(data.empty()==1){
						flag=1;
						break;
					}
					if(data.top()=='(') data.pop();
					else flag=1;
					break;
				case ']':
					if(data.empty()==1){
						flag=1;
						break;
					}
					if(data.top()=='[') data.pop();
					else flag=1;
					break;
				default:
					break;
			}
			if(inp=='.') break;
			flag2=1;
		}
		if(flag2==0) break;
		if(flag==0&&data.empty()==1) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
		getchar();
	}

	return 0;
}