Thread: Parsing a complicated string

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    9

    Question Parsing a complicated string

    Hello,

    the program I am writting receives a very complicated command line. One of the command line parameter itself actually describes some structure.

    For example, the parameter may look something like this:
    Code:
    QUEUE1[1-50,700-1000],QUEUE2,QUEUE3[1-10,50-100]{1-3,5-5=CUAC,CUAD},QUEUE4{1-4=CUAU}
    etc.

    Or in other words, it consists of name of queues, than there may be a list of substrings (i.e. take only characters 1 to 50 and 700 to 1000), and than there may be a list of filters (i.e. character 1,2,3 and 5 must equal to 'CUAC' or 'CUAD'), etc.

    Because of how the program is called, it has to be one string (no spaces). I am looking for an (academic) advice of how to parse such string effectively. For now, I have only implemented the queue names and substrings, and the parsing logic is getting pretty complicated and pain to modify. On the other hand, I would really prefer not creating a "sort-of" compiler with its own grammar.

    I can expect that the command line is correct, and i may modify it a little (instead of ',' use '@', add " if required, or something like that...), but it must remain a single string and must be callable from bash.

  2. #2
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    why does it have to be one string? you can pass multiple parameters to a strings via the command line.

    ABC++ - Lesson 11 - Command Lines
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Thanks for the reply, but I know I can pass multiple parameters in a command line. The problem is, that the string itself is generated by a black-box tool that does not handle spaces correctly
    Last edited by adamm; 02-21-2011 at 08:58 AM. Reason: typo

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by adamm View Post
    Thanks for the reply, but I know I can pass multiple parameters in a command line. The problem is, that the string itself is generated by a black-box tool that does not handle spaces correctly
    Are you having trouble with passing the string as one parameter or parsing it in the program or both?
    What are the rules for parsing the string i.e what information do you need to extract from the string?
    Pass it as a single parameter to your program by enclosing that complex string in quotes (single or double).

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Divide and conquer.
    Code:
    Loop: 
        confirm QUEUEn
        find the next comma in front of next QUEUEn or end of string; calc length of this piece 
        if '[' present { 
             find end ']', calculate length 
             call [ subroutine 
             advance pointer  
        }
        if '{' present { 
             find end '}', calculate length 
             call { subroutine 
             advance pointer  
        }
        advance pointer past comma
        repeat 
    endloop.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Dino: I did not consider divide and conquer. Thanks very much, looks viable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Parsing
    By Shaun32887 in forum C++ Programming
    Replies: 11
    Last Post: 07-02-2008, 02:15 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM