#!/usr/bin/perl -w

use strict;

sub ismatch {
	my $string = shift @_;

	my @chars = split( //, $string);
	my @parens;
	foreach my $c (@chars) {
		if($c eq "(" || $c eq "[" || $c eq "{") {
			push(@parens, $c);
		} elsif ($c eq ")") {
			my $match = pop(@parens);
			if($match ne "(") {
				return "Error: parentheses do not match \n";
			}
		} elsif ($c eq "]") {
			my $match = pop(@parens);
			if($match ne "[") {
				return "Error: parentheses do not match \n";
			}
		} elsif ($c eq "}") {
			my $match = pop(@parens);
			if($match ne "{") {
				return "Error: parentheses do not match \n";
			}
		}
	}
	if(defined(pop(@parens))) {
		return "Error: parentheses do not match\n";
	}
	return "Parentheses match\n";
}

# main body of code

my $string1 = "public static void main(String[] args) {
		A a = new A();
		a.method1(\"first\");
	     }";

my $string2 = "if((dirp = opendir(\".\") == NULL)";

print ismatch($string1);
print ismatch($string2);


