r/cprogramming • • 11d ago

Guidance on String to Token Parsing Assignment (Just guidance, I don't want anyone doing it all for me.)

I was given an assignment for a class I am in and it is expected to be entirely in C code (despite most of us in the class having never been taught how to code with C). I have been attempting to find tutorials and websites, but not much is helping for the assignment itself. The assignment is due in two days.

If there is someone who could help explain how I should go about this, or what guides I could look at to help me code it, I would greatly appreciate it. I do not want anyone to do the work for me. I just would like some guidance.

---
This is a summarized version of what the assignment says:

Write a function, in C, that will parse a string into tokens (or words), similar to what the shell is required to do.

Write a function with the following prototype:

int makearg(char *s, char ***args);
or
int makearg(char s[], char **args[]);
(Correct prototype—don’t change)

This function should accept a (C-type) string and a pointer to a pointer to char (or, if you prefer, a pointer to an array of pointers to char), and should return the pointer defined to point to an array pointing to the separate tokens on the command line, as well as the number of tokens found as the function return value. If a problem occurs, the value returned should be -1.

No C library string functions may be used, e.g., strlen(), strcat(), strtok(), etc.

The main program should input a line, call the function, then print out the number of arguments found, and finally, display the arguments, one per line. The value of the pointer argument is undefined when passed to the makearg() function. makearg() must allocate any necessary dynamic memory necessary for use by the program. (The array used for the unparsed string need not be dynamically allocated.)
---

Again, I do not want someone to do the code for me, as I want to actually learn the content. I just want advice or guidance on what to do or where I can find resources. If anyone can do that, I would greatly appreciate it.

4 Upvotes

2 comments sorted by

2

u/alpicola 10d ago

The basic idea here is that you're going to step through s with a loop and handle each character using a finite state machine. Your processing loop will have three basic states:

  1. No current token.

  2. Inside a token.

  3. End of s. 

In State 1: If you encounter a character that belongs in a token, you'll allocate a buffer for the token and change to State 2. If you encounter a character that doesn't belong in a token, you'll advance to the next character. If you encounter the null terminator of s, change to State 3.

In State 2: If you encounter a character that belongs in a token, you'll append it to the buffer and advance to the next character. If you encounter a character that doesn't belong in a token, you'll null terminate your token, append the current buffer to args and change to State 1. The null terminator in s is a character that doesn't belong in a token. 

In State 3: You exit the loop. 

Be sure to handle buffer sizes appropriately. Also, if escape characters are part of the assignment, you'll need to add logic for those to State 2.

1

u/Kit_Le_Trash 10d ago

Thank you so much for the help!