Thread: Recursion Project

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Recursion Project

    The program deals with tokens and seperators.

    So I need to use recursion to get the next token from the input, choose if it is a token or separator, and then return the token buffer, and store the delimiters into another buffer which gets returned in another method.

    My issue is the recursion. We are supposed to use fgets to put the input line into the line buffer, but once the char's are in the buffer, the only way i can think of going through it is using a while loop. Doing recursion with the method just scans in another line to the buffer, and everything after the first token and first separator is lost.

    I have been struggling with this for a long time, please help!!

    Code:
    
    char *tokrdr_next_token(void){
    
        if( strlen( _tokrdr_line_buffer) > _tokrdr_line_limit){
            error_fatal(TOKRDR_ERROR_LINE_LENGTH, _tokrdr_line_number);        
        }
        fgets ( _tokrdr_line_buffer, _tokrdr_line_limit, _tokrdr_input);
    
        _tokrdr_token_index = 0;
        _tokrdr_separator_index = 0;
        _tokrdr_next_character = _tokrdr_line_buffer[_tokrdr_token_index];
    
        if(_tokrdr_next_character == '\n'){
            _tokrdr_line_number++;
        }
    
        printf("NEXT CHAR %s\n", _tokrdr_line_buffer);
    
        while (_tokrdr_next_character != *_tokrdr_delimiters){        
            _tokrdr_next_character = _tokrdr_line_buffer[_tokrdr_token_index];
            _tokrdr_token_buffer[_tokrdr_token_index] = _tokrdr_next_character;
            _tokrdr_token_index++;
        }
    
        while (_tokrdr_next_character == *_tokrdr_delimiters){        
            _tokrdr_next_character = _tokrdr_line_buffer[_tokrdr_token_index + _tokrdr_separator_index];
            _tokrdr_separator_buffer[_tokrdr_separator_index] = _tokrdr_next_character;
            _tokrdr_separator_index++;
        }
    
        //tokrdr_next_token();
    
        printf("NEXT CHAR %c\n", _tokrdr_line_buffer[_tokrdr_token_index]);
    
    
        return _tokrdr_token_buffer;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Write out in words or pseudo-code what you want to happen step by step. That's the easiest way I find to work through an issue. Once you hae an actual idea of what you expect it to do, one step at a time, it's easier to translate that into actual code.


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

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    If that function is simply supposed to scan each token (recursively) until reaching a delimiter, than you should not invoke the fgets() function from that method. Have another function that calls fgets() and stores the input into the buffer, and then call the function.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2
    OK, so i would use a recursion helper method

    but since fgets scans in the whole line, and the method only returns 1 token (and sets up the separator to be returned in another method), how do the second and third tokens get returned.

    if $ is a separator:
    EXAMPLE: if the input line was one$$two$$$THREE$$, the method would only return one as a token and $$ since after running once through the helper method it would use fgets to read the next line

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Return an array of pointers to characters, and assign each token it's own string.
    Code:
    char **foo( char *buffer, char **list, size_t size )
    {
        peal off a token from the buffer
        realloc the list with 1 more row
        add what you pealed off to the end
        if not at end of buffer
            list = foo( buffer, list, currentsize )
    
        return list;
    }
    Something like that should work.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. recursion project
    By Psycho in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2002, 03:24 PM