开发者

Grammar for Unix command line options

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 exc开发者_运维技巧luding '-' | 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)


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜