47

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.

Alan Munn
  • 218,180
roxrook
  • 9,907
  • Personally, I would use listings package with some manual settings. – Leo Liu Aug 04 '11 at 10:42
  • Quick texdoc bnf revealed that there's also a ConTeXt module for that (just in case you are interested;)). – mbork Aug 04 '11 at 12:33
  • The highlight tool by Andre Simon can export to LaTeX. And I see in its documentation that it supports ABNF (http://www.andre-simon.de/doku/highlight/en/highlight_langs.html). I hope there would be a package using it as a back-end (as is minted to pygments) – John Kirollos Aug 04 '11 at 13:17
  • 2
    Are you sure you're looking at the right documentation? The documentation for syntax package (part of mdwtools) is 38 pages, which is fairly substantial. There are three lengths that control spacing within the grammars themselves. Perhaps you could post an example of what you've tried and what doesn't work. – Alan Munn Aug 04 '11 at 13:39
  • @Alan Munn: First of all, thanks for your reply. Yes, I looked at that document 38 pages. As you pointed out, there are three lengths that control spacing but no example is given. I wonder how could I know exactly what syntax is used. – roxrook Aug 05 '11 at 01:44
  • I've added a sample document that shows how to change the parameters. Do you have a particular format that you're trying to achieve? – Alan Munn Aug 05 '11 at 03:37
  • See also http://tex.stackexchange.com/questions/4455/how-to-type-a-backus-naur-form-in-latex – knut Aug 30 '12 at 20:20

4 Answers4

52

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> =' &lt;expr&gt; \altfor' <ident> =' &lt;expr&gt;to' <expr> do' &lt;statement&gt; \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> =' &lt;expr&gt; \altfor' <ident> =' &lt;expr&gt;to' <expr> do' &lt;statement&gt; \alt{' <stat-list> `}' \alt <empty>

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

\end{grammar} \end{document}

output of code

yegor256
  • 12,021
Alan Munn
  • 218,180
  • Many thanks for your help. I really appreciated it. As you can see, I'm a novice so I honestly didn't know that the three length options can be set with \setlength(). By the way, the mentioned the lhs must be of the form <xxx>, I wonder is there a way to get rid of <>. Once again, thank you. – roxrook Aug 05 '11 at 04:39
  • By the way, besides those two packages, do you know any other alternatives? – roxrook Aug 05 '11 at 04:59
  • The third length isn't a length, its a macro, and needs to be changed with \renewcommand (But I wouldn't recommend changing it.) For the delimeters, see section 1.7 of the manual. You can change the non-terminal < and > with \renewcommand{\syntleft}{} and \renewcommand{\syntright}{}, respectively. (I don't know any alternative packages.) – Alan Munn Aug 05 '11 at 05:15
  • 2
    I wonder is there a way to indent the production line, so it can be center aligned? – roxrook Aug 21 '11 at 00:00
  • How to specify repition? A plain * is typeset as superscript, but + is not. A workaround would be to replace + by $^+$ but is there a proper way? – user905686 Aug 25 '16 at 12:04
  • My < appear as ¡ and my > appear as ¿. – user4417 Jan 18 '19 at 16:03
  • 1
    @Alan Munn is there any way we can get rid of certain angled brackets? For example, those around the word 'statement' ? – user65526 May 28 '20 at 10:50
  • 1
    @user65526 See the third comment. – Alan Munn May 28 '20 at 11:55
  • @AlanMunn \renewcommand{\syntleft}{} got rid of the brackets in the result, but the brackets are still required when typing, is there a way to remove that requirement as well? (for example, just typing a ::= b + c) – Jack Jan 27 '21 at 04:46
18

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).

knut
  • 8,838
16

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

9999years
  • 433
  • 2
  • 12
knut
  • 8,838
3

The simplebnf package on CTAN parses BNF expressions:

\begin{bnf}
$\tau$ : \textsf{Type} ::=
| \texttt{num} : numbers
| \texttt{str} : strings
;;
$e$ : \textsf{Expr} ::=
| $x$ : variable
| $n$ : numeral
| \texttt{$e$ + $e$} : addition
| \texttt{$e$ * $e$} : multiplication
| \texttt{$e$ \textasciicircum{} $e$} : concatenation
| \texttt{len($e$)} : length
| \texttt{let $x$ = $e_1$ in $e_2$} : definition
\end{bnf}

simplebnf sample render

This package provides a simple way for typesetting grammars in Backus-Naur form (BNF). It features a flexible configuration system, allowing for the customization of the domain-specific language (DSL) used in typesetting the grammar. Additionally, the package comes with sensible defaults.

Disclaimer: I'm the author of the package.

Jay Lee
  • 353
  • 1
  • 12
  • It didn't work for me in overleaf. – Bishnu Bhusal Mar 22 '24 at 14:45
  • @BishnuBhusal I’m sorry to hear that it didn’t work. Overleaf is probably using a (very) old version of my package, so you should either download the package from CTAN and put it in the same directory, or you can work with a local installation of the most recent TeX Live distribution (or whichever distribution you would like to use). – Jay Lee Mar 24 '24 at 06:44