This assignment is due on Tuesday, 10 April, no later than 2:00 p.m.See the assignment turn-in page for instructions on turning in your assignment.
As a convenience, command line interpreters often let users type the initial part (or prefix) of a command rather than the whole command. For example, the commandlogin
can be typed aslog
or justl
.One problem with allowing prefixes to stand for commands is that a prefix may not uniquely identify a command. For example, if both
login
andlogout
are commands, then the prefixlog
(and any shorter prefix) is ambiguous because it could refer to eitherlogin
orlogout
. In this case, the shortest unambiguous prefixes arelogi
andlogo
. In such cases, the command-line interpreter usually presents a list of possible commands and lets the user pick one.
Write a program that generates a C++ procedure to recognize a set of commands. In particular, your program reads from std-in a set of single-word commands and writes to std-out a procedure that recognizes the given commands or their unambiguous prefixes. An example of how your program, assuming it's namedgen-cmd
, might be used is$ gen-cmd < cmd-file > cmd.cc $ g++ -c cmd.cc $
The input to your program is a sequence of one-word commands; white space in excess of that needed to separate one command from another is ignored. Any non-white-space characters may appear in a command, and there is no limit to either a command's length or the number of commands.
The procedure generated by your program should have the prototypeThe input toconst string * command(const string &)command()
is a word. Output fromcommand
is a pointer to an array of strings, where each string is a command matched by the input word. If the array contains one string, it is the command uniquely identified by the input word. If the array contains more than one string, then the input word was ambiguous and the array contains all the possible commands which the input word matched. If the array is empty then the input word didn't match any command. The end of the array is marked by an empty string containing no characters.
command
's comparison should be case insensitive; the input wordLoG
should match the commandlogin
. Whencommand()
returns more than one command in the array, the order of the commands in the array should follow the order of the commands given in the input that generatedcommand()
. Note that the empty prefix matches any word.Your program may generate any extra output you feel is necessary, but
command()
should be the the only externally visible entity. Your program's output, when captured in a file, should compile without needing any other files, as shown in the example above.The code calling
command()
should not be required to do any storage management with respect to the value returned bycommand()
.
The file/export/opt/cs-509/pa6
/main.cc
is an example of howcommand()
might be used; you can also use it as a test harness for the code you generate.
This page last modified on 27 March 2001.