[Tex/LaTex] Which package can be used to write BNF grammars

grammarpackagessyntax

I googled and found two packages:

  1. mdwtools
  2. 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 the mdwtools 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 to 8pt 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:

\newcommand{\grammarlabel}[2]{\synt{#1}\hfill#2}

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.

\documentclass{article}
\usepackage{syntax}

\begin{document}
\paragraph{Default settings}
\begin{grammar}

<statement> ::= <ident> `=' <expr> 
\alt `for' <ident> `=' <expr> `to' <expr> `do' <statement> 
\alt `{' <stat-list> `}' 
\alt <empty> 

<stat-list> ::= <statement> `;' <stat-list> | <statement> 

\end{grammar}
\paragraph{Increase the two lengths}
\setlength{\grammarparsep}{20pt plus 1pt minus 1pt} % increase separation between rules
\setlength{\grammarindent}{12em} % increase separation between LHS/RHS 

\begin{grammar}

<statement> ::= <ident> `=' <expr> 
\alt `for' <ident> `=' <expr> `to' <expr> `do' <statement> 
\alt `{' <stat-list> `}' 
\alt <empty> 

<stat-list> ::= <statement> `;' <stat-list> | <statement> 

\end{grammar}
\end{document}

output of code

Related Question