Commit eeae0985 authored by bguillaum's avatar bguillaum

move strategies definition in grew_ast.ml

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/semagramme/libcaml-grew/trunk@9145 7838e531-6607-4d57-9587-6c381814729c
parent 52af666e
......@@ -2,7 +2,7 @@ digraph grew {
node [shape=Mrecord];
rankdir = LR;
grew_base [label="grew_base|String_map\nString_set\nInt_map\nInt_set\nString_\nDot\nLoc\nFile\nArray_\nList_\nMassoc_make()\nError\nId\nTimeout\nGlobal"]
grew_types [label="grew_types|Pid(_map\|_set)\nGid(_map)\nMassoc[_gid\|_pid]\nLabel_domain\nFeature_domain\nDomain\nLabel\nFeature_value\nLex_par\nConcat_item\nStrategy"]
grew_types [label="grew_types|Pid(_map\|_set)\nGid(_map)\nMassoc[_gid\|_pid]\nLabel_domain\nFeature_domain\nDomain\nLabel\nFeature_value\nLex_par\nConcat_item"]
grew_ast [label="grew_ast|Ast"]
grew_loader [label="grew_loader|Loader\nParser"]
grew_fs [label="grew_fs|G_feature\nP_feature\nG_fs\nP_fs"]
......
......@@ -314,6 +314,49 @@ module Ast = struct
mod_dir: string; (* the directory where the module is defined (for lp file localisation) *)
}
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 *)
| Plus of strat_def list (* a set of strategies to apply in parallel *)
| Star of strat_def (* a strategy to apply iteratively *)
| Diamond of strat_def (* pick one normal form a the given strategy *)
| Sequence of string list (* compatibility mode with old code *)
let rec strat_def_to_string = function
| Ref m -> m
| Seq l -> "[" ^ (String.concat "; " (List.map strat_def_to_string l)) ^ "]"
| Plus l -> "[" ^ (String.concat "+" (List.map strat_def_to_string l)) ^ "]"
| Star s -> "[" ^ (strat_def_to_string s) ^"]" ^ "*"
| Diamond s -> "◇" ^ "[" ^(strat_def_to_string s)^"]"
| Sequence names -> "{" ^ (String.concat ";" names) ^ "}"
(* invariant: Seq list and Plus list are not empty in the input and so not empty in the output *)
let rec strat_def_flatten = function
| Sequence l -> Sequence l
| Ref m -> Ref m
| Star s -> Star (strat_def_flatten s)
| Diamond s -> Diamond (strat_def_flatten s)
| Seq l ->
let fl = List.map strat_def_flatten l in
let rec loop = function
| [] -> []
| (Seq l) :: tail -> l @ (loop tail)
| x :: tail -> x :: (loop tail)
in Seq (loop fl)
| Plus l ->
let fl = List.map strat_def_flatten l in
let rec loop = function
| [] -> []
| (Plus l) :: tail -> l @ (loop tail)
| x :: tail -> x :: (loop tail)
in Plus (loop fl)
type strategy = {
strat_name: string;
strat_def: strat_def;
strat_doc: string list;
strat_loc: Loc.t;
}
(** a GRS: graph rewriting system *)
......@@ -331,13 +374,13 @@ module Ast = struct
type grs_wi = {
domain_wi: domain_wi option;
modules_wi: module_or_include list;
strategies_wi: Strategy.t list;
strategies_wi: strategy list;
}
type grs = {
domain: domain option;
modules: modul list;
strategies: Strategy.t list;
strategies: strategy list;
}
type gr = {
......
......@@ -187,33 +187,50 @@ module Ast : sig
type domain_wi = Dom of domain | Dom_file of string
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 *)
| Plus of strat_def list (* a set of strategies to apply in parallel *)
| Star of strat_def (* a strategy to apply iteratively *)
| Diamond of strat_def (* pick one normal form a the given strategy *)
| 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 *)
}
type grs_wi = {
domain_wi: domain_wi option;
modules_wi: module_or_include list;
strategies_wi: Strategy.t list;
strategies_wi: strategy list;
}
(* a GRS: graph rewriting system *)
type grs = {
domain: domain option;
modules: modul list;
strategies: Strategy.t list;
strategies: strategy list;
}
val empty_grs: grs
type gr = {
meta: string list;
nodes: node list;
edges: edge list;
}
val complete_graph: gr -> gr
val empty_grs: grs
(* 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 *)
This diff is collapsed.
......@@ -324,10 +324,10 @@ module Html_doc = struct
wnl " <center><h1>List of sequences</h1></center>";
List.iter
(fun ast_seq ->
wnl "<h6>%s</h6>" ast_seq.Strategy.name;
List.iter (fun l -> wnl "<p>%s</p>" (doc_to_html l)) ast_seq.Strategy.doc;
wnl "<h6>%s</h6>" ast_seq.Ast.strat_name;
List.iter (fun l -> wnl "<p>%s</p>" (doc_to_html l)) ast_seq.Ast.strat_doc;
wnl "<div class=\"code\">";
wnl "%s" (Strategy.to_string ast_seq.Strategy.def);
wnl "%s" (Ast.strat_def_to_string ast_seq.Ast.strat_def);
wnl "</div>";
) ast.Ast.strategies;
......
......@@ -155,11 +155,11 @@ module Parser = struct
with Sys_error msg -> Error.parse "[Grew_loader.Parser.pattern] %s" msg
(* ------------------------------------------------------------------------------------------*)
let strategy desc =
let strat_def desc =
try
Global.init "Not a file";
let lexbuf = Lexing.from_string desc in
let strategy = parse_handle "Not a file" (Grew_parser.strategy Grew_lexer.global) lexbuf in
let strategy = parse_handle "Not a file" (Grew_parser.strat_def Grew_lexer.global) lexbuf in
strategy
with Sys_error msg -> Error.parse "[Grew_loader.Parser.strategy] %s" msg
......
......@@ -32,5 +32,5 @@ module Parser : sig
val pattern: string -> Ast.pattern
val strategy: string -> Strategy.def
val strat_def: string -> Ast.strat_def
end
\ No newline at end of file
......@@ -118,7 +118,7 @@ let localize t = (t,get_loc ())
%start <Grew_ast.Ast.module_or_include list> included
%start <Grew_ast.Ast.pattern> pattern
%start <Grew_ast.Ast.domain> domain
%start <Grew_types.Strategy.def> strategy
%start <Grew_ast.Ast.strat_def> strat_def
/* parsing of the string representation of the constituent representation of Sequoia */
/* EX: "( (SENT (NP (NC Amélioration) (PP (P de) (NP (DET la) (NC sécurité))))))" */
......@@ -692,34 +692,34 @@ sequences:
sequence:
/* seq_name { ant; p7_to_p7p-mc} */
| doc = option(COMMENT) id_loc=simple_id_with_loc mod_names=delimited(LACC,separated_list_final_opt(SEMIC,simple_id),RACC)
{ let (name,loc) = id_loc in
{ let (strat_name,strat_loc) = id_loc in
{
Strategy.name;
def = Strategy.Sequence mod_names;
doc = begin match doc with Some d -> d | None -> [] end;
loc;
Ast.strat_name;
strat_def = Ast.Sequence mod_names;
strat_doc = begin match doc with Some d -> d | None -> [] end;
strat_loc;
}
}
/* strat = <>(M1+M2)* */
| doc = option(COMMENT) id_loc=simple_id_with_loc EQUAL def=strat_def
{ let (name,loc) = id_loc in
| doc = option(COMMENT) id_loc=simple_id_with_loc EQUAL strat_def=strat_def_rec
{ let (strat_name,strat_loc) = id_loc in
{
Strategy.name;
def;
doc = begin match doc with Some d -> d | None -> [] end;
loc;
Ast.strat_name;
strat_def;
strat_doc = begin match doc with Some d -> d | None -> [] end;
strat_loc;
}
}
strat_def:
| m=simple_id { Strategy.Ref m }
| LPAREN s=strat_def RPAREN { s }
| s=strat_def STAR { Strategy.Star (s) }
| s1=strat_def PLUS s2=strat_def { Strategy.Plus [s1; s2] }
| s1=strat_def SEMIC s2=strat_def { Strategy.Seq [s1; s2] }
| DISEQUAL s=strat_def { Strategy.Diamond s }
strat_def_rec:
| m=simple_id { Ast.Ref m }
| LPAREN s=strat_def_rec RPAREN { s }
| s=strat_def_rec STAR { Ast.Star (s) }
| s1=strat_def_rec PLUS s2=strat_def_rec { Ast.Plus [s1; s2] }
| s1=strat_def_rec SEMIC s2=strat_def_rec { Ast.Seq [s1; s2] }
| DISEQUAL s=strat_def_rec { Ast.Diamond s }
strategy: s = strat_def EOF { s }
strat_def: s = strat_def_rec EOF { s }
/*=============================================================================================*/
/* ISOLATED PATTERN (grep mode) */
......
......@@ -443,49 +443,3 @@ module Concat_item = struct
| String of string
end (* module Concat_item *)
(* ================================================================================ *)
module Strategy = struct
type def =
| Ref of string
| Seq of def list (* /!\ The list must not be empty *)
| Plus of def list (* /!\ The list must not be empty *)
| Star of def
| Diamond of def
| Sequence of string list (* compatibility mode with old code *)
type t = {
name:string;
def:def;
doc:string list;
loc:Loc.t;
}
let rec to_string = function
| Ref m -> m
| Seq l -> "[" ^ (String.concat "; " (List.map to_string l)) ^ "]"
| Plus l -> "[" ^ (String.concat "+" (List.map to_string l)) ^ "]"
| Star s -> "[" ^ (to_string s) ^"]" ^ "*"
| Diamond s -> "◇" ^ "[" ^(to_string s)^"]"
| Sequence names -> "{" ^ (String.concat ";" names) ^ "}"
(* invariant: Seq list and Plus list are not empty in the input and so not empty in the output *)
let rec flatten = function
| Sequence l -> Sequence l
| Ref m -> Ref m
| Star s -> Star (flatten s)
| Diamond s -> Diamond (flatten s)
| Seq l ->
let fl = List.map flatten l in
let rec loop = function
| [] -> []
| (Seq l) :: tail -> l @ (loop tail)
| x :: tail -> x :: (loop tail)
in Seq (loop fl)
| Plus l ->
let fl = List.map flatten l in
let rec loop = function
| [] -> []
| (Plus l) :: tail -> l @ (loop tail)
| x :: tail -> x :: (loop tail)
in Plus (loop fl)
end (* module Strategy *)
......@@ -177,33 +177,3 @@ module Concat_item : sig
| String of string
end (* module Concat_item *)
(* ================================================================================ *)
module Strategy : sig
(* recursive definition of strategies *)
type def =
| Ref of string (* reference to a module name or to another strategy *)
| Seq of def list (* a sequence of strategies to apply one after the other *)
| Plus of def list (* a set of strategies to apply in parallel *)
| Star of def (* a strategy to apply iteratively *)
| Diamond of def (* pick one normal form a the given strategy *)
(* /!\ The list must not be empty in the Seq or Plus constructor *)
| Sequence of string list (* compatibility mode with old code *)
(* string dump of a strat *)
val to_string : def -> string
(* build an equivalent strategies where embedded Seq in Seq (resp Plus in Plus) are flattened *)
val flatten : def -> def
(* a strategy is given by its descrition in the grs file and the 4 fields: *)
type t = {
name:string; (* a unique name of the stratgy *)
def:def; (* the definition itself *)
doc:string list; (* lines of docs (if any in the GRS file) *)
loc:Loc.t; (* the location of the [name] of the strategy *)
}
end (* module Strategy *)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment