standard.mly 7.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/******************************************************************************/
/*                                                                            */
/*                                   Menhir                                   */
/*                                                                            */
/*                       François Pottier, Inria Paris                        */
/*              Yann Régis-Gianas, PPS, Université Paris Diderot              */
/*                                                                            */
/*  Copyright Inria. All rights reserved. This file is distributed under the  */
/*  terms of the GNU Library General Public License version 2, with a         */
/*  special exception on linking, as described in the file LICENSE.           */
/*                                                                            */
/******************************************************************************/

14 15 16 17 18 19
(* This is menhir's standard library. It offers a number of
   parameterized nonterminal definitions, such as options and lists,
   that should be useful in a number of circumstances. *)

%%

20 21 22
(* ------------------------------------------------------------------------- *)
(* The identity. *)

23
(* [endrule(X)] is the same as [X]. *)
24

25
(* This allows placing an anonymous subrule in the middle of a rule, as in:
26

27 28 29
     cat
     endrule(dog { action1 })
     cow
30 31
     { action2 }

32
   Because [endrule] is marked %inline, everything is expanded away. So,
33 34
   this is equivalent to:

35
     cat dog cow { action1; action2 }
36

37
   Note that [action1] moves to the end of the rule. The anonymous subrule
38 39
   can even have several branches, as in:

40 41 42
     cat
     endrule(dog { action1a } | fox { action1b })
     cow
43 44 45 46
     { action2 }

   This is expanded to:

47 48
     cat dog cow { action1a; action2 }
   | cat fox cow { action1b; action2 }
49 50

*)
51

52 53 54 55 56 57 58
%public %inline endrule(X):
x = X
    { x }

(* [anonymous(X)] is a deprecated synonym for [endrule(X)].
   It was never documented. *)

59
%public %inline anonymous(X):
60 61 62
x = X
    { x }

63
(* [midrule(X)] is the same as [X]. *)
64

65
(* This allows placing an anonymous subrule in the middle of a rule, as in:
66

67 68 69
     cat
     midrule(dog { action1 })
     cow
70 71
     { action2 }

72
   Because [midrule] is not marked %inline, this is equivalent to:
73

74
     cat xxx cow { action2 }
75

76
   where the fresh nonterminal symbol [xxx] is separately defined by:
77

78
     xxx: dog { action1 }
79

80
   In particular, if there is no [dog], what we get is a semantic action
81
   embedded in the middle of a rule. For instance,
82

83
     cat midrule({ action1 }) cow { action2 }
84 85 86

   is equivalent to:

87
     cat xxx cow { action2 }
88 89 90 91 92 93 94

   where [xxx] is separately defined by the rule:

     xxx: { action1 }

*)

95 96 97 98 99 100 101
%public midrule(X):
x = X
    { x }

(* [embedded(X)] is a deprecated synonym for [midrule(X)].
   It was never documented. *)

102 103
%public embedded(X):
x = X
104 105
    { x }

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 142 143 144 145 146 147 148
(* ------------------------------------------------------------------------- *)
(* Options. *)

(* [option(X)] recognizes either nothing or [X]. It produces a value
   of type ['a option] if [X] produces a value of type ['a]. *)

%public option(X):
  /* nothing */
    { None }
| x = X
    { Some x }

(* [ioption(X)] is identical to [option(X)], except its definition is
   inlined. This has the effect of duplicating the production that
   refers to it, possibly eliminating an LR(1) conflict. *)

%public %inline ioption(X):
  /* nothing */
    { None }
| x = X
    { Some x }

(* [boption(X)] recognizes either nothing or [X]. It produces a value
   of type [bool]. *)

%public boption(X):
  /* nothing */
    { false }
| X
    { true }

(* [loption(X)] recognizes either nothing or [X]. It produces a value
   of type ['a list] if [X] produces a value of type ['a list]. *)

%public loption(X):
  /* nothing */
    { [] }
| x = X
    { x }

(* ------------------------------------------------------------------------- *)
(* Sequences. *)

149
(* [epsilon] recognizes the empty word. It can be used instead of the
150 151 152 153
   traditional /* empty */ comment. *)

(* NOT YET ADDED because we first need to remove the limitation that
   every symbol must be reachable from the start symbol!
154 155 156 157 158

%public %inline epsilon:
  /* empty */
    { () }

159 160
 *)

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
(* [pair(X, Y)] recognizes the sequence [X Y]. It produces a value of
   type ['a * 'b] if [X] and [Y] produce values of type ['a] and ['b],
   respectively. *)

%public %inline pair(X, Y):
  x = X; y = Y
    { (x, y) }

(* [separated_pair(X, sep, Y)] recognizes the sequence [X sep Y]. It
   produces a value of type ['a * 'b] if [X] and [Y] produce values of
   type ['a] and ['b], respectively. *)

%public %inline separated_pair(X, sep, Y):
  x = X; sep; y = Y
    { (x, y) }

(* [preceded(opening, X)] recognizes the sequence [opening X]. It
   passes on the value produced by [X], so that it produces a value of
   type ['a] if [X] produces a value of type ['a]. *)

%public %inline preceded(opening, X):
  opening; x = X
    { x }

(* [terminated(X, closing)] recognizes the sequence [X closing]. It
   passes on the value produced by [X], so that it produces a value of
   type ['a] if [X] produces a value of type ['a]. *)

%public %inline terminated(X, closing):
  x = X; closing
    { x }

(* [delimited(opening, X, closing)] recognizes the sequence [opening X
   closing]. It passes on the value produced by [X], so that it
   produces a value of type ['a] if [X] produces a value of type
   ['a]. *)

%public %inline delimited(opening, X, closing):
  opening; x = X; closing
    { x }

(* ------------------------------------------------------------------------- *)
(* Lists. *)

(* [list(X)] recognizes a possibly empty list of [X]'s. It produces a
   value of type ['a list] if [X] produces a value of type ['a]. The
   front element of the list is the first element that was parsed. *)

%public list(X):
  /* nothing */
    { [] }
| x = X; xs = list(X)
    { x :: xs }

(* [nonempty_list(X)] recognizes a nonempty list of [X]'s. It produces
   a value of type ['a list] if [X] produces a value of type ['a]. The
   front element of the list is the first element that was parsed. *)

%public nonempty_list(X):
  x = X
    { [ x ] }
| x = X; xs = nonempty_list(X)
    { x :: xs }

(* [separated_list(separator, X)] recognizes a possibly empty list of
   [X]'s, separated with [separator]'s. It produces a value of type
   ['a list] if [X] produces a value of type ['a]. The front element
   of the list is the first element that was parsed. *)

%public %inline separated_list(separator, X):
  xs = loption(separated_nonempty_list(separator, X))
    { xs }

(* [separated_nonempty_list(separator, X)] recognizes a nonempty list
   of [X]'s, separated with [separator]'s. It produces a value of type
   ['a list] if [X] produces a value of type ['a]. The front element
   of the list is the first element that was parsed. *)

%public separated_nonempty_list(separator, X):
  x = X
    { [ x ] }
| x = X; separator; xs = separated_nonempty_list(separator, X)
    { x :: xs }

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
(* ------------------------------------------------------------------------- *)
(* List manipulation and transformation. *)

(* [rev(XS)] recognizes the same language as [XS], but reverses the resulting
   OCaml list. (20181005) *)

%public %inline rev(XS):
  xs = XS
    { List.rev xs }

(* [flatten(XSS)] recognizes the same language as [XSS], and flattens the
   resulting OCaml list of lists. (20181005) *)

%public %inline flatten(XSS):
  xss = XSS
    { List.flatten xss }

(* [append(XS, YS)] recognizes [XS YS], and appends (concatenates) the
   resulting OCaml lists. (20181005) *)

%public %inline append(XS, YS):
  xs = XS ys = YS
    { xs @ ys }

269
%%