program par;
{controlla se un'espressione ha le parentesi bilanciate}

uses ustackca;

var expression:string;


function isbalanced(expression:string):boolean;
 var parstack:stack;
     nextch,closepar,openpar:char;
     index:integer;
     balanced:boolean;
 begin
  parstack.init;
  balanced:=true;
  index:=1;
  while balanced and (index <=length(expression)) do
   begin
   nextch:=expression[index];
   if nextch in ['(','[','{'] then  parstack.push(nextch)
   else if nextch in [')',']','}'] then
          begin
          closepar:=nextch;
          parstack.pop(openpar,balanced);
          if balanced then
            case closepar of
            ')': balanced := (openpar = '(');
            ']': balanced := (openpar = '[');
            '}': balanced:= (openpar = '{')
            end;
          end;
  index:=index+1;
 end;
 isbalanced:=balanced and parstack.isempty
 end;

 begin
  writeln('Scrivi un''espressione con parentesi:');
  readln(expression);
  if isbalanced(expression) then writeln('Le parentesi vanno bene')
  else writeln('Attento! le parentesi non sono bilanciate!')
 end.