# Treat grammar as listing

I have a grammar in my document using the package syntax like described in the answer to How to type a Backus-Naur form in LaTeX.

Now I want it to be treated like the listings I have in my document. This means I want to add a label, a caption (that says "Listing 42" in the beginning) and it should appear in \lstlistoflistings. Is there a way to achive this?

A strongly simplified example of my document so far:

\documentclass[12pt,tablecaptionabove]{scrartcl}
\usepackage[final]{listings}
\usepackage[nounderscore]{syntax}
\begin{document}
\lstlistoflistings

some Text

\begin{lstlisting}[caption={A listing},label=myListing]
myListingCode
\end{lstlisting}

some Text

\begin{grammar}
<S> ::= someTerminal
\end{grammar}
\end{document}

-
–  Claudio Fiandrino Feb 4 '13 at 19:42

You can define a new environment that calls the caption type to be the same as for a lstlisting, with the help of the caption package:

\documentclass[12pt,tablecaptionabove]{scrartcl}
\usepackage[final]{listings}
\usepackage[nounderscore]{syntax}
\usepackage{caption}

\usepackage{lipsum} % just to provide mock text

\newenvironment{BNF}
{\captionsetup{type=lstlisting}}
{}

\begin{document}
\lstlistoflistings

\section{A section}

\lipsum[1]

\begin{lstlisting}[caption={A listing},label=myListing]
myListingCode
\end{lstlisting}

\lipsum[2]

\begin{BNF}
\caption{A grammar}
\begin{grammar}
<S> ::= someTerminal
\end{grammar}
\end{BNF}

\lipsum[3]

\end{document}


-