Programming Assignment 6
Command Prefix Recognition

Advanced Programming II, Spring 2001


Table of Contents

Due Date

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.

Background

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 command login can be typed as log or just l.

One problem with allowing prefixes to stand for commands is that a prefix may not uniquely identify a command. For example, if both login and logout are commands, then the prefix log (and any shorter prefix) is ambiguous because it could refer to either login or logout. In this case, the shortest unambiguous prefixes are logi and logo. In such cases, the command-line interpreter usually presents a list of possible commands and lets the user pick one.

Problem

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 named gen-cmd, might be used is
$ gen-cmd < cmd-file > cmd.cc

$ g++ -c cmd.cc

$

Program Input

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.

Program Output

The procedure generated by your program should have the prototype
const string * command(const string &)
The input to command() is a word. Output from command 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 word LoG should match the command login. When command() 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 generated command(). 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 by command().

Test Files

The file /export/opt/cs-509/pa6/main.cc is an example of how command() 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.