1 : |
jhr |
97 |
% grammar.sty
|
2 : |
|
|
%
|
3 : |
|
|
% A package for writing grammars.
|
4 : |
|
|
%
|
5 : |
|
|
% Usage:
|
6 : |
|
|
%
|
7 : |
|
|
% Each grammar fragment lives in a Grammar environment:
|
8 : |
|
|
%
|
9 : |
|
|
% \begin{Grammar}
|
10 : |
|
|
% ... rules ...
|
11 : |
|
|
% \end{Grammar}
|
12 : |
|
|
%
|
13 : |
|
|
% The Grammar environment can have an optional argument, which is used in
|
14 : |
|
|
% HTML generation.
|
15 : |
|
|
% Each rule is a collection of productions with the same lhs:
|
16 : |
|
|
%
|
17 : |
|
|
% \begin{Rules}{nonterm}
|
18 : |
|
|
% ... right-hand sides ...
|
19 : |
|
|
% \end{Rules}
|
20 : |
|
|
%
|
21 : |
|
|
% where "nonterm" is the lhs nonterminal. A right-hand side is
|
22 : |
|
|
% formatted with the \RHS{} command. The following commands may
|
23 : |
|
|
% be used inside a \RHS{} command:
|
24 : |
|
|
%
|
25 : |
|
|
% \GRP{item} -- (item)
|
26 : |
|
|
% \OPT{item} -- item?
|
27 : |
|
|
% \OPTGRP{item} -- same as \OPT{\GRP{item}}
|
28 : |
|
|
% \LIST{item} -- item*
|
29 : |
|
|
% \LISTGRP{item} -- same as \LIST{\GRP{item}}
|
30 : |
|
|
% \LISTONE{item} -- item+
|
31 : |
|
|
% \LISTONEGRP{item} -- same as \LISTONE{\GRP{item}}
|
32 : |
|
|
%
|
33 : |
|
|
% To support lexical definitions, we also have the form
|
34 : |
|
|
% \begin{OneOfRule}{nonterm}
|
35 : |
|
|
% ... items ...
|
36 : |
|
|
% \end{OneOfRule}
|
37 : |
|
|
%
|
38 : |
|
|
% where "nonterm" is the lhs nonterminal. The items are formatted with
|
39 : |
|
|
% the \ITEM{item} command.
|
40 : |
|
|
%
|
41 : |
|
|
% This package also defines the following font commands:
|
42 : |
|
|
%
|
43 : |
|
|
% \kw{item} -- for keywords (default is bold \tt)
|
44 : |
|
|
% \nt{item} -- for nonterminals (default is \it)
|
45 : |
|
|
% \term{item} -- for terminal symbols (default is \it)
|
46 : |
|
|
% \sym{item} -- for literal symbols (default is bold \tt)
|
47 : |
|
|
%
|
48 : |
|
|
|
49 : |
|
|
\RequirePackage{ifthen}
|
50 : |
|
|
\RequirePackage{amstext}
|
51 : |
|
|
|
52 : |
|
|
\makeatletter
|
53 : |
|
|
|
54 : |
|
|
\newboolean{G@firstrule}
|
55 : |
|
|
\newcommand{\G@sepsymb}{
|
56 : |
|
|
\ifthenelse{\boolean{G@firstrule}}{$::=$}{$\mid$}}
|
57 : |
|
|
\newcommand{\G@RHS}[2][]{
|
58 : |
|
|
\G@sepsymb{} & {#2} & {#1} \\
|
59 : |
|
|
\setboolean{G@firstrule}{false}%
|
60 : |
|
|
}
|
61 : |
|
|
\newcommand{\G@CONT}[1]{
|
62 : |
|
|
& \hspace*{1em} {#1} & \\
|
63 : |
|
|
}
|
64 : |
|
|
\newcommand{\G@ITEM}[1]{%
|
65 : |
|
|
\mbox{%
|
66 : |
|
|
\ifthenelse{\boolean{G@firstrule}}{$::=$}{$\mid$} #1}
|
67 : |
|
|
\setboolean{G@firstrule}{false}%
|
68 : |
|
|
}
|
69 : |
|
|
|
70 : |
|
|
%
|
71 : |
|
|
% font commands
|
72 : |
|
|
\newcommand{\kw}[1]{\texttt{\bfseries{}#1}}
|
73 : |
|
|
\newcommand{\nt}[1]{\textit{#1}}
|
74 : |
|
|
\newcommand{\term}[1]{\textit{#1}}
|
75 : |
|
|
\newcommand{\sym}[1]{\kw{#1}}
|
76 : |
|
|
|
77 : |
|
|
\newenvironment{Grammar}[1][]{%
|
78 : |
|
|
\begin{list}{}{
|
79 : |
|
|
\setlength{\leftmargin}{4em}%
|
80 : |
|
|
\setlength{\itemindent}{2em}%
|
81 : |
|
|
\setlength{\itemsep}{0em}%
|
82 : |
|
|
\setlength{\parsep}{0em}%
|
83 : |
|
|
}
|
84 : |
|
|
\newcommand{\GRP}[1]{$(\text{##1})$}%
|
85 : |
|
|
\newcommand{\OPT}[1]{$\text{\protect{##1}}^{\textit{opt}}$}%
|
86 : |
|
|
\newcommand{\OPTGRP}[1]{$(\text{##1})^{\textit{opt}}$}%
|
87 : |
|
|
\newcommand{\LIST}[1]{$\text{\protect{##1}}^{*}$}%
|
88 : |
|
|
\newcommand{\LISTGRP}[1]{$(\text{##1})^{*}$}%
|
89 : |
|
|
\newcommand{\LISTONE}[1]{$\text{\protect{##1}}^{+}$}%
|
90 : |
|
|
\newcommand{\LISTONEGRP}[1]{$(\text{##1})^{+}$}%
|
91 : |
|
|
\newenvironment{Rules}[1]{%
|
92 : |
|
|
\begin{samepage}%
|
93 : |
|
|
\setboolean{G@firstrule}{true}%
|
94 : |
|
|
\newcommand{\RHS}{\G@RHS}%
|
95 : |
|
|
\newcommand{\CONT}{\G@CONT}%
|
96 : |
|
|
\item[\nt{##1}]\mbox{}\\
|
97 : |
|
|
\begin{tabular}{clr}%
|
98 : |
|
|
}{%
|
99 : |
|
|
\end{tabular}%
|
100 : |
|
|
\end{samepage}\pagebreak[2]%
|
101 : |
|
|
}
|
102 : |
|
|
\newenvironment{OneOfRule}[1]{%
|
103 : |
|
|
\begin{samepage}%
|
104 : |
|
|
\setboolean{G@firstrule}{true}%
|
105 : |
|
|
\newcommand{\ITEM}{\G@ITEM}%
|
106 : |
|
|
\item[\nt{##1}]\mbox{}\\
|
107 : |
|
|
}{%
|
108 : |
|
|
\end{samepage}\pagebreak[2]%
|
109 : |
|
|
}
|
110 : |
|
|
}{%
|
111 : |
|
|
\end{list}
|
112 : |
|
|
}
|
113 : |
|
|
|
114 : |
|
|
\makeatother
|
115 : |
|
|
|