Which package can be used to write BNF grammars?

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


Perhaps you find an alternative in Syntax diagrams (or railroad diagrams). They represent a graphical alternative to Backus–Naur Form.

For LaTeX you can use the rail-package. It contains a LaTeX-style and a programm (compiled exe or the C-Sources). An example of a document with the package: example for rail-diagramm

Remark: The rail-package contains an old exe. If you have problems to use it, there are German hints how to compile the sources on Windows 7 (64-Bit). The results (adapted makefile and the compiled version) are also available on the website: http://www.karsten-brodmann.de/downloads/Rail.zip.


There is also syngen A tool for generating syntax diagrams from BNF (I have no experience with the package).


There is again a new package: simplebnf


And another one: syntaxdi (Using TikZ-Styles. Description in German).


The backnaur package on CTAN works well for this.

The package typesets Backus-Naur Form (BNF) definitions. It creates aligned lists of productions, with numbers if required. It can also print in-line BNF expressions using math mode.

This snippet (from the documentation):

\documentclass{article}
\usepackage{backnaur}
\begin{document}
\begin{bnf*}
  \bnfprod{list}
    {\bnfpn{listitems} \bnfor \bnfes}\\
  \bnfprod{listitems}
    {\bnfpn{item} \bnfor \bnfpn{item}
     \bnfsp \bnfts{;} \bnfsp \bnfpn{listitems}}\\
  \bnfprod{item}
    {\bnftd{description of item}}
\end{bnf*}
\end{document}

renders like this:

BNF example