How to use caption and label in grammar environment?

I can't manage to add a caption and a label to my grammar environment:

\documentclass{article}

\usepackage{syntax}
\grammarindent 80pt

\begin{document}

\begin{grammar}
<condition> ::= "$" <element> "$ = " <value>

<element> ::= <field> \alt <property>
\end{grammar}

\end{document}

-

The {grammar} environment is not a floating environment and thus can't have a caption and cannot be referred to via \label and \ref. From what I can see the syntax package doesn't provide a floating equivalent, either.

What you can do, though, is define your own floating environment. There are a number of packages that can help with this task, one of them being newfloat (which plays nicely together with the caption package - it's from the same author).

\documentclass{article}
\usepackage{newfloat}
\usepackage{syntax}

% declare the floating environment {Grammar}
% this will also define \listofGrammars:
\DeclareFloatingEnvironment[
% the file extension for the file used to create the list:
fileext   = logr,% don't use log here!
% the heading for the list:
listname  = {List of Grammars},
% the name used in captions:
name      = Grammar,
% the default floating parameters if the environment is used
% without optional argument:
placement = htp
]{Grammar}

\grammarindent=80pt

\begin{document}

\begin{Grammar}
\begin{grammar}
<condition> ::= "$" <element> "$ = " <value>

<element> ::= <field> \alt <property>
\end{grammar}
\caption{A sample caption}\label{gra:sample}
\end{Grammar}

See grammar~\ref{gra:sample} ...

\end{document}


-