-
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;
}
-
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.
-
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.
-
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
-
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.