grammar.ml 30.7 KB
Newer Older
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
open UnparameterizedSyntax
open Syntax
open Stretch
open Positions

(* ------------------------------------------------------------------------ *)
(* Precedence levels for tokens or pseudo-tokens alike. *)

module TokPrecedence = struct

  (* This set records, on a token by token basis, whether the token's
     precedence level is ever useful. This allows emitting warnings
     about useless precedence declarations. *)

  let ever_useful : StringSet.t ref =
    ref StringSet.empty

  let use id =
    ever_useful := StringSet.add id !ever_useful

  (* This function is invoked when someone wants to consult a token's
     precedence level. This does not yet mean that this level is
     useful, though. Indeed, if it is subsequently compared against
     [UndefinedPrecedence], it will not allow solving a conflict. So,
     in addition to the desired precedence level, we return a delayed
     computation which, when evaluated, records that this precedence
     level was useful. *)

  let levelip id properties =
    lazy (use id), properties.tk_priority

  let leveli id = 
    let properties =
      try
	StringMap.find id Front.grammar.tokens
      with Not_found ->
	assert false (* well-formedness check has been performed earlier *)
    in
    levelip id properties    

  (* This function is invoked after the automaton has been constructed.
     It warns about unused precedence levels. *)

  let diagnostics () =
    StringMap.iter (fun id properties ->
      if not (StringSet.mem id !ever_useful) then
	match properties.tk_priority with
	| UndefinedPrecedence ->
	    ()
	| PrecedenceLevel (_, _, pos1, pos2) ->
	    Error.grammar_warning (Positions.two pos1 pos2)
	      (Printf.sprintf "the precedence level assigned to %s is never useful." id)
    ) Front.grammar.tokens

end

(* ------------------------------------------------------------------------ *)
(* Nonterminals. *)

module Nonterminal = struct

  type t = int

  let n2i i = i

  let compare = (-)

  (* Determine how many nonterminals we have and build mappings
     both ways between names and indices. A new nonterminal is
     created for every start symbol. *)

  let new_start_nonterminals =
    StringSet.fold (fun symbol ss -> (symbol ^ "'") :: ss) Front.grammar.start_symbols []

  let original_nonterminals =
    StringMap.fold (fun nt _ rules -> nt :: rules) Front.grammar.rules []
  
  let start =
    List.length new_start_nonterminals

  let (n : int), (name : string array), (map : int StringMap.t) =
    Misc.index (new_start_nonterminals @ original_nonterminals)

  let () =
    Error.logG 1 (fun f ->
      Printf.fprintf f
	"Grammar has %d nonterminal symbols, among which %d start symbols.\n"
	(n - start) start
    )

  let is_start nt =
    nt < start

  let print normalize nt =
    if normalize then
      Misc.normalize name.(nt)
    else
      name.(nt)

  let lookup name =
    StringMap.find name map

  let positions nt =
    (StringMap.find (print false nt) Front.grammar.rules).positions

  let iter f =
    Misc.iteri n f

  let fold f accu =
    Misc.foldi n f accu

  let map f =
    Misc.mapi n f

  let iterx f =
    for nt = start to n - 1 do
      f nt
    done

  let foldx f accu =
    Misc.foldij start n f accu

  let ocamltype nt =
    assert (not (is_start nt));
    try
      Some (StringMap.find (print false nt) Front.grammar.types)
    with Not_found ->
      None

130
131
132
133
134
135
136
137
  let ocamltype_of_start_symbol nt =
    match ocamltype nt with
    | Some typ ->
        typ
    | None ->
        (* Every start symbol has a type. *)
        assert false

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
245
246
  let tabulate f =
    Array.get (Array.init n f)

end

(* Sets and maps over nonterminals, used only below. *)

module NonterminalMap = Patricia.Big

module NonterminalSet = Patricia.Big.Domain

(* ------------------------------------------------------------------------ *)
(* Terminals. *)

module Terminal = struct

  type t = int

  let t2i i = i

  let compare = (-)

  let equal (tok1 : t) (tok2 : t) =
    tok1 = tok2

  (* Determine how many terminals we have and build mappings
     both ways between names and indices. A new terminal "#"
     is created. A new terminal "error" is created. The fact
     that the integer code assigned to the "#" pseudo-terminal
     is the last one is exploited in the table-based back-end.
     (The right-most row of the action table is not created.)

     Pseudo-tokens (used in %prec declarations, but never
     declared using %token) are filtered out. *)

  let (n : int), (name : string array), (map : int StringMap.t) =
    let tokens = 
      StringMap.fold (fun token properties tokens ->
	if properties.tk_is_declared then token :: tokens else tokens
      ) Front.grammar.tokens []
    in
    match tokens with
    | [] ->
	Error.error [] "no tokens have been declared."
    | _ ->
	Misc.index ("error" :: tokens @ [ "#" ])

  let print tok =
    name.(tok)

  let lookup name =
    StringMap.find name map

  let sharp =
    lookup "#"

  let error =
    lookup "error"

  let pseudo tok =
    (tok = sharp) || (tok = error)

  let token_properties = 
    let not_so_dummy_properties = (* applicable to [error] and [#] *)
      {
	tk_filename      = "__primitives__";
	tk_priority      = UndefinedPrecedence;
	tk_associativity = UndefinedAssoc;
	tk_ocamltype     = None;
	tk_is_declared   = true;
	tk_position      = Positions.dummy;
      }
    in
    Array.init n (fun tok ->
      try 
	 StringMap.find name.(tok) Front.grammar.tokens 
       with Not_found ->
	 assert (tok = sharp || tok = error);
	 not_so_dummy_properties
    )

  let () =
    Error.logG 1 (fun f ->
      Printf.fprintf f "Grammar has %d terminal symbols.\n" (n - 2)
    )

  let precedence_level tok = 
    TokPrecedence.levelip (print tok) token_properties.(tok)

  let associativity tok =
    token_properties.(tok).tk_associativity

  let ocamltype tok =
    token_properties.(tok).tk_ocamltype

  let iter f =
    Misc.iteri n f

  let fold f accu =
    Misc.foldi n f accu

  let map f =
    Misc.mapi n f

  let mapx f =
    assert (sharp = n - 1);
    Misc.mapi (n-1) f

  (* If a token named [EOF] exists, then it is assumed to represent
247
     ocamllex's [eof] pattern. *)
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693

  let eof =
    try
      Some (lookup "EOF")
    with Not_found ->
      None

end

(* Sets of terminals are used intensively in the LR(1) construction,
   so it is important that they be as efficient as possible. *)

module TerminalSet = struct

  include CompressedBitSet 

  let print toks =
    let _, accu =
      fold (fun tok (first, accu) ->
	false,
	if first then
          accu ^ (Terminal.print tok)
	else
	  accu ^ " " ^ (Terminal.print tok)
    ) toks (true, "") in
    accu

  let universe =
    remove Terminal.sharp (
      remove Terminal.error (
        Terminal.fold add empty
      )
    )

end

(* Maps over terminals. *)

module TerminalMap = Patricia.Big

(* ------------------------------------------------------------------------ *)
(* Symbols. *)

module Symbol = struct

  type t =
    | N of Nonterminal.t
    | T of Terminal.t

  let compare sym1 sym2 =
    match sym1, sym2 with
    | N nt1, N nt2 ->
	Nonterminal.compare nt1 nt2
    | T tok1, T tok2 ->
	Terminal.compare tok1 tok2
    | N _, T _ ->
	1
    | T _, N _ ->
	-1

  let equal sym1 sym2 =
    compare sym1 sym2 = 0

  let rec lequal syms1 syms2 =
    match syms1, syms2 with
    | [], [] ->
	true
    | sym1 :: syms1, sym2 :: syms2 ->
	equal sym1 sym2 && lequal syms1 syms2
    | _ :: _, []
    | [], _ :: _ ->
	false

  let print = function
    | N nt ->
	Nonterminal.print false nt
    | T tok ->
	Terminal.print tok

  let nonterminal = function
    | T _ ->
	false
    | N _ ->
	true

  (* Printing an array of symbols. [offset] is the start offset -- we
     print everything to its right. [dot] is the dot offset -- we
     print a dot at this offset, if we find it. *)

  let printaod offset dot symbols =
    let buffer = Buffer.create 512 in
    let length = Array.length symbols in
    for i = offset to length do
      if i = dot then
	Buffer.add_string buffer ". ";
      if i < length then begin
	Buffer.add_string buffer (print symbols.(i));
	Buffer.add_char buffer ' '
      end
    done;
    Buffer.contents buffer

  let printao offset symbols =
    printaod offset (-1) symbols

  let printa symbols =
    printao 0 symbols

  let printl symbols =
    printa (Array.of_list symbols)

  let lookup name =
    try
      T (Terminal.lookup name)
    with Not_found ->
      try
	N (Nonterminal.lookup name)
      with Not_found ->
	assert false (* well-formedness check has been performed earlier *)

end

(* Sets of symbols. *)

module SymbolSet = Set.Make(Symbol)

(* Maps over symbols. *)

module SymbolMap = struct

  include Map.Make(Symbol)

  let domain m =
    fold (fun symbol _ accu ->
      symbol :: accu
    ) m []

  let purelynonterminal m =
    fold (fun symbol _ accu ->
      accu && Symbol.nonterminal symbol
    ) m true

end

(* ------------------------------------------------------------------------ *)
(* Productions. *)

module Production = struct

  type index =
      int

  (* Create an array of productions. Record which productions are
     associated with every nonterminal. A new production S' -> S
     is created for every start symbol S. It is known as a
     start production. *)

  let n : int =
    let n = StringMap.fold (fun _ { branches = branches } n ->
      n + List.length branches
    ) Front.grammar.rules 0 in
    Error.logG 1 (fun f -> Printf.fprintf f "Grammar has %d productions.\n" n);
    n + StringSet.cardinal Front.grammar.start_symbols

  let p2i prod =
    prod

  let i2p prod =
    assert (prod >= 0 && prod < n);
    prod

  let table : (Nonterminal.t * Symbol.t array) array =
    Array.make n (-1, [||])

  let identifiers : identifier array array =
    Array.make n [||]

  let used : bool array array =
    Array.make n [||]

  let actions : action option array =
    Array.make n None

  let ntprods : (int * int) array =
    Array.make Nonterminal.n (-1, -1)

  let positions : Positions.t list array =
    Array.make n []

  let (start : int),
      (startprods : index NonterminalMap.t) =
    StringSet.fold (fun nonterminal (k, startprods) ->
      let nt = Nonterminal.lookup nonterminal
      and nt' = Nonterminal.lookup (nonterminal ^ "'") in
      table.(k) <- (nt', [| Symbol.N nt |]);
      identifiers.(k) <- [| "_1" |];
      used.(k) <- [| true |];
      ntprods.(nt') <- (k, k+1);
      positions.(k) <- Nonterminal.positions nt;
      k+1,
      NonterminalMap.add nt k startprods
    ) Front.grammar.start_symbols (0, NonterminalMap.empty)

  let prec_decl : symbol located option array = 
    Array.make n None

  let reduce_precedence : precedence_level array = 
    Array.make n UndefinedPrecedence

  let (_ : int) = StringMap.fold (fun nonterminal { branches = branches } k ->
    let nt = Nonterminal.lookup nonterminal in
    let k' = List.fold_left (fun k branch ->
      let action = branch.action
      and sprec = branch.branch_shift_precedence 
      and rprec = branch.branch_reduce_precedence in	
      let symbols = Array.of_list branch.producers in
      table.(k) <- (nt, Array.map (fun (v, _) -> Symbol.lookup v) symbols);
      identifiers.(k) <- Array.mapi (fun i (_, ido) ->
	match ido with
	| None ->
	    (* Symbols for which no name was chosen will be represented
	       by variables named _1, _2, etc. *)
	    Printf.sprintf "_%d" (i + 1)
        | Some id ->
	    (* Symbols for which a name was explicitly chosen will be
	       known by that name in semantic actions. *)
	    id
      ) symbols;
      used.(k) <- Array.mapi (fun i (_, ido) ->
	match ido with
	| None ->
	    (* A symbol referred to as [$i] is used if and only if the
	       [$i] keyword appears in the semantic action. *)
            Action.has_dollar (i + 1) action
	| Some _ ->
	    (* A symbol referred to via a name is considered used.
	       This is a conservative approximation. *)
            true
      ) symbols;
      actions.(k) <- Some action;
      reduce_precedence.(k) <- rprec;
      prec_decl.(k) <- sprec;
      positions.(k) <- [ branch.branch_position ];
      k+1
    ) k branches in
    ntprods.(nt) <- (k, k');
    k'
  ) Front.grammar.rules start

  (* Iteration over the productions associated with a specific
     nonterminal. *)

  let iternt nt f =
    let k, k' = ntprods.(nt) in
    for prod = k to k' - 1 do
      f prod
    done

  let foldnt (nt : Nonterminal.t) (accu : 'a) (f : index -> 'a -> 'a) : 'a =
    let k, k' = ntprods.(nt) in
    let rec loop accu prod =
      if prod < k' then
	loop (f prod accu) (prod + 1)
      else
	accu
    in
    loop accu k

  (* Accessors. *)

  let def prod =
    table.(prod)

  let nt prod =
    let nt, _ = table.(prod) in
    nt

  let rhs prod =
    let _, rhs = table.(prod) in
    rhs

  let length prod =
    Array.length (rhs prod)

  let identifiers prod =
    identifiers.(prod)

  let used prod =
    used.(prod)

  let is_start prod =
    prod < start

  let classify prod =
    if is_start prod then
      match (rhs prod).(0) with
      | Symbol.N nt ->
	  Some nt
      | Symbol.T _ ->
	  assert false
    else
      None

  let action prod =
    match actions.(prod) with
    | Some action ->
	action
    | None ->
	(* Start productions have no action. *)
	assert (is_start prod);
	assert false

  let positions prod =
    positions.(prod)

  let startsymbol2startprod nt =
    try
      NonterminalMap.find nt startprods
    with Not_found ->
      assert false (* [nt] is not a start symbol *)

  (* Iteration. *)

  let iter f =
    Misc.iteri n f

  let fold f accu =
    Misc.foldi n f accu

  let map f =
    Misc.mapi n f

  let iterx f =
    for prod = start to n - 1 do
      f prod
    done

  let foldx f accu =
    Misc.foldij start n f accu

  (* Printing a production. *)

  let print prod =
    assert (not (is_start prod));
    let nt, rhs = table.(prod) in
    Printf.sprintf "%s -> %s" (Nonterminal.print false nt) (Symbol.printao 0 rhs)

  (* Tabulation. *)

  let tabulate f =
    Misc.tabulate n f

  let tabulateb f =
    Misc.tabulateb n f

  (* This array allows recording, on a production by production basis,
     whether the production's shift precedence is ever useful. This
     allows emitting warnings about useless %prec declarations. *)

  let prec_decl_ever_useful =
    Array.make n false

  let consult_prec_decl prod =
    lazy (prec_decl_ever_useful.(prod) <- true),
    prec_decl.(prod)

  let diagnostics () =
    iterx (fun prod ->
      if not prec_decl_ever_useful.(prod) then
	match prec_decl.(prod) with
	| None ->
	    ()
	| Some id ->
	    Error.grammar_warning [Positions.position id] "this %prec declaration is never useful."
    )

  (* Determining the precedence level of a production. If no %prec
     declaration was explicitly supplied, it is the precedence level
     of the rightmost terminal symbol in the production's right-hand
     side. *)

  type production_level =
    | PNone
    | PRightmostToken of Terminal.t
    | PPrecDecl of symbol

  let rightmost_terminal prod =
    Array.fold_left (fun accu symbol ->
      match symbol with
      | Symbol.T tok ->
	  PRightmostToken tok
      | Symbol.N _ ->
	  accu
    ) PNone (rhs prod)

  let combine e1 e2 =
    lazy (Lazy.force e1; Lazy.force e2)

  let shift_precedence prod =
    let fact1, prec_decl = consult_prec_decl prod in
    let oterminal =
      match prec_decl with
      | None ->
	  rightmost_terminal prod
      | Some { value = terminal } ->
	  PPrecDecl terminal
    in
    match oterminal with
    | PNone ->
	fact1, UndefinedPrecedence
    | PRightmostToken tok ->
	let fact2, level = Terminal.precedence_level tok in
	combine fact1 fact2, level
    | PPrecDecl id ->
	let fact2, level = TokPrecedence.leveli id  in
	combine fact1 fact2, level

end

(* ------------------------------------------------------------------------ *)
(* Maps over productions. *)

module ProductionMap = struct

  include Patricia.Big

  (* Iteration over the start productions only. *)

  let start f =
    Misc.foldi Production.start (fun prod m ->
      add prod (f prod) m
    ) empty

end

(* ------------------------------------------------------------------------ *)
(* Build the grammar's forward and backward reference graphs.

   In the backward reference graph, edges relate each nonterminal [nt]
   to each of the nonterminals whose definition mentions [nt]. The
   reverse reference graph is used in the computation of the nullable,
   nonempty, and FIRST sets.

   The forward reference graph is unused but can be printed on demand. *)

let forward : NonterminalSet.t array =
694
  Array.make Nonterminal.n NonterminalSet.empty
695
696

let backward : NonterminalSet.t array =
697
  Array.make Nonterminal.n NonterminalSet.empty
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798

let () =
  Array.iter (fun (nt1, rhs) ->
    Array.iter (function
      | Symbol.T _ ->
	  ()
      | Symbol.N nt2 ->
	  forward.(nt1) <- NonterminalSet.add nt2 forward.(nt1);
	  backward.(nt2) <- NonterminalSet.add nt1 backward.(nt2)
    ) rhs
  ) Production.table

(* ------------------------------------------------------------------------ *)
(* If requested, dump the forward reference graph. *)

let () =
  if Settings.graph then
    let module P = Dot.Print (struct
      type vertex = Nonterminal.t
      let name nt =
	Printf.sprintf "nt%d" nt
      let successors (f : ?style:Dot.style -> label:string -> vertex -> unit) nt =
	NonterminalSet.iter (fun successor ->
	  f ~label:"" successor
	) forward.(nt)
      let iter (f : ?style:Dot.style -> label:string -> vertex -> unit) =
	Nonterminal.iter (fun nt ->
	  f ~label:(Nonterminal.print false nt) nt
	)
    end) in
    let f = open_out (Settings.base ^ ".dot") in
    P.print f;
    close_out f

(* ------------------------------------------------------------------------ *)
(* Generic support for fixpoint computations.

   A fixpoint computation associates a property with every nonterminal.
   A monotone function tells how properties are computed. [compute nt]
   updates the property associated with nonterminal [nt] and returns a
   flag that tells whether the property actually needed an update. The
   state of the computation is maintained entirely inside [compute] and
   is invisible here.

   Whenever a property of [nt] is updated, the properties of the
   terminals whose definitions depend on [nt] are updated. The
   dependency graph must be explicitly supplied. *)

let fixpoint (dependencies : NonterminalSet.t array) (compute : Nonterminal.t -> bool) : unit =
  let queue : Nonterminal.t Queue.t = Queue.create () in
  let onqueue : bool array = Array.make Nonterminal.n true in
  for i = 0 to Nonterminal.n - 1 do
    Queue.add i queue
  done;
  Misc.qiter (fun nt ->
    onqueue.(nt) <- false;
    let changed = compute nt in
    if changed then
      NonterminalSet.iter (fun nt ->
	if not onqueue.(nt) then begin
	  Queue.add nt queue;
	  onqueue.(nt) <- true
	end
      ) dependencies.(nt)
  ) queue

(* ------------------------------------------------------------------------ *)
(* Compute which nonterminals are nonempty, that is, recognize a
   nonempty language. Also, compute which nonterminals are
   nullable. The two computations are almost identical. The only
   difference is in the base case: a single terminal symbol is not
   nullable, but is nonempty. *)

let compute (basecase : bool) : (bool array) * (Symbol.t -> bool) =
  let property : bool array =
    Array.make Nonterminal.n false
  in
  let symbol_has_property = function
    | Symbol.T _ ->
	basecase
    | Symbol.N nt ->
	property.(nt)
  in
  fixpoint backward (fun nt ->
    if property.(nt) then
      false (* no change *)
    else
      (* disjunction over all productions for this nonterminal *)
      let updated = Production.foldnt nt false (fun prod accu ->
	accu ||
	let rhs = Production.rhs prod in
	(* conjunction over all symbols in the right-hand side *)
	Array.fold_left (fun accu symbol ->
	  accu && symbol_has_property symbol
	) true rhs
      ) in
      property.(nt) <- updated;
      updated
  );
  property, symbol_has_property

799
800
let (nonempty : bool array), _ =
  compute true
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845

let (nullable : bool array), (nullable_symbol : Symbol.t -> bool) =
  compute false

(* ------------------------------------------------------------------------ *)
(* Compute FIRST sets. *)

let first =
  Array.make Nonterminal.n TerminalSet.empty

let first_symbol = function
  | Symbol.T tok ->
      TerminalSet.singleton tok
  | Symbol.N nt ->
      first.(nt)

let nullable_first_rhs (rhs : Symbol.t array) (i : int) : bool * TerminalSet.t =
  let length = Array.length rhs in
  assert (i <= length);
  let rec loop i toks =
    if i = length then
      true, toks
    else
      let symbol = rhs.(i) in
      let toks = TerminalSet.union (first_symbol symbol) toks in
      if nullable_symbol symbol then
	loop (i+1) toks
      else
	false, toks
  in
  loop i TerminalSet.empty

let () =
  fixpoint backward (fun nt ->
    let original = first.(nt) in
    (* union over all productions for this nonterminal *)
    let updated = Production.foldnt nt TerminalSet.empty (fun prod accu ->
      let rhs = Production.rhs prod in
      let _, toks = nullable_first_rhs rhs 0 in
      TerminalSet.union toks accu
    ) in
    first.(nt) <- updated;
    TerminalSet.compare original updated <> 0
  )

846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
(* ------------------------------------------------------------------------ *)

let () =
  (* If a start symbol generates the empty language or generates
     the language {epsilon}, report an error. In principle, this
     could be just a warning. However, in [Engine], in the function
     [start], it is convenient to assume that neither of these
     situations can arise. This means that at least one token must
     be read. *)
  StringSet.iter (fun symbol ->
    let nt = Nonterminal.lookup symbol in
    if not nonempty.(nt) then
      Error.error
	(Nonterminal.positions nt)
	(Printf.sprintf "%s generates the empty language." (Nonterminal.print false nt));
    if TerminalSet.is_empty first.(nt) then
      Error.error
	(Nonterminal.positions nt)
	(Printf.sprintf "%s generates the language {epsilon}." (Nonterminal.print false nt))
  ) Front.grammar.start_symbols;
  (* If a nonterminal symbol generates the empty language, issue a warning. *)
  for nt = Nonterminal.start to Nonterminal.n - 1 do
    if not nonempty.(nt) then
      Error.grammar_warning
	(Nonterminal.positions nt)
	(Printf.sprintf "%s generates the empty language." (Nonterminal.print false nt));
  done

874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
(* ------------------------------------------------------------------------ *)
(* Dump the analysis results. *)

let () =
  Error.logG 2 (fun f ->
    for nt = 0 to Nonterminal.n - 1 do
      Printf.fprintf f "nullable(%s) = %b\n"
	(Nonterminal.print false nt)
	nullable.(nt)
    done;
    for nt = 0 to Nonterminal.n - 1 do
      Printf.fprintf f "first(%s) = %s\n"
	(Nonterminal.print false nt)
	(TerminalSet.print first.(nt))
    done
  )

let () =
  Time.tick "Analysis of the grammar"

(* ------------------------------------------------------------------------ *)
(* Compute FOLLOW sets. Unnecessary for us, but requested by a user. Also,
   this is useful for the SLR(1) test. Thus, we perform this analysis only
   on demand. *)

let follow : TerminalSet.t array Lazy.t =
  lazy (

    let follow =
      Array.make Nonterminal.n TerminalSet.empty

    and forward : NonterminalSet.t array =
906
      Array.make Nonterminal.n NonterminalSet.empty
907
908

    and backward : NonterminalSet.t array =
909
      Array.make Nonterminal.n NonterminalSet.empty
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190

    in

    (* Iterate over all start symbols. *)
    for nt = 0 to Nonterminal.start - 1 do
      assert (Nonterminal.is_start nt);
      (* Add # to FOLLOW(nt). *)
      follow.(nt) <- TerminalSet.singleton Terminal.sharp
    done;
    (* We need to do this explicitly because our start productions are
       of the form S' -> S, not S' -> S #, so # will not automatically
       appear into FOLLOW(S) when the start productions are examined. *)

    (* Iterate over all productions. *)
    Array.iter (fun (nt1, rhs) ->
      (* Iterate over all nonterminal symbols [nt2] in the right-hand side. *)
      Array.iteri (fun i symbol ->
	match symbol with
	| Symbol.T _ ->
	    ()
	| Symbol.N nt2 ->
	    let nullable, first = nullable_first_rhs rhs (i+1) in
	    (* The FIRST set of the remainder of the right-hand side
	       contributes to the FOLLOW set of [nt2]. *)
	    follow.(nt2) <- TerminalSet.union first follow.(nt2);
	    (* If the remainder of the right-hand side is nullable,
	       FOLLOW(nt1) contributes to FOLLOW(nt2). *)
	    if nullable then begin
	      forward.(nt1) <- NonterminalSet.add nt2 forward.(nt1);
	      backward.(nt2) <- NonterminalSet.add nt1 backward.(nt2)
	    end
      ) rhs
    ) Production.table;

    (* The fixpoint computation used here is not the most efficient
       algorithm -- one could do better by first collapsing the
       strongly connected components, then walking the graph in
       topological order. But this will do. *)

    fixpoint forward (fun nt ->
      let original = follow.(nt) in
      (* union over all contributors *)
      let updated = NonterminalSet.fold (fun nt' accu ->
	TerminalSet.union follow.(nt') accu
      ) backward.(nt) original in
      follow.(nt) <- updated;
      TerminalSet.compare original updated <> 0
    );

    follow

  )

(* Define an accessor that triggers the computation of the FOLLOW sets
   if it has not been performed already. *)

let follow nt =
  (Lazy.force follow).(nt)

(* At log level 2, display the FOLLOW sets. *)

let () =
  Error.logG 2 (fun f ->
    for nt = 0 to Nonterminal.n - 1 do
      Printf.fprintf f "follow(%s) = %s\n"
	(Nonterminal.print false nt)
	(TerminalSet.print (follow nt))
    done
  )

(* Compute FOLLOW sets for the terminal symbols as well. Again, unnecessary
   for us, but requested by a user. This is done in a single pass over the
   grammar -- no new fixpoint computation is required. *)

let tfollow : TerminalSet.t array Lazy.t =
  lazy (

    let tfollow =
      Array.make Terminal.n TerminalSet.empty
    in

    (* Iterate over all productions. *)
    Array.iter (fun (nt1, rhs) ->
      (* Iterate over all terminal symbols [t2] in the right-hand side. *)
      Array.iteri (fun i symbol ->
	match symbol with
	| Symbol.N _ ->
	    ()
	| Symbol.T t2 ->
	    let nullable, first = nullable_first_rhs rhs (i+1) in
	    (* The FIRST set of the remainder of the right-hand side
	       contributes to the FOLLOW set of [t2]. *)
	    tfollow.(t2) <- TerminalSet.union first tfollow.(t2);
	    (* If the remainder of the right-hand side is nullable,
	       FOLLOW(nt1) contributes to FOLLOW(t2). *)
	    if nullable then
	      tfollow.(t2) <- TerminalSet.union (follow nt1) tfollow.(t2)
      ) rhs
    ) Production.table;

    tfollow

  )

(* Define another accessor. *)

let tfollow t =
  (Lazy.force tfollow).(t)

(* At log level 3, display the FOLLOW sets for terminal symbols. *)

let () =
  Error.logG 3 (fun f ->
    for t = 0 to Terminal.n - 1 do
      Printf.fprintf f "follow(%s) = %s\n"
	(Terminal.print t)
	(TerminalSet.print (tfollow t))
    done
  )

(* ------------------------------------------------------------------------ *)
(* Provide explanations about FIRST sets. *)

(* The idea is to explain why a certain token appears in the FIRST set
   for a certain sequence of symbols. Such an explanation involves
   basic assertions of the form (i) symbol N is nullable and (ii) the
   token appears in the FIRST set for symbol N. We choose to take
   these basic facts for granted, instead of recursively explaining
   them, so as to keep explanations short. *)

(* We first produce an explanation in abstract syntax, then
   convert it to a human-readable string. *)

type explanation =
  | EObvious                                 (* sequence begins with desired token *)
  | EFirst of Terminal.t * Nonterminal.t     (* sequence begins with a nonterminal that produces desired token *)
  | ENullable of Symbol.t list * explanation (* sequence begins with a list of nullable symbols and ... *)

let explain (tok : Terminal.t) (rhs : Symbol.t array) (i : int) =
  let length = Array.length rhs in
  let rec loop i =
    assert (i < length);
    let symbol = rhs.(i) in
    match symbol with
    | Symbol.T tok' ->
	assert (Terminal.equal tok tok');
	EObvious
    | Symbol.N nt ->
	if TerminalSet.mem tok first.(nt) then
	  EFirst (tok, nt)
	else begin
	  assert nullable.(nt);
	  match loop (i + 1) with
	  | ENullable (symbols, e) ->
	      ENullable (symbol :: symbols, e)
	  | e ->
	      ENullable ([ symbol ], e)
	end
  in
  loop i

let rec convert = function
  | EObvious ->
      ""
  | EFirst (tok, nt) ->
      Printf.sprintf "%s can begin with %s"
	(Nonterminal.print false nt)
	(Terminal.print tok)
  | ENullable (symbols, e) ->
      let e = convert e in
      Printf.sprintf "%scan vanish%s%s"
	(Symbol.printl symbols)
	(if e = "" then "" else " and ")
	e

(* ------------------------------------------------------------------------ *)
(* Package the analysis results. *)

module Analysis = struct

  let nullable_first_rhs = nullable_first_rhs

  let explain_first_rhs (tok : Terminal.t) (rhs : Symbol.t array) (i : int) =
    convert (explain tok rhs i)

  let follow = follow

end

(* ------------------------------------------------------------------------ *)
(* Conflict resolution via precedences. *)

module Precedence = struct

  type choice =
    | ChooseShift
    | ChooseReduce
    | ChooseNeither
    | DontKnow

  type order = Lt | Gt | Eq | Ic

  let precedence_order p1 p2 = 
    match p1, p2 with
      |	UndefinedPrecedence, _
      | _, UndefinedPrecedence -> 
	  Ic

      | PrecedenceLevel (m1, l1, _, _), PrecedenceLevel (m2, l2, _, _) ->
	  if not (Mark.same m1 m2) then
	    Ic
	  else
	    if l1 > l2 then 
	      Gt 
	    else if l1 < l2 then 
	      Lt
	    else 
	      Eq

  let shift_reduce tok prod =
    let fact1, tokp  = Terminal.precedence_level tok
    and fact2, prodp = Production.shift_precedence prod in
    match precedence_order tokp prodp with
   
      (* Our information is inconclusive. Drop [fact1] and [fact2],
	 that is, do not record that this information was useful. *)

    | Ic ->
	DontKnow

      (* Our information is useful. Record that fact by evaluating
	 [fact1] and [fact2]. *)

    | (Eq | Lt | Gt) as c ->
	Lazy.force fact1;
	Lazy.force fact2;
	match c with

	| Ic ->
	    assert false (* already dispatched *)

	| Eq -> 
	    begin
	      match Terminal.associativity tok with
	      | LeftAssoc  -> ChooseReduce
	      | RightAssoc -> ChooseShift
	      | NonAssoc   -> ChooseNeither
	      | _          -> assert false
			      (* If [tok]'s precedence level is defined, then
				 its associativity must be defined as well. *)
	    end

	| Lt ->
	    ChooseReduce

	| Gt ->
	    ChooseShift


  let reduce_reduce prod1 prod2 =
    let rp1 = Production.reduce_precedence.(prod1) 
    and rp2 = Production.reduce_precedence.(prod2) in
    match precedence_order rp1 rp2 with
    | Lt -> 
	Some prod1
    | Gt -> 
	Some prod2
    | Eq -> 
	(* the order is strict except in presence of inlining: 
	   two branches can have the same precedence level when
	   they come from an inlined one. *)
	None
    | Ic -> 
	None

end
  
let diagnostics () =
  TokPrecedence.diagnostics();
  Production.diagnostics()