如何正确形式化 GNU/Linux 命令的命令行用法?
我想写一个类似 BNF 的形式语法来描述一些 GNU/Linux 工具的命令行用法。例如,我可以将 cat
命令的用法描述为:
(cat-command) : 'cat' (arguments-list)
(arguments-list) : (argument)
(arguments-list) : (arguments-list) (argument)
(argument) : (file)
问题是我无法写下某些命令(例如 md5sum
)的精确语法。我的第一次尝试如下:
(md5sum-command) : 'md5sum' (arguments-list)
(arguments-list) : (argument)
(arguments-list) : (arguments-list) (argument)
(argument) : (file)
(argument) : '--check'
但是正如您所看到的,此语法允许您根据需要多次指定 --check
参数,这是不正确的,因为您最多应该使用它一度。
我该如何解决这个问题?另外,我应该学习什么样的形式语法才能更好地处理这类问题?
I'd like to write down a BNF-like formal grammar for describing the command line usage of some GNU/Linux tools. For example I can describe the usage of the cat
command as:
(cat-command) : 'cat' (arguments-list)
(arguments-list) : (argument)
(arguments-list) : (arguments-list) (argument)
(argument) : (file)
The problem is I can't write down a precise grammar for some commands such as md5sum
. My first attempt at that would be the following:
(md5sum-command) : 'md5sum' (arguments-list)
(arguments-list) : (argument)
(arguments-list) : (arguments-list) (argument)
(argument) : (file)
(argument) : '--check'
But as you can see this grammar allows you to specify the --check
argument as many times as you wish, which is incorrect as you should use it at most one time.
How can I fix that? Also, what kind of formal grammars I should study for better treating this kind of problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试类似的操作:
假设您希望能够为每个命令指定一个
--check
,但不依赖于它作为第一个参数,您可以使用:另请注意,管道 (
|
) 符号只是附加规则的快捷方式。以下是等效的:如果您不能使用上下文无关语法(如 BNF 中表达的语法)指定大多数 UNIX 命令,我会感到惊讶。
You could try something like:
Assuming you want to be able to specify exactly one
--check
per command, but not depend on it being the first argument, you could use:Also note, that the pipe (
|
) symbol is just a shortcut for an additional rule. The following is equivalent:I'd be surprised if you couldn't specify most unix commands with a context free grammar like those expressed in BNFs.
我可能找到了答案,尽管这不是预期的答案。您可以选择识别命令的正确性,而不是生成正确的命令。使用某种混合语言,您可以编写以下一组要求:
我不会将此答案标记为正确的答案,因为我仍然很好奇是否存在一种生成正确命令的方法。
I probably found an answer, although it's not the expected one. You can choose to recognise the correctness of a command instead of generate correct commands. Using some hybrid language, you can write the following set of requirements:
I won't mark this answer as the correct one because I'm still curious to know if there exists a way to generate correct commands.