I googled and found two packages:
- mdwtools
- bnf
Unfortunately, both of them have extremely poor documentation. There are only one to two examples for each one, which makes it hard to follow. Furthermore, none of them shows how to indent the grammar properly, rather than just setting up the environment to have a nice look. I wonder is there a major package for writing BNF, or we can just manually use tabbing to handle the indentation? Thank you.
Best Answer
The
syntax
package from themdwtools
bundle has simple ways of typesetting BNF grammars, with some reasonable controls over how the rules are formatted.The space between grammar rules can be set with the
\grammarparsep
length. This is a rubber length, and defaults to8pt plus 1pt minus 1pt
.The distance between the left hand side of the production rule and the right hand side is controlled by the
\grammarindent
length. Increasing this value will move the right hand side of the rule further to the right.Both of these lengths can be set using the standard LaTeX
\setlength
command.The
\grammarlabel
command controls how the left hand side of the rule is set relative to the production operator. Its default definition is:where
#1
is the nonterminal and#2
is the production operator.Here's a sample document that plays with the two length values. I've purposefully made the values large so that the effect will be seen. I wouldn't change the definition of
\grammarlabel
if I were you.