Thread: Reusing code blocks for different variables

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    35
    Quote Originally Posted by jarro_2783
    I don't know if you actually want to write this or if you just want a parser, but in the latter case you could consider looking at Flex and Bison. They are parser generators and are fairly easy to use.
    Quote Originally Posted by jafet
    But I don't think this fella is writing a whole new language; (s)he doesn't need a compiler-quality parser. Learning Flex/Bison/Yacc may take as much time as, if not more than, to just write a decent-quality string parser from scratch.
    jafet is right, I prefer writing my own string parser from scratch in C.

    Dave_Sinkula, the code you posted would not be considered a FSM, right? If it is, what exactly made it so? And if not, what would you have to add so that it would be considered a FSM?

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Queue
    Dave_Sinkula, the code you posted would not be considered a FSM, right? If it is, what exactly made it so? And if not, what would you have to add so that it would be considered a FSM?
    No. An FSM would have states, like first field, second field, etc. Then your foo might look at what state is in and do different things based on the state. Or perhaps foo is called for fields one and two, and some function bar is called for other fields. That sort of thing.

    [edit]Kinda like this.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int myfunction(const char *text, int len)
    {
       const char *comma = strchr(text, ',');
       if ( comma == NULL )
       {
          return 0;
       }
       printf("\"%.*s\"\n", len, text);
       fflush(stdout);
       return 1;
    }
    
    void parse(const char *text, int (*foo)(const char *, int len))
    {
       int state = 0;
       const char *end = text, *start = text;
       while ( *start != '\0' )
       {
          while ( *end != '\0' && !isspace(*end) )
          {
             ++end;
          }
          /*
           * Do stuff.
           */
          if ( state == 0 )
          {
             if ( !foo(start, end -  start) )
             {
                state = 1;
             }
          }
          if ( state == 1 )
          {
             printf("other state : \"%.*s\"\n", end -  start, start);
          }
          /*
           * Move the start and end pointers.
           */
          start = end;
          while ( *start != '\0' && isspace(*start) )
          {
             ++start;
          }
          end = start;
       }
    }
    
    int main(void)
    {
       const char line[] = "user1,user2 group1,group2,group3 string1 string2 string3";
       parse(line, myfunction);
       return 0;
    }
    
    /* my output
    "user1,user2"
    "group1,group2,group3"
    other state : "string1"
    other state : "string2"
    other state : "string3"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. keeping track of code blocks
    By Aisthesis in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2009, 10:13 PM
  2. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  3. Dev and Code Blocks
    By swgh in forum Game Programming
    Replies: 3
    Last Post: 04-07-2006, 07:21 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM