Tokenizer
InterviewTime limit1sMemory limit1024 MB
Split a shell command line at the delimiters <, >, &&, ||, (, ), and spaces, then print the non-space tokens separated by single spaces.
- Level
Easy3 of 10
- Topics
- String, Implementation
- Solved
- No attempts yet
Problem
Joowon is building a shell that gives users a text interface for interacting with the system. When the shell receives a command line, it calls the tokenizer, lexer, parser, expander, and executor in that order to interpret the command and run a valid program. Joowon first needs a tokenizer that splits a command line at the delimiters below. Joowon has always split strings on spaces only and finds it hard to split a command at several delimiters. Help Joowon build this tokenizer.
The tokenizer works as follows.
- Read the string from the front.
- When a delimiter is found, cut the string on both sides of the delimiter. Then continue reading from the position right after the delimiter. Each cut piece is called a token.
- When the end of the string is reached, print the tokens from front to back, separated by single spaces. Tokens that are a single space are not printed.
Let S be grep skku <infile> outfile. When S is split into tokens by the character :, the result is:
grep: :skku: : : :<:infile:>: :outfile
Remove the tokens that consist only of a single space, and print the rest in order, separated by single spaces. For this S, the output is grep skku < infile > outfile.
Input
The first line contains the shell command S. The command consists of delimiters, uppercase and lowercase English letters, digits, and $, ?, +, _. No run of & or | characters occurs an odd number of times in a row.
Output
Split the command S at the delimiters, then print the tokens separated by single spaces. Do not print tokens that consist only of a single space.