/* ECP: FILEname=fig10_4.c */ /* 1*/ int /* 2*/ CheckBalance( void ) /* 3*/ { /* 4*/ Stack S = StMakeEmpty( NULL ); /* 5*/ Symbol Last = { 0, 1 }, Match; /* 6*/ int Errors = 0; /* 7*/ while( ( Last.Sym = getchar( ) ) != EOF ) /* 8*/ switch( Last.Sym ) /* 9*/ { /*10*/ case '\n': /*11*/ Last.Line++; /*12*/ break; /*13*/ case '(': case '[': case '{': /*14*/ Push( Last, S ); /*15*/ break; /*16*/ case ')': case ']': case '}': /*17*/ if( StIsEmpty( S ) ) /*18*/ { /*19*/ Errors++; /*20*/ printf( "Extraneous %c at line %d\n", /*21*/ Last.Sym, Last.Line ); /*22*/ } /*23*/ else /*24*/ { /*25*/ Match = Top( S ); Pop( S ); /*26*/ if( CheckMatch( &Match, &Last ) ) /*27*/ Errors++; /*28*/ } /*29*/ break; /*30*/ default: /*31*/ break; /*32*/ } /*33*/ while( !StIsEmpty( S ) ) /* Unmatched Symbols */ /*34*/ { /*35*/ Errors++; /*36*/ Match = Top( S ); Pop( S ); /*37*/ printf( "Unmatched %c at line %d\n", /*38*/ Match.Sym, Match.Line ); /*39*/ } /*40*/ StRecycle( S ); /*41*/ return Errors; /*42*/ }