yacc-parser.mly 11.5 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 General Public License version 2, as described in the    */
/*  file LICENSE.                                                             */
/*                                                                            */
/******************************************************************************/

14 15
/* This is the crude version of the parser. It is meant to be processed
   by ocamlyacc. Its existence is necessary for bootstrapping. It is kept
16
   in sync with [fancy-parser], with a few differences:
POTTIER Francois's avatar
POTTIER Francois committed
17
   0. [yacc-parser] produces dummy position information;
18 19
   1. [fancy-parser] exploits many features of Menhir;
   2. [fancy-parser] performs slightly more refined error handling;
POTTIER Francois's avatar
POTTIER Francois committed
20 21
   3. [fancy-parser] supports anonymous rules.
   4. [fancy-parser] supports the new rule syntax. */
22 23 24 25 26 27 28 29

%{

open Syntax
open Positions

%}

30
%token TOKEN TYPE LEFT RIGHT NONASSOC START PREC PUBLIC COLON BAR EOF EQUAL
31
%token INLINE LPAREN RPAREN COMMA QUESTION STAR PLUS PARAMETER ON_ERROR_REDUCE
32
%token PERCENTATTRIBUTE SEMI
33
%token <string Positions.located> LID UID QID
34 35
%token <Stretch.t> HEADER
%token <Stretch.ocamltype> OCAMLTYPE
36
%token <Stretch.t Lazy.t> PERCENTPERCENT
POTTIER Francois's avatar
POTTIER Francois committed
37
%token <Syntax.raw_action> ACTION
38
%token <Syntax.attribute> ATTRIBUTE GRAMMARATTRIBUTE
POTTIER Francois's avatar
POTTIER Francois committed
39 40
/* For the new rule syntax: */
%token LET TILDE UNDERSCORE COLONEQUAL EQUALEQUAL
41
%start grammar
42 43
%type <ParserAux.early_producer> producer
%type <ParserAux.early_production> production
44
%type <Syntax.partial_grammar> grammar
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

/* These declarations solve a shift-reduce conflict in favor of
   shifting: when the declaration of a non-terminal symbol begins with
   a leading bar, it is understood as an (insignificant) leading
   optional bar, *not* as an empty right-hand side followed by a bar.
   This ambiguity arises due to the existence of a new notation for
   letting several productions share a single semantic action. */

%nonassoc no_optional_bar
%nonassoc BAR

%%

/* ------------------------------------------------------------------------- */
/* A grammar consists of declarations and rules, followed by an optional
60
   postlude, which we do not parse. */
61 62

grammar:
63
  declarations PERCENTPERCENT rules postlude
64 65
    {
      {
66 67 68
        pg_filename          = ""; (* filled in by the caller *)
        pg_declarations      = List.rev $1;
        pg_rules             = $3;
69
        pg_postlude          = $4
70 71 72
      }
    }

73
postlude:
74 75
  EOF
    { None }
76
| PERCENTPERCENT /* followed by actual postlude */
77 78 79
    { Some (Lazy.force $1) }

/* ------------------------------------------------------------------------- */
80
/* A declaration is an %{ OCaml header %}, or a %token, %start,
81 82 83 84 85 86 87
   %type, %left, %right, or %nonassoc declaration. */

declarations:
  /* epsilon */
    { [] }
| declarations declaration
    { $2 @ $1 }
88 89
| declarations SEMI
    { $1 }
90 91 92 93 94 95

declaration:
| HEADER /* lexically delimited by %{ ... %} */
    { [ unknown_pos (DCode $1) ] }

| TOKEN optional_ocamltype terminals
96 97 98 99
    { let ty, ts = $2, $3 in
      List.map (Positions.map (fun (terminal, alias, attrs) ->
        DToken (ty, terminal, alias, attrs)
      )) ts }
100 101 102 103

| START nonterminals
    { List.map (Positions.map (fun nonterminal -> DStart nonterminal)) $2 }

104
| TYPE OCAMLTYPE actuals
105 106 107 108 109
    { List.map (Positions.map (fun nt -> DType ($2, nt)))
        (List.map Parameters.with_pos $3) }

| START OCAMLTYPE nonterminals
    /* %start <ocamltype> foo is syntactic sugar for %start foo %type <ocamltype> foo */
110
    { Misc.mapd (fun ntloc ->
111 112 113
        Positions.mapd (fun nt -> DStart nt, DType ($2, ParameterVar ntloc)) ntloc) $3 }

| priority_keyword symbols
114
    { let prec = ParserAux.new_precedence_level (rhs_start_pos 1, rhs_end_pos 1) in
115 116 117 118 119
      List.map (Positions.map (fun symbol -> DTokenProperties (symbol, $1, prec))) $2 }

| PARAMETER OCAMLTYPE
    { [ unknown_pos (DParameter $2) ] }

120 121 122 123 124 125
| GRAMMARATTRIBUTE
    { [ unknown_pos (DGrammarAttribute $1) ] }

| PERCENTATTRIBUTE actuals attributes
    { [ unknown_pos (DSymbolAttributes ($2, $3)) ] }

126
| ON_ERROR_REDUCE actuals
127 128
    { let prec = ParserAux.new_on_error_reduce_level() in
      List.map (Positions.map (fun nt -> DOnErrorReduce (nt, prec)))
129 130
        (List.map Parameters.with_pos $2) }

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
optional_ocamltype:
  /* epsilon */
    { None }
| OCAMLTYPE /* lexically delimited by angle brackets */
    { Some $1 }

priority_keyword:
  LEFT
    { LeftAssoc }
| RIGHT
    { RightAssoc }
| NONASSOC
    { NonAssoc }

/* ------------------------------------------------------------------------- */
146 147 148 149 150 151 152 153 154 155
/* A symbol is a terminal or nonterminal symbol. */

/* One would like to require nonterminal symbols to begin with a lowercase
   letter, so as to lexically distinguish them from terminal symbols, which
   must begin with an uppercase letter. However, for compatibility with
   ocamlyacc, this is impossible. It can be required only for nonterminal
   symbols that are also start symbols. */

/* We also accept token aliases in place of ordinary terminal symbols.
   Token aliases are quoted strings. */
156 157 158 159 160 161 162 163 164 165 166 167

symbols:
  /* epsilon */
    { [] }
| symbols optional_comma symbol
    { $3 :: $1 }

symbol:
  LID
    { $1 }
| UID
    { $1 }
168 169
| QID
    { $1 }
170 171 172 173 174 175 176

optional_comma:
  /* epsilon */
    { () }
| COMMA
    { () }

177 178 179 180 181
attributes:
  /* epsilon */
    { [] }
| ATTRIBUTE attributes { $1 :: $2 }

182 183 184 185 186 187 188
/* ------------------------------------------------------------------------- */
/* Terminals must begin with an uppercase letter. Nonterminals that are
   declared to be start symbols must begin with a lowercase letter. */

terminals:
  /* epsilon */
    { [] }
189 190 191 192
| terminals optional_comma UID optional_alias attributes
    { let ts, uid, alias, attrs = $1, $3, $4, $5 in
      let alias = Option.map Positions.value alias in
      Positions.map (fun uid -> uid, alias, attrs) uid :: ts }
193 194 195 196 197 198 199

nonterminals:
  /* epsilon */
    { [] }
| nonterminals LID
    { $2 :: $1 }

200 201 202 203 204 205
optional_alias:
  /* epsilon */
    { None }
| QID
    { Some $1 }

206 207 208 209 210 211 212 213 214 215
/* ------------------------------------------------------------------------- */
/* A rule defines a symbol. It is optionally declared %public, and optionally
   carries a number of formal parameters. The right-hand side of the definition
   consists of a list of production groups. */

rules:
  /* epsilon */
    { [] }
| rules rule
    { $2 :: $1 }
216 217
| rules SEMI
    { $1 }
218 219 220 221

rule:
  flags
  symbol
222
  attributes
223
  optional_formal_parameters
224
  COLON
225 226
  optional_bar
  production_group production_groups
227
    {
228
      let public, inline = $1 in
229
      { pr_public_flag = public;
230 231 232
        pr_inline_flag = inline;
        pr_nt          = Positions.value $2;
        pr_positions   = [ Positions.position $2 ];
233 234 235
        pr_attributes  = $3;
        pr_parameters  = $4;
        pr_branches    = List.flatten ($7 :: List.rev $8)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      }
    }

flags:
  /* epsilon */
    { false, false }
| PUBLIC
    { true, false }
| INLINE
    { false, true }
| PUBLIC INLINE
    { true, true }
| INLINE PUBLIC
    { true, true }

/* ------------------------------------------------------------------------- */
/* Parameters are surroundered with parentheses and delimited by commas.
   The syntax of actual parameters allows applications, whereas the syntax
   of formal parameters does not. It also allows use of the "?", "+", and
   "*" shortcuts. */

optional_formal_parameters:
  /* epsilon */
    { [] }
| LPAREN formal_parameters RPAREN
    { $2 }

formal_parameters:
  symbol
    { [ Positions.value $1 ] }
| symbol COMMA formal_parameters
    { Positions.value $1 :: $3 }

269
optional_actuals:
270 271
  /* epsilon */
    { [] }
272
| LPAREN actuals_comma RPAREN
273 274
    { $2 }

275
actuals_comma:
276
  actual
277
    { [ $1 ] }
278
| actual COMMA actuals_comma
279 280
    { $1 :: $3 }

281 282
actual:
  symbol optional_actuals
283
    { Parameters.app $1 $2 }
284
| actual modifier
285
    { ParameterApp ($2, [ $1 ]) }
286

287
actuals:
288 289
  /* epsilon */
    { [] }
290
| actuals optional_comma actual
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    { $3::$1 }

optional_bar:
  /* epsilon */ %prec no_optional_bar
    { () }
| BAR
    { () }

/* ------------------------------------------------------------------------- */
/* The "?", "+", and "*" modifiers are short-hands for applications of
   certain parameterized nonterminals, defined in the standard library. */

modifier:
  QUESTION
    { unknown_pos "option" }
| PLUS
    { unknown_pos "nonempty_list" }
| STAR
    { unknown_pos "list" }

/* ------------------------------------------------------------------------- */
/* A production group consists of a list of productions, followed by a
   semantic action and an optional precedence specification. */

production_groups:
  /* epsilon */
    { [] }
| production_groups BAR production_group
    { $3 :: $1 }

production_group:
  productions ACTION /* action is lexically delimited by braces */ optional_precedence
323
    {
324
      let productions, action, oprec2 = $1, $2, $3 in
325 326 327 328
      (* If multiple productions share a single semantic action, check
         that all of them bind the same names. *)
      ParserAux.check_production_group productions;
      (* Then, *)
329
      List.map (fun (producers, oprec1, level, pos) ->
330 331 332 333
        (* Replace [$i] with [_i]. *)
        let pr_producers = ParserAux.normalize_producers producers in
        (* Distribute the semantic action. Also, check that every [$i]
           is within bounds. *)
334 335
        let names = ParserAux.producer_names producers in
        let pr_action = action Settings.dollars names in
336 337 338 339 340 341 342
        {
          pr_producers;
          pr_action;
          pr_branch_prec_annotation   = ParserAux.override pos oprec1 oprec2;
          pr_branch_production_level  = level;
          pr_branch_position          = pos
        })
343
      productions
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    }

optional_precedence:
  /* epsilon */
    { None }
| PREC symbol
    { Some $2 }

/* ------------------------------------------------------------------------- */
/* A production is a list of producers, optionally followed by a
   precedence declaration. Lists of productions are nonempty and
   separated with bars. */

productions:
  production
    { [ $1 ] }
| production bar_productions
    { $1 :: $2 }

bar_productions:
  BAR production
    { [ $2 ] }
| BAR production bar_productions
    { $2 :: $3 }

production:
  producers optional_precedence
    { List.rev $1,
      $2,
373
      ParserAux.new_production_level(),
374
      Positions.import (symbol_start_pos(), symbol_end_pos())
375 376 377
    }

producers:
378
  /* epsilon */
379 380 381 382 383 384
    { [] }
| producers producer
    { $2 :: $1 }

/* ------------------------------------------------------------------------- */
/* A producer is an actual parameter, possibly preceded by a
385
   binding, and possibly followed with attributes. */
386 387

producer:
388
|           actual attributes optional_semis
389
    { Positions.import (symbol_start_pos(), symbol_end_pos()),    None, $1, $2 }
390
| LID EQUAL actual attributes optional_semis
391
    { Positions.import (symbol_start_pos(), symbol_end_pos()), Some $1, $3, $4 }
392

393 394 395 396 397 398 399 400
/* ------------------------------------------------------------------------- */
/* Semicolons used to be considered whitespace by our lexer, but are no longer.
   We must allow optional semicolons in a few conventional places. */

optional_semis:
  /* empty */         { () }
| optional_semis SEMI { () }

401
%%