gr_grs_parser.mly 18.9 KB
Newer Older
pj2m's avatar
pj2m committed
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
130
131
132
133
134
135
136
137
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
247
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
%{

open Ast
open Utils

(* Some intermediate sum types used in sub-functions when xsvbuilding the ast *)
type pat_item = 
  | Pat_node of node
  | Pat_edge of edge
  | Pat_const of const

type graph_item =
  | Graph_node of node
  | Graph_edge of edge

let get_loc () = (!Parser_global.current_file,!Parser_global.current_line+1)
let localize t = (t,get_loc ())
%}

%token LACC 			/* { */
%token RACC			/* } */
%token LBRACKET			/* [ */
%token RBRACKET			/* ] */
%token LPAREN			/* ( */
%token RPAREN			/* ) */
%token DDOT			/* : */
%token COMA			/* , */
%token SEMIC			/* ; */
%token STAR			/* * */
%token EQUAL			/* = */
%token DISEQUAL			/* <> */
%token PIPE			/* | */
%token GOTO_NODE		/* -> */
%token LTR_EDGE_LEFT		/* -[ */
%token LTR_EDGE_LEFT_NEG	/* -[^ */
%token LTR_EDGE_RIGHT		/* ]-> */
%token RTL_EDGE_LEFT		/* <-[ */
%token RTL_EDGE_RIGHT		/* ]- */
%token LONGARROW		/* ==> */

%token FEATURES			/* features */
%token LABELS			/* labels */
%token BAD_LABELS		/* bad_labels */
%token MATCH			/* match */
%token WITHOUT			/* without */
%token COMMANDS			/* commands */
%token MODULE			/* module */
%token CONFLUENT		/* confluent */
%token RULE			/* rule */
%token SEQUENCES		/* sequences */
%token GRAPH			/* graph */

%token DEL_EDGE			/* del_edge */
%token ADD_EDGE			/* add_edge */
%token MERGE			/* merge */
%token SHIFT			/* shift */
%token DEL_NODE			/* del_node */
%token ADD_NODE			/* add_node */
%token DEL_FEAT			/* del_feat */

%token <string> IDENT		/* indentifier */
%token <string> FEAT		/* ident.ident */
%token <string> STRING
%token <int> NUMBER
%token <string> COMMENT

%token EOF			/* end of file */

%start <Ast.grs> grs
%start <Ast.gr> gr

%%

/*=============================================================================================*/
/*  GREW GRAPH                                                                                 */
/*=============================================================================================*/

gr: 
	| GRAPH LACC items = separated_list(SEMIC,option(gr_item)) RACC EOF 
	    {
	     {
	      nodes = List_.opt_map (function Some (Graph_node n) -> Some n | _ -> None) items;
	      edges = List_.opt_map (function Some (Graph_edge n) -> Some n | _ -> None) items;
	    }
	   }
	  	
gr_item:
	| id = IDENT position = option(delimited(LPAREN,index,RPAREN)) feats = delimited(LBRACKET,separated_list(COMA,node_features),RBRACKET) 
	    (* { Graph_node (id, position, feats,(!Parser_global.current_file, !Parser_global.current_line+1)) } *)
	    { Graph_node (localize {node_id = id; position=position; fs=feats}) }

	| n1 = IDENT labels = delimited(LTR_EDGE_LEFT_NEG,separated_nonempty_list(PIPE,IDENT),LTR_EDGE_RIGHT) n2 = IDENT
	    (* { Graph_edge (None, (n1,n2,true,labels),(!Parser_global.current_file,!Parser_global.current_line+1)) } *)
	    { Graph_edge (localize {edge_id = None; src=n1; edge_labels=labels; tar=n2; negative=true; }) }

	| n1 = IDENT labels = delimited(LTR_EDGE_LEFT,separated_nonempty_list(PIPE,IDENT),LTR_EDGE_RIGHT) n2 = IDENT
	    (* { Graph_edge (None, (n1,n2,false,labels),(!Parser_global.current_file,!Parser_global.current_line+1)) } *)
	    { Graph_edge (localize {edge_id = None; src=n1; edge_labels=labels; tar=n2; negative=false; }) }

index:
	| NUMBER { $1 }










/*=============================================================================================*/
/*  GREW GRAPH SYSTEM REWRITING                                                                */
/*=============================================================================================*/


grs:
	| f = features_group g = global_labels m = modules s = sequences EOF {
		{ Ast.domain=List_.opt f; 
		labels=g; 
		modules=m; 
		sequences=s }
	}



/*=============================================================================================*/
/*                                                                                             */
/* FEATURES DOMAIN DEFINITION                                                                  */
/*                                                                                             */
/* features {                                                                                  */
/*      cat: n, np, v, adj;                                                                    */
/*      mood: inf, ind, subj, pastp, presp;                                                    */
/*      lemma: *; phon: *                                                                      */
/* }                                                                                           */
/*                                                                                             */
/*=============================================================================================*/

features_group:
	| FEATURES x = features { x }
	
%inline features:
	| LACC x = separated_nonempty_list(SEMIC,option(feature)) RACC { x }
	
%inline feature:
	| name = feature_name DDOT values = features_values { 
			if (List.length values == 1 && List.hd values = "*") then (
				Open name
			) else (
				Closed (name,List.sort Pervasives.compare values)
			)
	 }

feature_name:
	| word = IDENT { word }

features_values:
	| STAR { ["*"] }
	| x = separated_nonempty_list(COMA,feature_value) { x }


/*=============================================================================================*/
/*                                                                                             */
/* GLOBAL LABELS DEFINITION                                                                    */
/*                                                                                             */
/* labels { OBJ, SUBJ, DE_OBJ, ANT }                                                           */
/*                                                                                             */
/*=============================================================================================*/

%inline labels:
	| x = delimited(LACC,separated_nonempty_list(COMA,label),RACC) { x }
	
%inline label:
	| x = IDENT color = option(ddot_color)  { (x, color) }

ddot_color:
	| DDOT color = IDENT { color }

global_labels:
	| LABELS x = labels { x }


/*=============================================================================================*/
/*                                                                                             */
/* MODULE DEFINITION                                                                           */
/*                                                                                             */
/* module p7_to_p7p-mc {                                                                       */
/*   ...                                                                                       */
/* }                                                                                           */
/*                                                                                             */
/*=============================================================================================*/

modules:
	| x = list(grew_module) { x }
	
grew_module: 
	| doc = option(module_doc) MODULE conf = boption(CONFLUENT) id = module_id LACC l = option(local_labels) b = option(local_bad_labels) r = option(rules) RACC 
	   {
	    { Ast.module_id = fst id; 
	      local_labels = begin match l with None -> [] | Some l -> l; end;
	      bad_labels = begin match b with None -> [] | Some b -> b; end;
	      rules = begin match r with None -> [] | Some r -> r; end;
	      confluent = conf;
	      module_doc = (match doc with Some d -> d | None -> "");
	      mod_loc = get_loc (); 
	    }
	  }

module_id:
	| id = IDENT { (id,!Parser_global.current_line+1) }

module_doc:
	| comment = COMMENT { comment } 

/*=============================================================================================*/
/*                                                                                             */
/* LOCAL LABELS DEFINITION                                                                     */
/*                                                                                             */
/* labels {ANT_TMP}                                                                            */
/*                                                                                             */
/* bad_labels { SUJ, OBJ }                                                                     */
/*=============================================================================================*/



local_labels:
	| LABELS x = labels { x }

local_bad_labels:
	| BAD_LABELS x = bad_labels { x }
	 
%inline bad_labels:
	| x = delimited(LACC,separated_nonempty_list(COMA,IDENT),RACC) { x }



/*=============================================================================================*/
/*                                                                                             */
/* RULES DEFINITION                                                                            */
/*                                                                                             */
/* rule ant_prorel_init {                                                                      */
/*   ...                                                                                       */
/* }                                                                                           */
/*=============================================================================================*/

rules:
	| r = list(rule) { r }

rule: 
	| doc = option(rule_doc) RULE id = rule_id LACC p = pos_item n = list(neg_item) cmds = commands RACC 
	    { 
		{ Ast.rule_id = fst id; 
		pos_pattern = p;
		neg_patterns = n;
		commands = List_.opt cmds; 
		rule_doc = begin match doc with Some d -> d | None -> "" end;
		rule_loc = (!Parser_global.current_file,snd id);
		}
	    }	 

pos_item:
	| MATCH i = pn_item { i }

neg_item:
	| WITHOUT i = pn_item { i }
	  
pn_item: 
	(* | LACC l = list (pat_item) RACC *)
	| l = delimited(LACC,separated_nonempty_list(SEMIC,option(pat_item)),RACC)
	    {
	     {
	      Ast.pat_nodes = List_.opt_map (function Some (Pat_node n) -> Some n | _ -> None) l;
	      Ast.pat_edges = List_.opt_map (function Some (Pat_edge n) -> Some n | _ -> None) l;
	      Ast.pat_const = List_.opt_map (function Some (Pat_const n) -> Some n | _ -> None) l;
	    }
	   }
	
rule_id:
	| id = IDENT { (id,!Parser_global.current_line+1) }

rule_doc:
	| comment = COMMENT { comment } 
	
/*=============================================================================================*/
/*                                                                                             */
/* MATCH DEFINITION                                                                            */
/*                                                                                             */
/* match {                                                                                     */
/*      P [cat = prorel];                                                                      */
/*      N -> P;                                                                                */
/*                                                                                             */
/*      without { P -[ANT]-> * }                                                               */
/*      without { P -[ANT_TMP]-> * }                                                           */
/*    }                                                                                        */
/*                                                                                             */
/*=============================================================================================*/


pat_item:
	| n = node { Pat_node n }
	| e = full_edge { Pat_edge e }
	| c = full_const { Pat_const c }

node:
	| id = IDENT feats = delimited(LBRACKET,separated_list(COMA,node_features),RBRACKET) 
	    { localize ({node_id = id; position=None; fs= feats}) }




node_features:
	| name = IDENT EQUAL STAR 
	    { localize {kind = Disequality; name=name; values=[]; } } 
	| name = IDENT EQUAL values = separated_nonempty_list(PIPE,feature_value) 
	    { localize {kind = Equality; name=name; values=values; } } 
	| name = IDENT DISEQUAL values = separated_nonempty_list(PIPE,feature_value) 
	    { localize {kind = Disequality; name=name; values=values; } } 

feature_value:
	| v = IDENT { v }
	| v = STRING { v }

full_edge:
	(* "e: A -> B" *)
	| id = edge_id n1 = IDENT GOTO_NODE n2 = IDENT
	    (* { (Some id, (n1,n2,true,[]),(!Parser_global.current_file,!Parser_global.current_line+1)) } *)
	    { localize ({edge_id = Some id; src=n1; edge_labels=[]; tar=n2; negative=true}) }

	(* "A -> B" *)
	| n1 = IDENT GOTO_NODE n2 = IDENT
 	    (* { (None, (n1,n2,true,[]),(!Parser_global.current_file,!Parser_global.current_line+1)) } *)
	    { localize ({edge_id = None; src=n1; edge_labels=[]; tar=n2; negative=true}) }

	(* "e: A -[^X|Y]-> B" *)
	| id = edge_id n1 = IDENT labels = delimited(LTR_EDGE_LEFT_NEG,separated_nonempty_list(PIPE,IDENT),LTR_EDGE_RIGHT) n2 = IDENT
            (* { (Some id, (n1,n2,true,labels),(!Parser_global.current_file,!Parser_global.current_line+1)) } *)
	    { localize ({edge_id = Some id; src=n1; edge_labels=labels; tar=n2; negative=true}) }
	    
	(* "A -[^X|Y]-> B"*)
	| n1 = IDENT labels = delimited(LTR_EDGE_LEFT_NEG,separated_nonempty_list(PIPE,IDENT),LTR_EDGE_RIGHT) n2 = IDENT
            (* { (None, (n1,n2,true,labels),(!Parser_global.current_file,!Parser_global.current_line+1)) } *)
	    { localize ({edge_id = None; src=n1; edge_labels=labels; tar=n2; negative=true}) }

	(* "e: A -[X|Y]-> B" *)
	| id = edge_id n1 = IDENT labels = delimited(LTR_EDGE_LEFT,separated_nonempty_list(PIPE,IDENT),LTR_EDGE_RIGHT) n2 = IDENT
            (* { (Some id, (n1,n2,false,labels),(!Parser_global.current_file,!Parser_global.current_line+1)) } *)
	    { localize ({edge_id = Some id; src=n1; edge_labels=labels; tar=n2; negative=false}) }       

	(* "A -[X|Y]-> B" *)
	| n1 = IDENT labels = delimited(LTR_EDGE_LEFT,separated_nonempty_list(PIPE,IDENT),LTR_EDGE_RIGHT) n2 = IDENT
            (* { (None, (n1,n2,false,labels),(!Parser_global.current_file,!Parser_global.current_line+1)) } *)
	    { localize ({edge_id = None; src=n1; edge_labels=labels; tar=n2; negative=false}) }


edge_id:
	| id = IDENT DDOT { id }
		

full_const:
	| n1 = IDENT labels = delimited(LTR_EDGE_LEFT,separated_nonempty_list(PIPE,IDENT),LTR_EDGE_RIGHT) STAR
	    { localize (Start (n1,labels)) }
	| STAR labels = delimited(LTR_EDGE_LEFT,separated_nonempty_list(PIPE,IDENT),LTR_EDGE_RIGHT) n2 = IDENT
	    { localize (End (n2,labels)) }


/*=============================================================================================*/
/*                                                                                             */
/* COMMANDS DEFINITION                                                                         */
/*                                                                                             */
/* commands {                                                                                  */
/*      del_edge A -[OBJ]-> B;                                                                 */
/*      del_edge e;                                                                            */
/*                                                                                             */
/*      add_edge A -[ATS]-> B;                                                                 */
/*                                                                                             */
/*      shift A ==> B;                                                                         */
/*                                                                                             */
/*      del_node A;                                                                            */
/*                                                                                             */
/*      add_node D: <-[SUJ]- A;                                                                */
/*                                                                                             */
/*      A.mood = inf;                                                                          */
/*                                                                                             */
/*      B.tense = A.tense;                                                                     */
/*                                                                                             */
/*      del_feat A.tense;                                                                      */
/* }                                                                                           */
/*                                                                                             */
/*=============================================================================================*/


commands:
	| COMMANDS x = delimited(LACC,separated_nonempty_list(SEMIC,option(command)),RACC) { x }

command:
	| DEL_EDGE n = IDENT
	    { localize (Del_edge_name n) }
	| DEL_EDGE n1 = IDENT label = delimited(LTR_EDGE_LEFT,IDENT,LTR_EDGE_RIGHT) n2 = IDENT 
	    { localize (Del_edge_expl (n1,n2,label)) }
	| ADD_EDGE n1 = IDENT label = delimited(LTR_EDGE_LEFT,IDENT,LTR_EDGE_RIGHT) n2 = IDENT
	    { localize (Add_edge (n1,n2,label)) }
	| SHIFT n1 = IDENT LONGARROW n2 = IDENT 
	    { localize (Shift_edge (n1,n2)) }
	| MERGE n1 = IDENT LONGARROW n2 = IDENT 
	    { localize (Merge_node (n1,n2)) }
	| DEL_NODE n = IDENT 
	    { localize (Del_node n) }
	| ADD_NODE n1 = IDENT DDOT label = delimited(RTL_EDGE_LEFT,IDENT,RTL_EDGE_RIGHT) n2 = IDENT 
	    { localize (New_neighbour (n1,n2,label)) }
	| DEL_FEAT feat = FEAT 
	    { localize (Del_feat feat) }
	| feat1 = FEAT EQUAL feat2 = FEAT 
	    { localize (Copy_feat (feat1, feat2)) }
	| feat = FEAT EQUAL value = feature_value 
	    { localize (New_feat (feat, value)) }

/*=============================================================================================*/
/*                                                                                             */
/* SEQUENCE DEFINITION                                                                         */
/*                                                                                             */
/* sequence { ant; p7_to_p7p-mc}                                                               */
/*                                                                                             */
/*=============================================================================================*/

sequences:
	| SEQUENCES seq = delimited(LACC,list(sequence),RACC) { seq }

sequence:
	| doc = option(COMMENT) id = sequence_id mod_names = delimited(LACC,separated_nonempty_list(SEMIC,option( IDENT)),RACC) { 
		{Ast.seq_name = fst id; 
		seq_mod = List_.opt mod_names ; 
		seq_doc = begin match doc with Some d -> d | None -> "" end; 
		seq_loc = (!Parser_global.current_file,snd id);
		} 
	}

sequence_id:
	| id = IDENT { (id,!Parser_global.current_line+1) }

%%