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 <bits/stdc++.h>
using namespace std;
int main(){
    string s;
    while(getline(cin,s)){
        stack<char> w;
        int d=0;
        if(s[0]=='.')break;
        for(int i=0;i<s.size();i++){
            if(s[i]=='['||s[i]=='(')w.push(s[i]);
            else if(s[i]==']'){
                if(w.top()=='[')w.pop();
            }
            else if(s[i]==')'){
                if(w.top()=='(')w.pop();
            }
        }
        if(w.empty())cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
}

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

using namespace std;

int main()
{
  bool isBalanced;
  string str;
  stack<char> bracket;

  while (1) {
    getline(cin, str);
    if (str == ".") break;
    while (!bracket.empty()) {
      bracket.pop();
    }
    isBalanced = true;
    for (int i = 0; i < str.size(); i++) {
      if (str[i] == '(' || str[i] == '[') {
        bracket.push(str[i]);
      } else if (str[i] == ')') {
        if (bracket.empty()) {
          isBalanced = false;
          break;
        }
        if (bracket.top() == '(') {
          bracket.pop();
        } else {
          isBalanced = false;
          break;
        }
      } else if (str[i] == ']') {
        if (bracket.empty()) {
          isBalanced = false;
          break;
        }
        if (bracket.top() == '[') {
          bracket.pop();
        } else {
          isBalanced = false;
          break;
        }
      }
    }
    if (!bracket.empty()) {
      isBalanced = false;
    }
    isBalanced ? cout << "yes" << endl : cout << "no" << endl;
  }
}

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

string s;
bool is_balanced(int l, int r){
	for(; l < r; ++l) {
		switch(s[l]) {
		case '(':
		case'[':
			goto next;
		case ')':
		case ']':
			return false;
		}
	}

	return true;

next:;

	int cnt = 1;
	for(int i = l + 1; i < r; ++i) {
		switch(s[i]) {
		case '(':
		case'[':
			++cnt;
			break;
		case ')':
		case ']':
			if(--cnt == 0) {
				if(s[l] == '(' && s[i] == ')' || s[l] == '[' && s[i] == ']')
					return is_balanced(l + 1, i) && is_balanced(i + 1, r);
			}
		}
	}

	return false;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(getline(cin, s), s != ".")
		cout << (is_balanced(0, s.size()) ? "yes" : "no") << endl;

	return EXIT_SUCCESS;
}

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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  string s;
  while(getline(cin,s)){
    if(s==".") return 0;
    stack<int> ss;
    for(int i=0;i<s.length();i++){
      if(s[i]=='('){
        ss.push(0);
      }else if(s[i]=='['){
        ss.push(1);
      }else if(s[i]==')'){
        if(ss.empty()){
          cout << "no" << endl;
          goto next;
        }
        if(ss.top()==0){
          ss.pop();
        }else{
          cout << "no" << endl;
          goto next;
        }
      }else if(s[i]==']'){
        if(ss.empty()){
          cout << "no" << endl;
          goto next;
        }
        if(ss.top()==1){
          ss.pop();
        }else{
          cout << "no" << endl;
          goto next;
        }
      }
    }
    if(ss.empty()){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
    next:
    continue;
  }
  return 0;
}

C++: import java.util.Scanner;
import java.util.Stack;


public class Main {
	
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		stdIn.useDelimiter("\\.");
		Stack<Character> stack;
		while(true) {
			String tmp = stdIn.nextLine();
			if(tmp.length() == 1) break;
			stack = new Stack<Character>();
			boolean isYes = true;
			for(int i = 0; i < tmp.length(); i++) {
				if(tmp.charAt(i) == '(') {
					stack.push('(');
				}
				if(tmp.charAt(i) == '[') {
					stack.push('[');
				}
				if(tmp.charAt(i) == ')') {
					if(stack.isEmpty() || stack.pop() != '(') {
						isYes = false;
					}
				}
				if(tmp.charAt(i) == ']') {
					if(stack.isEmpty() || stack.pop() != '[') {
						isYes = false;
					}
				}
				if(!isYes) {
					break;
				}
			}
			if(!stack.isEmpty()) {
				isYes = false;
			}
			System.out.println((isYes)?"yes":"no");
		}
		
	}
	
}

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

int main() {
  while (true) {
    string s;
    getline(cin, s);
    if (s == ".")
      break;
    vector<int> stack;
    int sz = 0;
    int now = 0;
    int n = s.size();
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (s[i] == '(') {
        stack.push_back(')');
      } else if (s[i] == '[') {
        stack.push_back(']');
      } else if (s[i] == ')' || s[i] == ']') {
        if (stack.size() == 0) {
          ok = false;
          break;
        } else {
          if (stack[stack.size() - 1] == s[i]) {
            stack.pop_back();
          } else {
            ok = false;
            break;
          }
        }
      }
    }
    if (stack.size() > 0)
      ok = false;
    if (ok) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

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

using namespace std;

int main(void){
  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;
  }
  
  return 0;
}

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

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

    bool ans = true;
    stack<char> scale;
    for(size_t i = 0; ans && i < s.length(); i++){
      if(s[i] == '(' || s[i] == '['){ scale.push(s[i]); }
      else if(s[i] == ')' || s[i] == ']'){
        if(scale.empty()){ ans = false; }
        else if((s[i] == ')' && scale.top() == '(') || (s[i] == ']' && scale.top() == '[')){ scale.pop(); }
        else{ ans = false; }
      }
    }
    if(ans){ ans = scale.empty(); }

    if(ans){ cout << "yes" << endl; }
    else{ cout << "no" << endl; }
  }
  
  return 0;
}