File: grammar.sty

package info (click to toggle)
haskell98-report 20030706-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,888 kB
  • ctags: 80
  • sloc: haskell: 3,810; makefile: 336; sh: 4
file content (141 lines) | stat: -rw-r--r-- 4,211 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
%%% LaTeX2e package file to set EBNF in the style of the Haskell report
%%%
%%% Manuel M. T. Chakravarty <chak@cse.unsw.edu.au> [1997..2001]
%%%
%%% $Id: grammar.sty,v 1.1 2003/01/14 06:58:33 chak Exp $
%%%
%%% This file is free software; you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation; either version 2 of the License, or
%%% (at your option) any later version.
%%%
%%% This file is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%%% GNU General Public License for more details.
%%%
%%% DOCU ======================================================================
%%%
%%% As a cheap (and ineffective) substitute for proper documentation, here an
%%% example using the macros:
%%%
%%%   \begin{grammar}
%%%     \grule{ftype}{%
%%%       \gnterm{frtype}}
%%%     \gor{%
%%%       \gnterm{fatype} -> \gnterm{ftype}}
%%%     \grule{frtype}{%
%%%       \gnterm{fatype}}
%%%     \gor{%
%%%       ()}
%%%     \grule[$k\geq0$]{fatype}{%
%%%       \gnterm{qtycon} \gnterm[1]{atype} \gellipse\ \gnterm[k]{atype}}
%%%   \end{grammar}
%%%
%%% TODO ======================================================================
%%%
%%% * flexible way of determining the column width is needed (should be the
%%%   same throughout a single grammar (or even the whole document)
%%%

\ProvidesPackage{grammar}[2001/04/17, v0.5a, <chak@cse.unsw.edu.au>]

\RequirePackage{ifthen}

% font configuration
%
% * these two commands can be redefined to change the default fonts for
%   terminals and non-terminals, respectively
%
\newcommand{\gtermfont}{\ttfamily}
\newcommand{\gntermfont}{\rmfamily\itshape}

% remembers whether we are in the star form of the grammar environment
% (internal) 
%
\newif\if@quoted

% grammar environment
%
% * typeset in paragraph mode within a tabular
% * there are four columns, containing the nonterminal, rule symbol (-> or |),
%   the rules rhs, and a comment, respectively
% * the star form of the command is not `quote'd
%
\newenvironment{grammar}{%
  \@quotedtrue
  \@grammar
  }{%
  \@endgrammar
  }
\newenvironment{grammar*}{%
  \@quotedfalse
  \@grammar
  }{%
  \@endgrammar
  }

% internal implementation of the grammar environment
%
\newcommand{\@grammar}{%
  \gtermfont                                    % for terminals
  \if@quoted\quote\fi
  \tabular{@{}lcl@{\quad}l@{}}
  }
\newcommand{\@endgrammar}{%
  \endtabular
  \if@quoted\endquote\fi
  \ignorespaces\global\@ignoretrue
  }

% a rule: optional comment, lhs, and rhs
%
\newcommand{\grule}[3][!*NEVER USED ARGUMENT*!]{%
  \ifthenelse{\equal{#1}{!*NEVER USED ARGUMENT*!}}{%
    \gnterm{#2} &$\rightarrow$& #3 \\
    }{%
    \gnterm{#2} &$\rightarrow$& #3 & \rmfamily(#1)\\
    }%
  }

% a rule alternative: optional comment and rhs
%
\newcommand{\gor}[2][!*NEVER USED ARGUMENT*!]{%
  \ifthenelse{\equal{#1}{!*NEVER USED ARGUMENT*!}}{%
    &$|$& #2\\
    }{%
    &$|$& #2 & \rmfamily(#1)\\
    }%
  }

% set a non-terminal with an optimal subscript
%
\newcommand{\gnterm}[2][!*NEVER USED ARGUMENT*!]{%
  \bgroup
  \gntermfont#2%
  \ifthenelse{\equal{#1}{!*NEVER USED ARGUMENT*!}}\relax{%
    \(_{#1}\)%
    }%
  \egroup
  }

\newcommand{\galt}{$|$}                         % inline alternatives
\newcommand{\gellipse}{\textrm{\ldots}}         % ellipses
\newcommand{\ggroup}[1]{\textrm(#1\textrm)}     % grouped expression
\newcommand{\gopt}[1]{\textrm[#1\textrm]}       % optinal term
\newcommand{\grepeat}[1]{\textrm\{#1\textrm\}}  % repeated term
\newcommand{\gminus}[1]{%                       % set difference
  \(_{\langle\mbox{\scriptsize #1}\rangle}\)}
\newcommand{\gverbal}[1]{{\rmfamily\textbf{#1}}}% informal description

% the following command is for terminals, but it is not necessary with the
% `grammar' environment, as terminals are the default; it is however useful in 
% the rest of the text
%
\newcommand{\gterm}[1]{{\normalfont\gtermfont#1}}

% some characters that are difficult to denote 
%
\newcommand{\gbackslash}{\char"5C}
\newcommand{\gtilde}{\char"7E}
\newcommand{\ghat}{\char"5E}