Question: in c + + Here are the new requirements Variables in this shell will be identifiers that begin with the $ character but otherwise follow
in c
Here are the new requirements
Variables in this shell will be identifiers that begin with the $ character but otherwise follow the usual convention where you should use upper and lower case letters or digits
Variables will only store strings and they are defined upon first use.
They are set using the set command which has the syntax SET
The contents of a variable are displayed using the echo command: echo
It is possible to use a variable in any of the other commands.
Update your tokenizer to scan and parse these new features. Use a dictionary for a symbol table for variables with name and current value as entries. Print the symbol table whenever a parse has been completed.
the code:
#include
#include
#include
#define MAXTOKENS
#define MAXTOKENLENGTH
Function to tokenize the command string
char tokenizeconst char command, int tokencount
char tokens mallocMAXTOKENS sizeofchar;
if tokens
perrorFailed to allocate memory";
exitEXITFAILURE;
char commandcopy strdupcommand;
if commandcopy
perrorFailed to duplicate command";
freetokens;
exitEXITFAILURE;
const char delimiters t
; Delimiters: space, tab, newline
char token strtokcommandcopy, delimiters;
tokencount ;
while token NULL && tokencount MAXTOKENS
tokenstokencount strduptoken;
if tokenstokencount
perrorFailed to allocate memory for token";
Free up previously allocated tokens
for int i ; i tokencount; i
freetokensi;
freetokens;
freecommandcopy;
exitEXITFAILURE;
tokencount;
token strtokNULL delimiters;
freecommandcopy;
return tokens;
Function to free the allocated tokens
void freetokenschar tokens, int tokencount
for int i ; i tokencount; i
freetokensi;
freetokens;
int main
const char command ls l homeuserdocs;
int tokencount ;
char tokens tokenizecommand &tokencount;
printfTokens:
;
for int i ; i tokencount; i
printfs
tokensi;
freetokenstokens tokencount;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
