Thread: Parentheses Checker Problem!!

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Parentheses Checker Problem!!

    Hey guys, I'm new here.. So I want to apologize beforehand if my question seems confusing or if I accidentally do something completely wrong.. :/

    Anyway, I'm doing a parentheses checker for my programming class. To be quite honest, the teacher doesn't have much to say so I pretty much do everything out of the book or on my own.

    However, I can't seem to get this problem to work. It's supposed to take the file from the command line and check if the parentheses within each line is correctly put. However, I can't seem to compile it.. What am I doing wrong??

    There are 3 layers to this program: Main/application, the interface, and the primitives..

    Here is my code for main and the interface. I don't think I need to post up the code for my primitives.. If I do, please let me know!

    Code:
    /******************/
    /* main.c         */
    /******************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "globals.h"
    #include "stack.h"
    
    #define ERR_PUSH
    #define ERR_POP
    #define ERR_MATCH
    #define ERR_EOL
    
    char matching_symbol( char c );
    
    int main( int argc, char *argv[] ){
    
      char c;
      char *ptr;
      char buffer[BUFSIZ];
      bool error;
      stack S;
    
      init_stack(&S);
    
      printf( "? " );
      while( gets( buffer ) != NULL && buffer[0] != '\0' ){
        error = FALSE;
        for( ptr = buffer; *ptr != '\0' && error == FALSE; ptr++ ){
          switch( *ptr ){
          case '(':
          case '[':
          case '{':
    	if( push_char( &S, *ptr ) == ERROR ){
    	  error = TRUE;
    	  printf( ERR_PUSH, ptr - buffer + 1 );
    	}
    	break;
          case ')':
          case ']':
          case '}':
    	if( pop_char( &S, &c ) == ERROR ){
    	  error = TRUE;
    	  printf( ERR_POP, ptr - buffer + 1 );
    	  break;
    	}
    	if( *ptr != matching_symbol( c ) ){
    	  error = TRUE;
    	  printf( ERR_MATCH, *ptr, ptr - buffer + 1, matching_symbol( c ) );
    	}
    	break;
          }
        }
        if( error == TRUE ){
          while( empty_stack( &S ) == FALSE ){
    	pop_char( &S, &c );
          }
        }
        else if( empty_stack( &S ) == TRUE ){
          printf( "Valid input.\n" );
        }
        else{
          printf( ERR_EOL );
          while( empty_stack( &S ) == FALSE ){
    	pop_char( &S, &c );
    	printf( "%c ", matching_symbol( c ) );
          }
          printf( "\n" );
        }
        printf( "? " );
      }
    }
    
    char matching_symbol( char c ){
      
      switch ( c ){
      case '(': return ')';
      case ')': return '(';
      case '[': return ']';
      case ']': return '[';
      case '{': return '}';
      case '}': return '{';
      }
      return 0;
    }
    Code:
    /*********************/
    /* stackinterface.c  */
    /*********************/
    
    #include <stdlib.h>
    #include "stackinterface.h"
    #include "globals.h"
    
    /* define push_char */
    extern status push_char( stack *p_S, char c ){
      char *p_c = (char *) malloc(sizeof(char));
    
      if( p_c == NULL )
        return( ERROR );
      *p_c = c;
      if( push( p_S, p_c ) == ERROR ){
        free( p_c );
        return( ERROR );
      }
      return( OK );
    }
    
    
    /* define pop_char */
    extern status pop_char( stack *p_S, char *p_c ){
      char *p_data;
      if( pop( p_S, &p_data ) == ERROR )
        return( ERROR );
    
      *p_c = *p_data;
      free( p_data );
      return( OK );
    }
    
    
    /* define top_char */
    extern status top_char( stack *p_S, char *p_c ){
      char *p_data;
      if( top( p_S, &p_data ) == ERROR )
        return( ERROR );
    
      *p_c = *p_data;
      return( OK );
    }
    It keeps saying there is an error with: ERR_PUSH, ERR_POP, ERR_MATCH, and ERR_EOL..

    Please save me! Ahhh. Thanks so much.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    What are the errors you are experiencing?
    I haven't bothered trying to compile it, but one thing I see wrong is this all lines like this:
    Code:
    	  printf( ERR_PUSH, ptr - buffer + 1 );
    Where ERR_PUSH is empty...

    And what are all those "extern" declarations meant to be? It's not extern; it's right there. Also, you shouldn't use gets.

    But that's just after quickly glancing over the code, I bet there are more problems. What exactly are you experiencing?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    It says I only have 4 errors. I'll paste them..

    Line 38: error: expected expression before ',' token
    Line 46: error: expected expression before ',' token
    Line 51: error: expected expression before ',' token
    Line 65: error: too few arguments to function 'printf'

    All the errors are in main.c.
    Line 38 is the line where ERR_PUSH was used.
    Line 46 is the line where ERR_POP was used.
    Line 51 is the line where ERR_MATCH was used.
    Line 65 is the line where ERR_EOL was used..

    However, the problem is that the professor actually gave us the beginning of our main.c and there supposedly is only 1 error within the entire main.c But I get more than that..

    I'm also using PuTTy and Code::Blocks to edit and compile if that's of any use of info..
    Last edited by chakolate; 11-03-2010 at 02:52 PM. Reason: Added client/compiler/editor

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    #define ERR_PUSH
    #define ERR_POP
    #define ERR_MATCH
    #define ERR_EOL
    Fix the above code. And your problems will likely go away. Try "ERR_EOL" first because it is the easiest.
    Hint: EOL means something like end of line

    Note: The same mistake made 4 times.

    Tim S.
    Last edited by stahta01; 11-03-2010 at 06:31 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    To be quite honest with you, I don't know how to go about fixing that portion because that's the portion in which the professor gave us and she deemed it to be fully correct.. And she doesn't exactly aid us whatsoever and we have to do much of it ourselves with only one book (which is old because the coding, we have to fix as well ). I'm at a lost. :/ Is there anyway you could possibly lead me in the right direction??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM