Commit c1cbbc95 authored by Bruno Guillaume's avatar Bruno Guillaume

new syntax for GRS: lexer, parser and AST

parent 180b9fe3
......@@ -470,3 +470,40 @@ module Ast = struct
| Leaf (_, p) -> [p]
| T (_,_,l) -> List.flatten (List.map word_list l)
end (* module Ast *)
(* ================================================================================================ *)
module New_ast = struct
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 *)
| If of strat * strat * strat (* choose a stragegy with a test *)
(* syntactic sugar *)
| Empty (* ≜ Seq [] *)
| Try of strat (* ≜ If (S, S, Empty): pick one normal form a the given strategy; return input if nf *)
| Rules of Ast.node_ident (* ≜ Alt (rules defined in the top level of the package with the given name *)
type decl =
| Package of (Ast.simple_ident * decl list)
| Rule of Ast.rule
| Strategy of (Ast.simple_ident * strat)
| Import of string
| Include of string
type top_decl =
| Features of Ast.feature_spec list
| Labels of (string * string list) list
| D of decl
type grs = top_decl list
end (* module New_ast *)
......@@ -242,3 +242,33 @@ module Ast : sig
| T of (Loc.t * string * pst list)
val word_list: pst -> string list
end (* module Ast *)
(* ================================================================================================ *)
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 *)
| If of strat * strat * strat (* choose a stragegy with a test *)
(* syntactic sugar *)
| Empty (* ≜ Seq [] *)
| Try of strat (* ≜ If (S, S, Empty): pick one normal form a the given strategy; return input if nf *)
| Rules of Ast.node_ident (* ≜ Alt (rules defined in the top level of the package with the given name *)
type decl =
| Package of (Ast.simple_ident * decl list)
| Rule of Ast.rule
| Strategy of (Ast.simple_ident * strat)
| Import of string
| Include of string
type top_decl =
| Features of Ast.feature_spec list
| Labels of (string * string list) list
| D of decl
type grs = top_decl list
end (* module New_ast *)
......@@ -178,7 +178,6 @@ module Domain = struct
| Some (_, feature_domain) -> Feature_domain.build_disj ?loc ~feature_domain name unsorted_values
| None -> Feature_domain.build_disj ?loc name unsorted_values
let feature_names (_, feature_domain) = Feature_domain.feature_names feature_domain
let get_label_name ?domain index = match domain with
......
......@@ -588,3 +588,43 @@ module Grs = struct
) grs.modules
end (* module Grs *)
module New_grs = struct
type decl =
| Rule of Rule.t
| Strategy of New_ast.strat
| Package of string * decl list
type t = {
filename: string;
domain: Domain.t option;
decls: decl list;
ast: New_ast.grs;
}
(*
let load_decl file =
let ast = Loader.new_grs file in
List.map (fun
)
let load file =
let ast = Loader.new_grs file in
match ast with
| *)
let load filename =
let ast = Loader.new_grs filename in
let feature_domains = List_.opt_map
(fun x -> match x with
| New_ast.Features desc -> Some desc
| _ -> None
) ast in
{ filename;
ast;
domain = None;
decls = [];
}
end
......@@ -112,3 +112,9 @@ module Grs: sig
val to_json: t -> Yojson.Basic.json
end (* module Grs *)
module New_grs : sig
type t
val load: string -> t
end
\ No newline at end of file
......@@ -151,6 +151,7 @@ and standard target = parse
| '\n' { Global.new_line (); Lexing.new_line lexbuf; global lexbuf}
| "include" { INCL }
| "import" { IMPORT }
| "domain" { DOMAIN }
| "features" { FEATURES }
| "feature" { FEATURE }
......@@ -174,14 +175,22 @@ and standard target = parse
| "del_feat" { DEL_FEAT }
| "module" { MODULE }
| "package" { PACKAGE }
| "confluent" { Log.fwarning "%s \"confluent\" is deprecated, please use \"deterministic\" instead" (Global.loc_string ()); DETERMINISTIC }
| "deterministic" { DETERMINISTIC }
| "rule" { RULE }
| "strat" { STRAT }
| "lex_rule" { Log.fwarning "%s \"lex_rule\" is deprecated, please use \"rule\" instead" (Global.loc_string ()); RULE }
| "sequences" { SEQUENCES }
| "pick" { PICK }
| "try" { TRY }
| "Pick" { PICK }
| "Alt" { ALT }
| "Seq" { SEQ }
| "Iter" { ITER }
| "If" { IF }
| "Empty" { EMPTY }
| "Try" { TRY }
| "Rules" { RULES }
| "graph" { GRAPH }
......
......@@ -95,6 +95,16 @@ module Loader = struct
Ast.strategies = grs_wi.Ast.strategies_wi;
}
let new_grs file =
try
Global.new_file file;
let in_ch = open_in file in
let lexbuf = Lexing.from_channel in_ch in
let grs = parse_handle (Grew_parser.new_grs Grew_lexer.global) lexbuf in
close_in in_ch;
grs
with Sys_error msg -> Error.parse ~loc:(Loc.file file) "[Grew_loader.Loader.grs] %s" msg
(* ------------------------------------------------------------------------------------------*)
let gr file =
try
......
......@@ -18,6 +18,8 @@ module Loader: sig
val grs: string -> Ast.grs
val new_grs: string -> New_ast.grs
val gr: string -> Ast.gr
val pattern: string -> Ast.pattern
......
......@@ -71,15 +71,18 @@ let localize t = (t,get_loc ())
%token DOMAIN /* domain */
%token INCL /* include */
%token IMPORT /* import */
%token FEATURES /* features */
%token FEATURE /* feature */
%token FILE /* file */
%token LABELS /* labels */
%token PATTERN /* match */
%token PATTERN /* pattern */
%token WITHOUT /* without */
%token COMMANDS /* commands */
%token MODULE /* module */
%token DETERMINISTIC /* deterministic (of deprecated confluent) */
%token STRAT /* strat */
%token PACKAGE /* package */
%token DETERMINISTIC /* deterministic (or deprecated confluent) */
%token RULE /* rule */
%token SEQUENCES /* sequences */
%token GRAPH /* graph */
......@@ -93,8 +96,14 @@ let localize t = (t,get_loc ())
%token ADD_NODE /* add_node */
%token DEL_FEAT /* del_feat */
%token PICK /* pick */
%token TRY /* try */
%token PICK /* Pick */
%token ALT /* Alt */
%token SEQ /* Seq */
%token ITER /* Iter */
%token IF /* If */
%token EMPTY /* Empty */
%token TRY /* Try */
%token RULES /* Rules */
%token <string> DOLLAR_ID /* $id */
%token <string> AROBAS_ID /* @id */
......@@ -119,6 +128,10 @@ let localize t = (t,get_loc ())
%start <Grew_ast.Ast.domain> domain
%start <Grew_ast.Ast.strat_def> strat_def
%start <Grew_ast.New_ast.grs> new_grs
/* 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é))))))" */
%start <Grew_ast.Ast.pst> phrase_structure_tree
......@@ -723,4 +736,43 @@ pst:
| LPAREN pos=ID ff=ID RPAREN { Grew_ast.Ast.T (get_loc(), pos, [Grew_ast.Ast.Leaf (get_loc(), ff)]) }
| LPAREN cat=ID daugthers=nonempty_list (pst) RPAREN { Grew_ast.Ast.T (get_loc(), cat, daugthers) }
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
/*=============================================================================================*/
new_grs:
| top_decls = list(top_decl) EOF { top_decls }
top_decl:
| f=features_group { New_ast.Features f }
| l=labels { New_ast.Labels l }
| d=decl { New_ast.D d }
decl:
| r=rule { New_ast.Rule r }
| IMPORT f=STRING { New_ast.Import f }
| INCL f=STRING { New_ast.Include f }
| PACKAGE id_loc=simple_id_with_loc LACC l=list(decl) RACC { New_ast.Package (fst id_loc, l) }
| STRAT id_loc=simple_id_with_loc LACC d = strat_desc RACC { New_ast.Strategy (fst id_loc, d) }
strat_desc:
| id = node_id { New_ast.Ref id }
| PICK LPAREN s=strat_desc RPAREN { New_ast.Pick s }
| ALT LPAREN sl=separated_list_final_opt(COMA,strat_desc) RPAREN { New_ast.Alt sl }
| SEQ LPAREN sl=separated_list_final_opt(COMA,strat_desc) RPAREN { New_ast.Seq sl }
| ITER LPAREN s=strat_desc RPAREN { New_ast.Iter s }
| IF LPAREN s1=strat_desc COMA s2=strat_desc COMA s3=strat_desc RPAREN { New_ast.If (s1,s2,s3) }
| EMPTY { New_ast.Empty }
| TRY LPAREN s=strat_desc RPAREN { New_ast.Try s }
| RULES LPAREN id=node_id RPAREN { New_ast.Rules id }
%%
......@@ -267,6 +267,19 @@ module Grs = struct
Yojson.Basic.pretty_to_string json
end
(* ==================================================================================================== *)
(** {2 New Graph Rewriting System} *)
(* ==================================================================================================== *)
module New_grs = struct
type t = Grew_grs.New_grs.t
let load file =
handle ~name:"New_grs.load" ~file
(fun () ->
Grew_grs.New_grs.load file
) ()
end
(* ==================================================================================================== *)
(** {2 Rewrite} *)
(* ==================================================================================================== *)
......
......@@ -127,6 +127,15 @@ module Grs: sig
val to_json: t -> string
end
(* ==================================================================================================== *)
(** {2 New Graph Rewriting System} *)
(* ==================================================================================================== *)
module New_grs : sig
type t
val load: string -> t
end
(* ==================================================================================================== *)
(** {2 Rewrite history} *)
(* ==================================================================================================== *)
......
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