grew_ast.mli 9.26 KB
Newer Older
bguillaum's avatar
bguillaum committed
1 2 3 4 5 6 7 8 9 10
(**********************************************************************************)
(*    Libcaml-grew - a Graph Rewriting library dedicated to NLP applications      *)
(*                                                                                *)
(*    Copyright 2011-2013 Inria, Université de Lorraine                           *)
(*                                                                                *)
(*    Webpage: http://grew.loria.fr                                               *)
(*    License: CeCILL (see LICENSE folder or "http://www.cecill.info")            *)
(*    Authors: see AUTHORS file                                                   *)
(**********************************************************************************)

11
open Grew_base
12
open Grew_types
13

bguillaum's avatar
bguillaum committed
14
module Ast : sig
15 16

  (* ---------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
17
  (* simple_ident: cat or V *)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  type simple_ident = Id.name
  val parse_simple_ident: string -> simple_ident
  val is_simple_ident: string -> bool
  val dump_simple_ident: simple_ident -> string

  (* ---------------------------------------------------------------------- *)
  (* label_ident: D:mod.dis *)
  type label_ident = string
  val parse_label_ident: string -> label_ident
  val dump_label_ident: label_ident -> string

  (* ---------------------------------------------------------------------- *)
  (* pattern_label_ident: D:mod.* *)
  type pattern_label_ident = string
  val parse_pattern_label_ident: string -> pattern_label_ident
  val dump_pattern_label_ident: pattern_label_ident -> string

35 36 37 38 39 40
  (* ---------------------------------------------------------------------- *)
  (* node_ident: W0.5 *)
  type node_ident = string
  val parse_node_ident: string -> node_ident
  val dump_node_ident: node_ident -> string

41 42 43 44 45 46
  (* ---------------------------------------------------------------------- *)
  (* feature_ident: V.cat *)
  type feature_ident = Id.name * feature_name
  val parse_feature_ident: string -> feature_ident
  val dump_feature_ident: feature_ident -> string

bguillaum's avatar
bguillaum committed
47 48 49 50 51 52
  (* ---------------------------------------------------------------------- *)
  (* simple_or_feature_ident: union of simple_ident and feature_ident *)
  (* Note: used for parsing of "X < Y" and "X.feat < Y.feat" without conflicts *)
  type simple_or_feature_ident = Id.name * feature_name option
  val parse_simple_or_feature_ident: string -> simple_or_feature_ident

53
  (* ---------------------------------------------------------------------- *)
54
  type feature_kind =
55 56
    | Equality of feature_value list
    | Disequality of feature_value list
bguillaum's avatar
bguillaum committed
57
    | Equal_param of string (* $ident *)
bguillaum's avatar
bguillaum committed
58
    | Absent
bguillaum's avatar
bguillaum committed
59 60

  type u_feature = {
61 62 63
    name: feature_name;
    kind: feature_kind;
  }
Bruno Guillaume's avatar
Bruno Guillaume committed
64 65
  val u_feature_to_string: u_feature -> string

bguillaum's avatar
bguillaum committed
66 67
  type feature = u_feature * Loc.t

68 69
  val default_fs: ?loc:Loc.t -> string -> feature list

bguillaum's avatar
bguillaum committed
70 71
  type u_node = {
      node_id: Id.name;
72
      position: float option;
bguillaum's avatar
bguillaum committed
73 74 75 76
      fs: feature list;
    }
  type node = u_node * Loc.t

77 78
  val grewpy_compare: node -> node -> int

79 80
  type edge_label = string (* p_obj.agt:suj *)

81 82 83 84
  type edge_label_cst =
    | Pos_list of edge_label list (*  X|Y|Z    *)
    | Neg_list of edge_label list (*  ^X|Y|Z   *)
    | Regexp of string            (*  re"a.*"  *)
bguillaum's avatar
bguillaum committed
85

bguillaum's avatar
bguillaum committed
86 87 88
  type u_edge = {
      edge_id: Id.name option;
      src: Id.name;
bguillaum's avatar
bguillaum committed
89
      edge_label_cst: edge_label_cst;
bguillaum's avatar
bguillaum committed
90 91 92 93
      tar: Id.name;
    }
  type edge = u_edge * Loc.t

bguillaum's avatar
bguillaum committed
94 95 96
  type ineq = Lt | Gt | Le | Ge
  val string_of_ineq: ineq -> string

97
  type u_const =
98 99
    | Cst_out of Id.name * edge_label_cst
    | Cst_in of Id.name * edge_label_cst
100 101 102
    | Features_eq of feature_ident * feature_ident
    | Features_diseq of feature_ident * feature_ident
    | Features_ineq of ineq * feature_ident * feature_ident
103
    | Feature_ineq_cst of ineq * feature_ident * float
104
    | Feature_eq_float of feature_ident * float
105 106
    | Feature_diff_float of feature_ident * float

107 108
    | Feature_eq_regexp of feature_ident * string
    | Feature_eq_cst of feature_ident * string
109 110
    | Feature_diff_cst of feature_ident * string

111 112
    | Immediate_prec of Id.name * Id.name
    | Large_prec of Id.name * Id.name
bguillaum's avatar
bguillaum committed
113 114
  type const = u_const * Loc.t

bguillaum's avatar
bguillaum committed
115
  type basic = {
bguillaum's avatar
bguillaum committed
116 117 118 119 120
      pat_nodes: node list;
      pat_edges: edge list;
      pat_const: const list;
    }

121 122 123 124 125
  type pattern = {
      pat_pos: basic;
      pat_negs: basic list;
    }

126 127 128 129
  (* [check for duplicate edge identifier in pos part and
     remove edge identifier in neg part] *)
  val normalize_pattern : pattern -> pattern

130 131
  val complete_pattern : pattern -> pattern

bguillaum's avatar
bguillaum committed
132
  type concat_item =
133
    | Qfn_item of feature_ident
bguillaum's avatar
bguillaum committed
134
    | String_item of string
135
    | Param_item of string
136

137
  type u_command =
bguillaum's avatar
bguillaum committed
138
    | Del_edge_expl of (Id.name * Id.name * edge_label)
bguillaum's avatar
bguillaum committed
139
    | Del_edge_name of string
bguillaum's avatar
bguillaum committed
140
    | Add_edge of (Id.name * Id.name * edge_label)
141
    | Add_edge_expl of (Id.name * Id.name * string)
142 143

    (* 4 args: source, target, labels, flag true iff negative cst *)
bguillaum's avatar
bguillaum committed
144 145 146
    | Shift_in of (Id.name * Id.name * edge_label_cst)
    | Shift_out of (Id.name * Id.name * edge_label_cst)
    | Shift_edge of (Id.name * Id.name * edge_label_cst)
147

bguillaum's avatar
bguillaum committed
148 149 150 151
    | New_node of Id.name
    | New_before of (Id.name * Id.name)
    | New_after of (Id.name * Id.name)

bguillaum's avatar
bguillaum committed
152
    | Del_node of Id.name
153

bguillaum's avatar
bguillaum committed
154 155
    | Del_feat of feature_ident
    | Update_feat of feature_ident * concat_item list
bguillaum's avatar
bguillaum committed
156
  val string_of_u_command:  u_command -> string
bguillaum's avatar
bguillaum committed
157
  type command = u_command * Loc.t
158

bguillaum's avatar
bguillaum committed
159 160
  type rule = {
      rule_id:Id.name;
161
      pattern: pattern;
bguillaum's avatar
bguillaum committed
162
      commands: command list;
163
      param: (string list * string list) option; (* (files, vars) *)
bguillaum's avatar
bguillaum committed
164
      lex_par: string list option; (* lexical parameters in the file *)
bguillaum's avatar
bguillaum committed
165
      rule_doc:string list;
bguillaum's avatar
bguillaum committed
166
      rule_loc: Loc.t;
167
      rule_dir: string option; (* the real folder where the file is defined *)
bguillaum's avatar
bguillaum committed
168
    }
169

bguillaum's avatar
bguillaum committed
170 171 172
  type modul = {
      module_id:Id.name;
      rules: rule list;
173
      deterministic: bool;
bguillaum's avatar
bguillaum committed
174
      module_doc:string list;
bguillaum's avatar
bguillaum committed
175
      mod_loc:Loc.t;
176
      mod_dir: string; (* the directory where the module is defined (for lp file localisation) *)
bguillaum's avatar
bguillaum committed
177
    }
178 179

  type module_or_include =
bguillaum's avatar
bguillaum committed
180
    | Modul of modul
181
    | Includ of (string * Loc.t)
bguillaum's avatar
bguillaum committed
182

183 184 185 186 187 188 189
  type feature_spec =
    | Closed of feature_name * feature_atom list (* cat:V,N *)
    | Open of feature_name (* phon, lemma, ... *)
    | Num of feature_name (* position *)

  val build_closed: feature_name -> feature_atom list -> feature_spec

190
  type domain = {
Bruno Guillaume's avatar
Bruno Guillaume committed
191
      conll_fields: string list option;
192
      feature_domain: feature_spec list;
193 194 195
      label_domain: (string * string list) list;
    }

196 197
  type domain_wi = Dom of domain | Dom_file of string

198 199 200 201
  type strat_def = (* /!\ The list must not be empty in the Seq or Plus constructor *)
    | Ref of string            (* reference to a module name or to another strategy *)
    | Seq of strat_def list    (* a sequence of strategies to apply one after the other *)
    | Star of strat_def        (* a strategy to apply iteratively *)
202
    | Pick of strat_def        (* pick one normal form a the given strategy; return 0 if nf *)
203 204 205 206 207 208 209 210 211 212 213 214 215 216
    | Sequence of string list  (* compatibility mode with old code *)

  val strat_def_to_string: strat_def -> string

  val strat_def_flatten: strat_def -> strat_def

  (* a strategy is given by its descrition in the grs file and the 4 fields: *)
  type strategy = {
    strat_name:string;       (* a unique name of the stratgy *)
    strat_def:strat_def;     (* the definition itself *)
    strat_doc:string list;   (* lines of docs (if any in the GRS file) *)
    strat_loc:Loc.t;         (* the location of the [name] of the strategy *)
  }

217
  type grs_wi = {
bguillaum's avatar
bguillaum committed
218
      domain_wi: domain_wi option;
219
      modules_wi: module_or_include list;
220
      strategies_wi: strategy list;
bguillaum's avatar
bguillaum committed
221 222
    }

223
  (* a GRS: graph rewriting system *)
bguillaum's avatar
bguillaum committed
224
  type grs = {
bguillaum's avatar
bguillaum committed
225
      domain: domain option;
bguillaum's avatar
bguillaum committed
226
      modules: modul list;
227
      strategies: strategy list;
bguillaum's avatar
bguillaum committed
228
    }
229
  val empty_grs: grs
bguillaum's avatar
bguillaum committed
230 231

  type gr = {
232
    meta: string list;
233 234 235
    nodes: node list;
    edges: edge list;
  }
236 237
  val complete_graph: gr -> gr

238 239 240 241 242 243
  (* phrase structure tree *)
  type pst =
  | Leaf of (Loc.t * string) (* phon *)
  | T of (Loc.t * string * pst list)
  val word_list: pst -> string list
end (* module Ast *)
244 245 246 247 248 249 250 251 252 253


(* ================================================================================================ *)
module New_ast : sig
  type strat =
  | Ref of Ast.node_ident       (* reference to a rule name or to another strategy *)
  | Pick of strat               (* pick one normal form a the given strategy; return 0 if nf *)
  | Alt of strat list           (* a set of strategies to apply in parallel *)
  | Seq of strat list           (* a sequence of strategies to apply one after the other *)
  | Iter of strat               (* a strategy to apply iteratively *)
Bruno Guillaume's avatar
Bruno Guillaume committed
254
  | Onf of strat                (* deterministic computation of One Normal Form *)
255 256 257 258
  | If of strat * strat * strat (* choose a stragegy with a test *)
  | Try of strat                (* ≜ If (S, S, Empty): pick one normal form a the given strategy; return input if nf *)

  type decl =
Bruno Guillaume's avatar
Bruno Guillaume committed
259
  | Conll_fields of string list
260 261
  | Features of Ast.feature_spec list
  | Labels of (string * string list) list
262
  | Package of (Loc.t * Ast.simple_ident * decl list)
263
  | Rule of Ast.rule
264
  | Strategy of (Loc.t * Ast.simple_ident * strat)
265 266 267
  | Import of string
  | Include of string

268
  type grs = decl list
269

Bruno Guillaume's avatar
Bruno Guillaume committed
270 271
  val strat_to_json: strat -> Yojson.Basic.json

272
  val strat_list: grs -> string list
273 274

  val convert: Ast.grs -> grs
275
end (* module New_ast *)