DependencyGraph.ml 2.15 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 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
open Grammar

let print_dependency_graph() =

  (* Allocate. *)

  let forward : NonterminalSet.t NonterminalMap.t ref =
    ref NonterminalMap.empty
  in

  let successors nt =
    try
      NonterminalMap.find nt !forward
    with Not_found ->
      NonterminalSet.empty
  in

  (* Populate. *)

  Production.iter (fun prod ->
    let nt1 = Production.nt prod
    and rhs = Production.rhs prod in
    Array.iter (function
      | Symbol.T _ ->
          ()
      | Symbol.N nt2 ->
          forward := NonterminalMap.add
            nt1
            (NonterminalSet.add nt2 (successors nt1))
            !forward
    ) rhs
  );

  (* Print. *)

  let module P = Dot.Print (struct
    type vertex = Nonterminal.t
    let name nt =
      Printf.sprintf "nt%d" (Nonterminal.n2i nt)
    let successors (f : ?style:Dot.style -> label:string -> vertex -> unit) nt =
      NonterminalSet.iter (fun successor ->
        f ~label:"" successor
      ) (successors nt)
    let iter (f : ?shape:Dot.shape -> ?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