# Automatic line break for writing context-free grammars?

I am currently writing context-free grammars in Latex, I have one production in each line, e.g.

S -> a | B
B -> C
C -> D | d


Currently I am using the math mode for this and a manual line break. Is there a better environment for my case? One where I e.g. don't need manual line breaks? Thanks :-)

-
Please add an MWE so we know what you're using at the moment. For this particular case, I'd recommend the amsmath package and the align environment, so \begin{align} S &\to a \mid B \\ B &\to C \\ C &\to D \mid d \end{align}. I don't think you'll get around manual line breaks... You also might want to use align* or an aligned environment inside of an equation environment, depending on your equation numbering needs. –  benwilfut Mar 13 '13 at 15:43

This looks like a job for the listings package, which not only will automatically do your line breaks, but will allow

• the -> arrows to be rendered as proper TeX-ish arrows;
• unicode characters (e.g. greek characters) to be rendered by the TeX-ish mathematical equivalent;
• easy indentation and inline presentation of the grammatical rules;

and other aspects of control of the formatting.

Example Source.

\documentclass{article}
\usepackage{listings}

\lstset{
basicstyle=\itshape,
xleftmargin=3em,
literate={->}{$\rightarrow$}{2}
{α}{$\alpha$}{1}
{δ}{$\delta$}{1}
}

\begin{document}
\noindent Example of a simple grammar:
\begin{lstlisting}
S -> α | B
B -> C
C -> D | δ
\end{lstlisting}
Note that the rules \lstinline{S -> α | B}\,'' and
\lstinline{C -> D | δ}\,'' involve terminals.
\end{document}


The various ways in which the formatting is controlled above are as follows:

• The basicstyle=\itshape key indicates that the characters are to be typeset in italics, as is typical for a mathematics environment.
• The xleftmargin=3em describes a left indentation to apply for a "displayed" listing in the lstlisting environment.
• The literate=... instructions indicate that 'literate' ways to render various sequences of characters. For instance, the two characters -> is with the corresponding arrow, and that for spacing purposes it should be given approximately two characters worth of horizontal space. (listings tries to give each character the same amount of space, even if you do not use a monospaced typeface; the default is about 0.6em.) The other entries describes how to typeset the unicode greek characters.
• It is possible also to use these features inline, using the \lstinline macro.

Result:

-
wow, that's great, thank you! Do you know how I can indent the complete lstlisting block? Is it possible to disable the justification on the | sign? Several | vertically don't look that good, as it's quite close to a line. –  stefan.at.wpf Mar 13 '13 at 20:51
hmm and how would one write an \alpha in the lstlisting? –  stefan.at.wpf Mar 13 '13 at 20:54
found the answers to my questions and edited Niel's post :-) –  stefan.at.wpf Mar 13 '13 at 21:06
@stefan.at.wpf: if you want to avoid the almost-solid-line, you can increase the vertical spacing in the grammar using basicstyle=\itshape\setstretch{1.3} and the setspace package. –  Niel de Beaudrap Mar 14 '13 at 16:02