Thread: Posting code for feedback/evaluation.

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Posting code for feedback/evaluation.

    Hi,

    Is it within the forums' purpose to post code for evaluation/feedback? I am trying to learn C although I am not a youth.

    The rules stated not to expect complete program revisions which is not a problem for what I need.

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Yes, you can go ahead and ask for feedback on your code. Of course, it would be good to explain what the code is supposed to do, perhaps with sample input and output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    This is a simple syntax checker for Boolean expressions of the type:
    a) (A+!B).(!C+D)
    b) (!!D+E).(!(!!E)+F)

    At first the input string is tokenized using '(', ')', '.', '+', '!', '\0'

    as delimiters.

    The syntax checker/parser tests that tokens are preceded and succeeded by acceptable tokens. The starting and ending tokens are tested separately as they are special cases. When an illegal token is found the error position saved. Two errors are saved with the first one being reported to the user.

    If more information is needed please post.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define INVALID 0
    #define VALID   1
    
    typedef char* pchar;
    pchar* tokens;             /* global variable to hold token list */
    int token_count = 0;       /* global variable to keep track of token count */
    int error_pos = 0;         /* global variable to hold error position */
    
    #define token_delta 50
    
    void tokenise(char* expression) {
      char ch;              /* character at iterator's position */
      char* atoken;         /* token to be added to token list  */
      char* start = NULL;   /* holds a char* to point to start of multicharacter token */
      char* string_token;   /* holds multicharacter token */
      int char_count = 0;   /* keeps track of number of characters in multicharacter token */
      
      int while_counter = 0;
      /* expr_len_and_null holds the total length of input string including null terminator */
      int expr_len_and_null = strlen(expression) + 1;
      while (while_counter++ <= expr_len_and_null) {
        ch = *expression++; 
        switch (ch) {
          case '(': 
          case ')':
          case '.':
          case '+':
          case '!':
          case '\0':
            if (start && char_count > 0) {
              string_token = malloc((char_count + 1)*sizeof(char));
              strncpy(string_token, start, char_count);
              tokens[token_count++] = string_token;
                      
              start = NULL;
              char_count = 0;
            }                
          
            if (ch == '\0') return;
            atoken = malloc(2*sizeof(char));
            atoken[0] = ch;
            atoken[1] = '\0';
            tokens[token_count++] = atoken;
            break;
            
          case '\n':
            break;  
            
          default:
            if (start == NULL)
              start = expression - 1;
            char_count++;  
        }
      }
    }
    
    void free_memory() {
      int k = token_count;
      
      while (--k >= 0) free(tokens[k]);
      free(tokens);
    }
    
    /****************PARSER*****************/
    
    static char* getToken(pchar* tokens, int i) {
      if (i < token_count)
        return tokens[i];
        else return NULL;
    }
    #define alpha              "abcdefghijklmnopqrstuvwxyz"
    
    /* succeeds "! alpha " */
    #define oper_op_br         "!+.("
    
    
    static int isOperand(pchar* tokens, int i) {
      char* atoken = getToken(tokens, i);
      
      if (atoken == NULL) 
        return 0;
      else {
        if (strlen(atoken) > 1)
          return 1;
          else return (strchr(alpha, atoken[0]) != NULL);
      }  
    }
    
    int getErrorPosn(pchar* tokens) {
      int i = -1;
      char prev_ch = '\0', ch;
      int error_pos1 = -1, error_pos2 = -1;
      char* atoken;
      
      if (token_count == 1) {
        if (!isOperand(tokens, 0))
          i = 0;
          
        error_pos = i;
        return error_pos;  
      }  
      
      while (++i < token_count) {
        atoken = getToken(tokens, i);
        ch = atoken[0];
        
        if (i > 0 && i < token_count - 1) {
          if ( (ch == '+' || ch == '.' || ch == ')') && 
               (prev_ch == ')' ||  isOperand(tokens, i - 1))  )
          {    
            prev_ch = ch;  
            continue;
          }
            
          if ( (ch == '!' || ch == '(' || isOperand(tokens, i)) &&
               strchr(oper_op_br, prev_ch) )
          {
            prev_ch = ch;
            continue;
          }
        }
        prev_ch = ch;
        
        /* at the end of token list */
        if ( (i == token_count - 1) && (ch == ')' || isOperand(tokens, i)) )
          continue;
        /* at the beginning of token list */  
        if ( (i == 0) && (ch == '!' || ch == '(' || isOperand(tokens, i)) )
          continue; 
          
        error_pos1 = i;
        break;   
      }
      
      int brackets = 0;
      for (i = 0; i < token_count; i++) {
        atoken = getToken(tokens, i);
        ch = atoken[0];
        
        if (ch == '(') brackets++;
          else if (ch == ')')
            brackets--;
            
        if (brackets < 0) {
          error_pos2 = i;
          break;
        }    
      }
      
      if (brackets > 0)
        error_pos2 = i;
      
      /* choose the earlier error for output */
      if (error_pos1 > error_pos2)
        error_pos = error_pos1;
        else error_pos = error_pos2;
        
      return error_pos;  
    }
    
    
    /***************************************/
    
    int main() {
      extern int error_pos;
    
      char input[101];
    
      tokens = malloc(token_delta*sizeof(char*));
      fgets(input, 100, stdin);
      tokenise(input);
      
      int k = -1;
      /* print the tokens one per line */
      printf("token count is %d\n", token_count);
      while(++k < token_count)
        printf("%s\n", tokens[k]);
        
      getErrorPosn(tokens);
      if (error_pos > -1)
        printf("Error found at position %d\n", error_pos + 1);
        else printf("No error found.\n");
    
      free_memory();
      
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. posting code
    By birdog9999 in forum Tech Board
    Replies: 4
    Last Post: 05-17-2015, 04:13 PM
  2. Feedback on prime number generator source code?
    By sepp in forum C Programming
    Replies: 5
    Last Post: 12-21-2014, 09:20 PM
  3. mini game - please feedback on code design
    By CoffeCat in forum C++ Programming
    Replies: 17
    Last Post: 06-13-2012, 02:42 AM
  4. Need feedback on code
    By dynamethod in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2007, 03:20 AM
  5. C Code for If Evaluation
    By satheesh in forum C Programming
    Replies: 6
    Last Post: 07-23-2007, 02:49 AM

Tags for this Thread