This is a homework question. I would like to write a simple parser for Unix command line options.
First, I would like to define a grammar with BNF.
Options = Option | Options, space, Option;
Option = OptionName | OptionName, OptionArguments;
OptionName = '--', any character excluding '-' | OptionName, any character;
OptionArguments = OptionArgument | OptionArguments, space, OptionArgument;
OptionArgument = any character excluding '-' | OptionArgument, any character;
("any character" here is any alphanumeric character).
Does it make sense ? The next question is how to add "old" Unix options, which start with a single hyphen and can be grouped together (e.g. ls -lht
)
In the original Unix tradition, command-line options are single letters preceded by a single hyphen. Mode-flag options that do not take following arguments can be ganged together; thus, if -a and -b are mode options, -ab or -ba is also correct and enables both.
Options are a list of flags and other parameters that can control the behavior of the wt command line as a whole. Commands provide the action, or list of actions separated by semicolons, that should be implemented. If no command is specified, then the command is assumed to be new-tab by default.
The syntax for a UNIX command is broken into three parts: the command, a list of options, and a list of arguments. For the purposes of this class, all three must be done in this order, although either the options or the list of arguments may be excluded depending on what command you are using and how it is being used.
Just notice that the given grammar is quite ambiguous - for example, if you have a few words in a row, you wouldn't know if these are different options or an option with some arguments.
As for your second question (regarding "old" unix), you could add another rule to the grammar, something of the sort:
option -> optionGroup | (anything that was there before);
optionGroup -> '-', flags;
flags -> flag | flag, flags;
flag -> single letter;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With