Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly formalize the command line usage of GNU/Linux commands?

Tags:

grammar

bnf

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?

like image 365
Francesco Turco Avatar asked Apr 08 '10 12:04

Francesco Turco


1 Answers

You could try something like:

(md5sum-command) : 'md5sum' (arguments-list)
(arguments-list) : (file-arguments) | '--check' (file-arguments)
(file-arguments) : (file) (file-arguments)

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:

(md5sum-command) : 'md5sum' (arguments-list)
(arguments-list) : (file-arguments) | (file-arguments) '--check' (file-arguments)
(file-arguments) : (file) (file-arguments)

Also note, that the pipe (|) symbol is just a shortcut for an additional rule. The following is equivalent:

(md5sum-command) : 'md5sum' (arguments-list)
(arguments-list) : (file-arguments) 
(arguments-list) : (file-arguments) '--check' (file-arguments)
(file-arguments) : (file) (file-arguments)

I'd be surprised if you couldn't specify most unix commands with a context free grammar like those expressed in BNFs.

like image 109
Daren Thomas Avatar answered Sep 24 '22 17:09

Daren Thomas