Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to represent a grammar where rules are numbered (for example, the following grammar: http://en.wikipedia.org/wiki/LR_parser#Additional_Example_1.2B1), in order to refer to those rules later in an action table. However, I don't find any way to do this properly. Ideally, the rules numbers would be right-aligned, like in a alignat environment.

Currently, I simply put the numbers directly in the grammar, and the output is not really readable.

My code:

\documentclass[11pt]{article}
\usepackage{syntax}

\begin{document}

\setlength{\grammarparsep}{20pt plus 1pt minus 1pt}
\setlength{\grammarindent}{12em}
\begin{grammar}
<E> ::= <E> \lit{*} <B>    (1)
    \alt <E> \lit{+} <B>   (2)
    \alt <B>               (3)

<B> ::= \lit{0}            (4)
    \alt \lit{1}           (5)
\end{grammar}

\end{document}

(note that the E and B were between 'less than' and 'greater than' symbols, I removed them from the listing because they seem to badly interact with stackexchange).

share|improve this question
    
Welcome to TeX.sx! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). About the E and B, please edit your code now, there should be no problem with those symbols any more. –  egreg Mar 16 '13 at 17:44
    
Edited, thanks! –  acieroid Mar 16 '13 at 17:51

1 Answer 1

up vote 4 down vote accepted

This should work for many cases; I'm no expert in these kinds of grammars.

I suggest you to define a new environment, patching the grammar environment to add line numbers.

\documentclass[11pt]{article}
\usepackage[pass,showframe]{geometry} % just to show the margins

\usepackage{syntax}

\usepackage{etoolbox} % for patching
\makeatletter
% define the main command on the model of the original one
% we add stepping the counter and typesetting the number
\def\gr@implnumbereditem<#1> #2 {%
  \stepcounter{grammarline}%
  \sbox\z@{\hskip\labelsep\grammarlabel{#1}{#2}}
  \strut\@@par%
  \vskip-\parskip%
  \vskip-\baselineskip%
  \hrule\@height\z@\@depth\z@\relax%
  \item[%
    \rlap{\hskip\dimexpr\linewidth+\grammarindent\relax %% add the number
          \llap{(\thegrammarline)}}%
    \unhbox\z@]%
  \catcode`\<\active%
}
% copy the grammar environment under a new name
\let\numberedgrammar\grammar
\let\endnumberedgrammar\endgrammar
% now patch the new environment
\pretocmd\numberedgrammar{\setcounter{grammarline}{0}}{}{}
\patchcmd\numberedgrammar
  {\gr@implitem}
  {\gr@implnumbereditem}
  {}{}
\patchcmd\numberedgrammar
  {\def\alt{\\\llap{\textbar\quad}}}
  {\let\alt\alt@num}
  {}{}

% the command for numbering the \alt lines
\def\alt@num{\\\relax
  \stepcounter{grammarline}%
  \rlap{\hskip\dimexpr\linewidth-\labelwidth+\grammarindent-\labelsep\relax
        \llap{(\thegrammarline)}}% add the number
  \llap{\textbar\quad}}

\newcounter{grammarline}
\makeatother

\begin{document}

\setlength{\grammarparsep}{20pt plus 1pt minus 1pt}
\setlength{\grammarindent}{5em}
Some text above
\begin{numberedgrammar}
<E> ::= <E> \lit{*} <B>
  \alt <E> \lit{+} <B>
  \alt <B>

<B> ::= \lit{0}
  \alt \lit{1}
\end{numberedgrammar}
Some text below
\begin{enumerate}
\item a
\begin{numberedgrammar}
<E> ::= <E> \lit{*} <B>
  \alt <E> \lit{+} <B>
  \alt <B>

<B> ::= \lit{0}
  \alt \lit{1}
\end{numberedgrammar}
\end{enumerate}
\end{document}

enter image description here

share|improve this answer
    
Works perfectly, thanks! –  acieroid Mar 16 '13 at 21:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.