1. Engineering
  2. Computer Science
  3. the following is in c the instructions for completion are...

Question: the following is in c the instructions for completion are...

Question details

The following is in C++. The instructions for completion are at the bottom.

#include
#include
#include
#include
#include
#include

// function to determine if a token
vector is_token(const string& s) {
}

int main() {
// make this into a loop such that, like a terminal, input is accepted until the user hits CTRL+d
string str;
cout<<">";
getline(cin, str);

//vector to store the strings or characters returned by is_token() function
vector tokens;

tokens = is_token(str);
}

Modify the main() function to loop until the user exits (e.g. by pressing CTRL+d).

Write a function named is_token() that accepts a string of user input and determines what is a token (see below for token determination rules).
The function must return the separated tokens.

Determining if something is a token:

1. Whitespace (space, tab, etc) ends a token and is skipped unless it is in quotes (see below). You will find the function int isspace(char) useful for detecting whitespace characters.
2. Some characters are considered special tokens: | ; < > &. When one of these characters is encountered, the token being built is completed and a new token consisting of the character is created.
3. The character \ is a special character, i.e. an escape character. The next character will be part (or the start) of a token.
4. Items between single or double quotes are treated as parts of the current token.
5. Characters that do not meet the above rules are added on to the current token being built.

Some examples to test your output:

Input: > we\'ll do some "crazy \"air quotes\""
Output (tokens separated by commas): we'll, do, some, crazy "air quotes"

Input: > ls | sort >sorted_files.txt
Output (tokens separated by commas): ls, |, sort, >, sorted_files.txt

Input: > "now\;is"
Output: now;is

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution