need some expert help here!

This is a discussion on need some expert help here! within the C Programming forums, part of the General Programming Boards category; can anyone help me to complete this code? Code: typedef enum { NUMBER, BOOL, ERROR} token_type; typedef struct { token_type ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    need some expert help here!

    can anyone help me to complete this code?
    Code:
    typedef enum {
      NUMBER,
      BOOL,
      ERROR} token_type;
    
    typedef struct {
      token_type type;
      union {
        int int_val;
        int bool_val;
        char* error_message;
      };
    } token;
    
    typedef struct {
      const char* data;
      int len;
    } substring;
    
    static substring trim(substring str) {
      substring result;
      int i;
      result.len=0;
      for(i=0;i<str.len;i++) {
        if(str.data[i]!=' ') {
          result.data = str.data+i;
          result.len = str.len-i;
          break;
        }
      }
      for(i=result.len-1;i>=0;i--) {
        if(result.data[i]!=' ') {
          result.len = i+1;
          break;
        }
      }
      return result;
    }
    
    /**
     * Return -1 if matching bracket isn't found
     */
    int matching_braket(substring str, int open_pos) {
      int depth = 0;
      int i;
      for(i=open_pos;i<str.len;i++) {
        if(str.data[i]=='(') depth++;
        if(str.data[i]==')') depth--;
        if(depth==0) return i;
        if(depth<0) return -1;
      }
      return -1;
    }
    
    token fold(substring str) {
      str = trim(str);
      if(str.data[0]=='(') {
        int right_pos = matching_bracket(str,0);
        if(right_pos==str.len-1) {
          // it's likely BOOL_EXPR ::= '(' BOOL_EXPR ')' rule
          substring subtask;
          subtask.data = str.data+1;
          subtask.len = str.len-2;
          return token_fold(subtask);
        }
        // ...
      }
      // ... and so on
    }__________________
    the output screen is something like this :-

    Prompt: (1 = = 1) && (2 != 1) || (2 <= 3)
    Result: true

    Prompt: 1 > 2 || 2 >= 2 || 3 > 2
    Result: true

    Prompt: (2 > 3 && 5 = = 2) || (2 > 3)
    Result: false

    Prompt: (1 > 2) > (2 = = 2)
    Result: Syntax error: nonnegative number expected as operand for >

    Prompt: 1 && 2 || 3
    Result: Syntax error: Boolean expression expected as operand for &&
    Result: Syntax error: Boolean expression expected as operand for ||

    Prompt: ( 2 > 3 && 4 < 3
    Result: Syntax error: ) missing

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. Do your own homework.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Performance Expert - New York, NY
    By txaggie94 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-28-2007, 08:33 PM
  2. Expert system?
    By FloatingPoint in forum Tech Board
    Replies: 1
    Last Post: 06-14-2003, 08:23 AM
  3. Should't the Forums subcategorized into.. Newbie.. Advance.. and Expert Users
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-09-2002, 05:28 PM
  4. c++ expert
    By febrian81 in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2002, 11:27 PM
  5. Replies: 1
    Last Post: 09-30-2001, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21