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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
let log_channel = ref stdout
let log_stack_channel = ref stdout
_log = open_out "debug_dypgen.txt"*)
let dypgen_verbose = ref 0
let list_append l1 l2 = List.rev_append (List.rev l1) l2
let (@) l1 l2 = list_append l1 l2
type token_name = int
type regexp =
| RE_Char of char
| RE_Char_set of (char * char) list
| RE_Char_set_exclu of (char * char) list
| RE_String of string
| RE_Alt of regexp list
| RE_Seq of regexp list
| RE_Star of regexp
| RE_Plus of regexp
| RE_Option of regexp
| RE_Name of string
| RE_Eof_char
include Priority_by_relation
type symb =
| Ter of string
| Ter_NL of string
| Non_ter of string * (string nt_prio)
| Non_ter_NL of string * (string nt_prio)
| Regexp of regexp
| Regexp_NL of regexp
type ('a,'b) psymbol =
| Ps_Ter of 'b
| Ps_Ter_NL of 'b
| Ps_Non_ter of 'a
| Ps_Non_ter_NL of 'a
type ('lhs,'nt_lit) prule_bis = 'lhs * (('nt_lit,int) psymbol array)
type 'nt_lit p_rhs = ('nt_lit,int) psymbol array
type non_ter = int
type rule_options = No_layout_inside | No_layout_follows
type rule = string * (symb list) * string * rule_options list
type nlit = ((non_ter * (priority nt_prio)),int) psymbol
type nrule = non_ter * (nlit list) * priority * int
module Ordered_non_ter =
struct
type t = non_ter
let compare = Stdlib.compare
end
module Nt_map = Map.Make(Ordered_non_ter)
type ('obj,'gd,'ld) merge_function =
('obj * 'gd * 'ld) list -> ('obj list) * 'gd * 'ld
let keep_all l = match l with
| (_,gd,ld)::_ -> List.map (fun (o,_,_) -> o) l, gd, ld
| [] -> assert false
let keep_one l = match l with
| (o,gd,ld)::_ -> [o], gd, ld
| [] -> assert false
let dummy_lexbuf_position _ = (Lexing.dummy_pos, Lexing.dummy_pos)
module Ordered_int =
struct
type t = int
let compare = Stdlib.compare
end
module Int_set = Set.Make(Ordered_int)
module Int_map = Map.Make(Ordered_int)
type lhs = non_ter * priority * int
type lit_nt = non_ter * (priority nt_prio)
let ind_of_lhs (_, _, i) = i
let str_lhs (nt, _, _) str_non_ter = str_non_ter.(nt)
type literal = (lit_nt, int) psymbol
type rule_bis = (lhs, lit_nt) prule_bis
type rhs = lit_nt p_rhs
module Ordered_rhs =
struct
type t = rhs * int
let compare l1 l2 =
Stdlib.compare l1 l2
end
module Map_rhs = Map.Make (Ordered_rhs)
(** [int] is the dot position in the item *)
type item = (int * int)
type item_rhs = rhs * int
module Ordered_items =
struct
type t = item
let compare = Stdlib.compare
end
module Item_map = Map.Make(Ordered_items)
module Ordered_token_name =
struct
type t = token_name
let compare = Stdlib.compare
end
module TNS = Set.Make (Ordered_token_name)
module Ordered_intc =
struct
type t = int * int
let compare = Stdlib.compare
end
module Intc_map = Map.Make(Ordered_intc)
module Intc_set = Set.Make(Ordered_intc)
module Ordered_predict =
struct
type t = (non_ter * (priority nt_prio))
let compare = Stdlib.compare
end
module Predict = Set.Make(Ordered_predict)
type item_set = {
mutable reducible : Int_set.t;
mutable kernel_nt : Intc_set.t;
mutable kernel_t : Intc_set.t;
mutable non_kernel : int list;
mutable predict : Predict.t
}
let new_item_set () = {
reducible = Int_set.empty;
kernel_nt = Intc_set.empty;
kernel_t = Intc_set.empty;
non_kernel = [];
predict = Predict.empty
}
let dummy_item_set = {
reducible = Int_set.empty;
kernel_nt = Intc_set.empty;
kernel_t = Intc_set.empty;
non_kernel = [];
predict = Predict.empty
}
let copy_item_set is = {
reducible = is.reducible;
kernel_nt = is.kernel_nt;
kernel_t = is.kernel_t;
non_kernel = is.non_kernel;
predict = is.predict
}
let is_size = ref []
let avg_list l =
let w = 1. /. (float_of_int (List.length l)) in
List.fold_left (fun r x -> r +. w *. x) 0. l
let incr_is_size is =
let f _ s = s +. 2. in
let s = Int_set.fold (fun _ s -> s+.1.) is.reducible 0. in
let s = Intc_set.fold f is.kernel_t s in
let s = Intc_set.fold f is.kernel_nt s in
let s = List.fold_left (fun s _ -> s+.1.) s is.non_kernel in
let s = Predict.fold (fun _ s -> s+.3.) is.predict s in
is_size := s::!is_size
let size_of_is is =
let f _ s = s +. 2. in
let s = Int_set.fold (fun _ s -> s+.1.) is.reducible 0. in
let s = Intc_set.fold f is.kernel_t s in
let s = Intc_set.fold f is.kernel_nt s in
let s = List.fold_left (fun s _ -> s+.1.) s is.non_kernel in
Predict.fold (fun _ s -> s+.3.) is.predict s
let compare_is is1 is2 =
let c = Int_set.compare is1.reducible is2.reducible in
if c<>0 then c else
let c = Intc_set.compare is1.kernel_t is2.kernel_t in
if c<>0 then c else
let c = Intc_set.compare is1.kernel_nt is2.kernel_nt in
if c<>0 then c else
let c = Stdlib.compare is1.non_kernel is2.non_kernel in
if c<>0 then c else Predict.compare is1.predict is2.predict
let soc c = Char.escaped c
let pr_cil l =
String.concat ";"
(List.map (fun (a,b) -> "('"^(soc a)^"','"^(soc b)^"')") l)
let pr_pretty_cil l =
String.concat ""
(List.map (fun (a,b) ->
if a=b then "'"^(soc a)^"'" else
"'"^(soc a)^"'-'"^(soc b)^"'") l)
let rec print_regexp = function
| RE_Char c -> "Dyp.RE_Char '"^(soc c)^"'"
| RE_Char_set l -> "Dyp.RE_Char_set ["^(pr_cil l)^"]"
| RE_Char_set_exclu l -> "Dyp.RE_Char_set_exclu ["^(pr_cil l)^"]"
| RE_String s -> "Dyp.RE_String \""^(String.escaped s)^"\""
| RE_Alt rl -> let sl = List.map print_regexp rl in
"Dyp.RE_Alt ["^(String.concat ";" sl)^"]"
| RE_Seq rl -> let sl = List.map print_regexp rl in
"Dyp.RE_Seq ["^(String.concat ";" sl)^"]"
| RE_Star r -> "Dyp.RE_Star ("^(print_regexp r)^")"
| RE_Plus r -> "Dyp.RE_Plus ("^(print_regexp r)^")"
| RE_Option r -> "Dyp.RE_Option ("^(print_regexp r)^")"
| RE_Name n -> "Dyp.RE_Name \""^n^"\""
| RE_Eof_char -> "Dyp.RE_Eof_char"
let rec print_pretty_regexp = function
| RE_Char c -> "'"^(soc c)^"'"
| RE_Char_set l -> "["^(pr_pretty_cil l)^"]"
| RE_Char_set_exclu l -> "[^"^(pr_pretty_cil l)^"]"
| RE_String s -> "\""^(String.escaped s)^"\""
| RE_Alt rl -> let sl = List.map print_pretty_regexp rl in
String.concat "|" sl
| RE_Seq rl -> let sl = List.map print_pretty_regexp rl in
String.concat " " sl
| RE_Star r -> "("^(print_pretty_regexp r)^")*"
| RE_Plus r -> "("^(print_pretty_regexp r)^")+"
| RE_Option r -> "("^(print_pretty_regexp r)^")?"
| RE_Name n -> n
| RE_Eof_char -> "eof"
let str_literal symb str_non_ter str_ter regexp_array = match symb with
| Ps_Ter tk -> (try str_ter.(tk) with _ ->
(Printf.sprintf "<%s>"
(print_pretty_regexp regexp_array.(tk-(Array.length str_ter)))))
| Ps_Ter_NL tk -> (try "- "^str_ter.(tk) with _ ->
(Printf.sprintf "- <%s>"
(print_pretty_regexp regexp_array.(tk-(Array.length str_ter)))))
| Ps_Non_ter (nt,_) -> str_non_ter.(nt)
| Ps_Non_ter_NL (nt,_) -> "- "^str_non_ter.(nt)
let str_handle litl dp str_non_ter str_ter regexp_array =
let rec aux i s =
let s1 = if dp=i then s^"." else s in
if i=Array.length litl then s1
else aux (i+1) (s1^(str_literal litl.(i)
str_non_ter str_ter regexp_array)^" ")
in
aux 0 ""
let str_token_set tns str_ter =
let f tn str = str^(try str_ter.(tn) with _ -> (Printf.sprintf "<regexp:%d>" tn))^"," in
let str = TNS.fold f tns "" in
if str = "" then "" else
let string_length = (String.length str) in
String.sub str 0 (string_length-1)
let print_reducible chan gram_rhs lhs_table str_non_ter str_ter regexp_array rn =
let lhs,rhs = lhs_table.(rn), gram_rhs.(rn) in
Printf.fprintf chan " %s -> %s\n"
(str_lhs lhs str_non_ter)
(str_handle rhs (Array.length rhs) str_non_ter str_ter regexp_array)
let print_kernel chan gram_rhs lhs_table str_non_ter str_ter regexp_array (rn,dp) =
let lhs,rhs = lhs_table.(rn), gram_rhs.(rn) in
Printf.fprintf chan " %s -> %s\n"
(str_lhs lhs str_non_ter) (str_handle rhs dp str_non_ter str_ter regexp_array)
let print_non_kernel chan gram_rhs lhs_table str_non_ter str_ter regexp_array rn =
print_kernel chan gram_rhs lhs_table str_non_ter str_ter regexp_array (rn,0)
let print_item chan nt_of_ind prio_of_ind ind str_non_ter str_ter regexp_array (rhs,dp) =
Printf.fprintf chan " %s -> %s\n"
(str_lhs (nt_of_ind.(ind),prio_of_ind.(ind),ind) str_non_ter)
(str_handle rhs dp str_non_ter str_ter regexp_array)
module Ordered_ntp =
struct
type t = priority nt_prio
let compare = Stdlib.compare
end
module Ntp_map = Map.Make(Ordered_ntp)
(** this type is used to construct the automaton. Each state in the automaton
has a field of type [lit_trans] which is the literal of transition to
this state. The difference with the type [literal] is that there is no
priority attached to the non terminals. *)
type lit_trans = (non_ter, int) psymbol
let lit_trans (symb:(lit_nt,int) psymbol) = match symb with
| Ps_Non_ter (nt, _) -> Ps_Non_ter nt
| Ps_Ter t -> Ps_Ter t
| Ps_Non_ter_NL (nt, _) -> Ps_Non_ter_NL nt
| Ps_Ter_NL t -> Ps_Ter_NL t
module Ordered_lit_trans=
struct
type t = lit_trans
let compare = Stdlib.compare
end
module Map_lit_trans = Map.Make(Ordered_lit_trans)
module Ordered_item_set =
struct
type t = item_set * int
let compare (is1,n1) (is2,n2) =
if n1>n2 then 1
else if n2>n1 then -1 else
let c = Int_set.compare is1.reducible is2.reducible in
if c<>0 then c else
let c = Intc_set.compare is1.kernel_nt is2.kernel_nt in
if c<>0 then c else
Intc_set.compare is1.kernel_t is2.kernel_t
end
module Map_is = Map.Make(Ordered_item_set)
type state = {
number : int;
li : lit_trans;
items : item_set;
mergeable : bool;
bestowing_nt : int option;
mutable succ_states : (state * priority) list;
}
module Ordered_States =
struct
type t = state
let compare s1 s2 = Stdlib.compare s1.number s2.number
end
module State_set = Set.Make(Ordered_States)
type debug_infos = {
prt_state : out_channel -> unit;
prt_grammar : out_channel -> unit; }
let size_of_state_list l =
List.fold_left (fun s v -> s +. (size_of_is v.items)) 0. l
module Dyp_special_types =
struct
type is_trace =
((state Map_is.t) array * (state Map_is.t) array) *
((state Map_is.t) array * (state Map_is.t) array)
type array_nt_prio = int Prio_map.t array
end
type nt_cons_map = int String_map.t
type node = {
id : int;
mutable trans : ((int * int) list * node) list;
mutable eps : node list;
matched : Int_set.t }
type lex_table = {
tbl_trans : int array;
tbl_final : int list array;
tbl_notrans : bool array }
type bool_option = True | False | NA
type ('t,'o,'gd,'ld,'l) action =
Dypgen_action of ( 'o list -> (Lexing.position * Lexing.position) ->
(Lexing.position * Lexing.position) list -> 'gd ->
'ld -> 'ld -> debug_infos ->
('t,'o,'gd,'ld,'l) parser_pilot ->
(unit -> string list) ->
('o * bool * bool *
'gd * 'ld *
((rule * ('t,'o,'gd,'ld,'l) action *
(int * ('t,'o,'gd,'ld,'l) inherited_val) list) list) *
(string * string) list *
string list list *
(out_channel option) * (out_channel option) *
('t,'o,'gd,'ld,'l) parsing_device option) )
and ('t,'o,'gd,'ld,'l) inherited_val =
'o list -> (Lexing.position * Lexing.position) ->
(Lexing.position * Lexing.position) list -> 'gd ->
'ld -> 'ld -> debug_infos ->
('t,'o,'gd,'ld,'l) parser_pilot ->
(unit -> string list) ->
'o
and ('token,'obj,'data,'local_data,'lexbuf) parser_parameters = {
merge_warning : bool;
undef_nt : bool;
get_value : 'token -> 'obj;
get_name : 'token -> int;
str_token : 'token -> string;
global_data_equal : 'data -> 'data -> bool;
local_data_equal : 'local_data -> 'local_data -> bool;
find_rightSib_global_data_equal : 'data -> 'data -> bool;
find_rightSib_local_data_equal : 'local_data -> 'local_data -> bool;
test_cons : ('obj -> bool) array;
str_cons : 'obj -> string;
cons_str : string array;
cons_table : (string, int) Hashtbl.t;
merge_array : ('obj,'data,'local_data) merge_function array;
lexbuf_position_fun : 'lexbuf -> (Lexing.position * Lexing.position);
regexp_fun : 'obj dyplexbuf -> 'obj;
use_dyplex : bool;
use_rule_order : bool;
use_all_actions : bool;
main_lexer_action_nb : int
}
and ('t,'o,'gd,'ld,'l) parsing_device = {
ra_list :
(rule * ('t,'o,'gd,'ld,'l) action *
(int * ('t,'o,'gd,'ld,'l) inherited_val) list)
list;
gram_lhs : ((int list) * (int option)) array;
gram_rhs : rhs array;
gram_parnt : (int * int) list array;
lhs_table : lhs array;
bnt_array : bool array;
actions : ('t,'o,'gd,'ld,'l) action list array;
inherited :
(('t,'o,'gd,'ld,'l) inherited_val list * int * int * int * bool) array;
entry_point : int;
entry_points : (int, state) Hashtbl.t;
g_nb : int;
lex_nb : int;
nt_table : (string, int) Hashtbl.t;
stations : state option array;
state_list : state list;
is_trace : Dyp_special_types.is_trace;
st_nb : int;
table : (int, int) Hashtbl.t array array;
table_it : item_set array;
state_is_mergeable : bool array;
state_bnt : int option array;
table_lit_trans : lit_trans array;
r_L : int list array;
po : ((int * int), bool) Hashtbl.t ;
(** Partial order between non terminal name couples. A couple of non
terminals is bound to a bool. This may be implemented alternatively
with a set : the presence of a couple (nt1,nt2) in the set would
denote that nt1<=nt2 is true. This would save memory space. *)
cyclic_rules : int list array;
token_nb : int;
nt_nb : int;
str_non_ter : string array;
str_non_ter_prio : (string * string) array;
cons_of_nt : int array;
relations : string list list;
nt_cons_map : nt_cons_map;
rn_of_rule : (rule, int) Hashtbl.t;
regexp_decl : (string, (node -> int -> node * int)) Hashtbl.t;
regexp_decl_list : (string * regexp) list;
main_lexer_start : node;
main_lexer_table : lex_table;
main_lexer_actions : ('o dyplexbuf -> 'o) array;
main_lexer_ter_id : int array;
main_lexer_init_id : int;
aux_lexer : 'o aux_lexer;
str_ter : string array;
ter_table : (string, int) Hashtbl.t;
layout_id : int;
regexp_table : (regexp, int) Hashtbl.t;
regexp_array : regexp array;
rule_options : int array;
nt_ntl_array : Int_set.t array;
implicit_rule : bool array;
left_rec_rule : bool array;
}
and ('t,'o,'gd,'ld,'l) parser_pilot = {
pp_dev : ('t,'o,'gd,'ld,'l) parsing_device;
pp_par : ('t,'o,'gd,'ld,'l) parser_parameters;
pp_gd : 'gd;
pp_ld : 'ld }
and 'o aux_lexer = {
aux_lexer_start : (string, node) Hashtbl.t;
aux_lexer_table : (string, lex_table) Hashtbl.t;
aux_lexer_actions : (string, ('o list -> 'o dyplexbuf -> 'o) array) Hashtbl.t
}
and 'o dyplexbuf = {
lb_lexbuf : Lexing.lexbuf;
lb_aux_lex : 'o aux_lexer }
type ('t,'o,'gd,'ld,'l) grammar = ((('t,'o,'gd,'ld,'l) action) list Map_rhs.t) array
let dummy_inherited _ _ _ _ _ _ _ _ _ = assert false
(** STRING functions used to print the states of the automaton *)
let str_literal_trans symb str_non_ter str_ter = match symb with
| Ps_Ter tk -> if tk= -1 then "#station" else
(try str_ter.(tk) with _ -> (Printf.sprintf "<regexp:%d>" tk))
| Ps_Ter_NL tk -> if tk= -1 then assert false else
(try "- "^str_ter.(tk) with _ -> (Printf.sprintf "- <regexp:%d>" tk))
| Ps_Non_ter nt -> str_non_ter.(nt)
| Ps_Non_ter_NL nt -> "- "^str_non_ter.(nt)
let rec str_tok_list ll str_non_ter str_ter regexp_array = match ll with
| [] -> ""
| [tok] -> str_literal (Ps_Ter tok) str_non_ter str_ter regexp_array
| tok::tl -> (str_literal (Ps_Ter tok) str_non_ter str_ter regexp_array)^","^
(str_tok_list tl str_non_ter str_ter regexp_array)
let str_state_succ succ_states str_non_ter str_ter =
let f str (state,_) =
str^" ["^(string_of_int state.number)^","^
(str_literal_trans state.li str_non_ter str_ter)^"]"
in
List.fold_left f "" succ_states
let print_item_set chan is gram_rhs lhs_table str_non_ter str_ter regexp_array =
Printf.fprintf chan " nb of items: %d\n"
((Int_set.cardinal is.reducible)+(Intc_set.cardinal is.kernel_nt)+
(Intc_set.cardinal is.kernel_t)+(List.length is.non_kernel));
Int_set.iter
(print_reducible chan gram_rhs lhs_table str_non_ter str_ter regexp_array)
is.reducible;
Intc_set.iter
(print_kernel chan gram_rhs lhs_table str_non_ter str_ter regexp_array)
is.kernel_t;
Intc_set.iter
(print_kernel chan gram_rhs lhs_table str_non_ter str_ter regexp_array)
is.kernel_nt;
List.iter
(print_non_kernel chan gram_rhs lhs_table str_non_ter str_ter regexp_array)
is.non_kernel
let str_predict predict str_non_ter =
Predict.fold
(fun (nt,_) str ->
str^(Printf.sprintf "(%s:%d)" str_non_ter.(nt) nt))
predict ""
let print_state s gram_rhs lhs_table str_non_ter str_ter regexp_array =
Printf.fprintf !log_channel " State %d\n" s.number;
Printf.fprintf !log_channel " li : %s\n"
(str_literal_trans s.li str_non_ter str_ter);
Printf.fprintf !log_channel " items :\n";
print_item_set !log_channel s.items gram_rhs lhs_table
str_non_ter str_ter regexp_array;
Printf.fprintf !log_channel " next states : %s\n"
(str_state_succ s.succ_states str_non_ter str_ter);
Printf.fprintf !log_channel " predict : %s\n"
(str_predict s.items.predict str_non_ter)
let rec str_lit_list litl str_non_ter str_ter regexp_array = match litl with
| [] -> ""
| symb::tl -> (str_literal symb str_non_ter str_ter regexp_array)^" "^
(str_lit_list tl str_non_ter str_ter regexp_array)
let str_rule (nt,litl,_) str_non_ter str_ter regexp_array =
(str_lhs nt str_non_ter)^" -> "^
(str_lit_list litl str_non_ter str_ter regexp_array)
let rec print_rule_list rl str_non_ter str_ter regexp_array = match rl with
| [] -> ()
| r::t -> Printf.fprintf !log_channel " %s\n"
(str_rule r str_non_ter str_ter regexp_array);
print_rule_list t str_non_ter str_ter regexp_array
let print_map m gram_rhs lhs_table str_non_ter str_ter regexp_array =
let f (is,_) s =
let () = Printf.fprintf !log_channel "state %d\n" (s.number) in
print_item_set !log_channel is gram_rhs lhs_table str_non_ter str_ter
regexp_array
in
Map_is.iter f m
let countst = ref 0
(** This ref counts the number of state creations. *)
let count_trans = ref 0
(** This ref counts the number of transitions between states. *)
let closure_v0_LR0 (is:item_set) _ gram_lhs nt_to_add non_kernel_array =
let g nk rn =
if non_kernel_array.(rn) then nk
else (non_kernel_array.(rn) <- true; rn::nk)
in
let f ind (nk, red) =
match gram_lhs.(ind) with
| rn_l, None ->
List.fold_left g nk rn_l, red
| rn_l, (Some rn) ->
List.fold_left g nk rn_l,
Int_set.add rn red
in
let non_kernel, reducible =
Int_set.fold f nt_to_add (is.non_kernel, is.reducible) in
List.iter (fun rn -> non_kernel_array.(rn) <- false) non_kernel;
is.reducible <- reducible;
is.non_kernel <- non_kernel
let lit_of_symb s = match s with
| Ps_Non_ter (nt,_,_) -> Ps_Non_ter nt
| Ps_Ter t -> Ps_Ter t
| Ps_Non_ter_NL (nt,_,_) -> Ps_Non_ter_NL nt
| Ps_Ter_NL t -> Ps_Ter_NL t
let ga_set a i v =
try !a.(i) <- v
with Invalid_argument e ->
if i<0 then raise (Invalid_argument e) else
let len = Array.length !a in
if i<2*len then
(let b = Array.init (2*len) (fun j -> if i<len then !a.(j) else false) in
b.(i) <- v; a := b)
else
(let b = Array.init (i+1) (fun j -> if i<len then !a.(j) else false) in
b.(i) <- v; a := b)
let ga_get a i =
try !a.(i) with Invalid_argument e ->
if i<0 then raise (Invalid_argument e) else false
let rec list_exists p = function
| a::t -> if p a then true else list_exists p t
| [] -> false
let is_mergeable is gram_parnt =
let f (rn,dp) = list_exists (fun (i, _) -> dp<i) gram_parnt.(rn) in
if Intc_set.exists f is.kernel_nt then false else
if Intc_set.exists f is.kernel_t then false else true
let print_bnt bnt =
print_endline "bestowing nt :";
Int_set.iter (fun i -> Printf.printf "%d " i) bnt;
print_newline ()
let make_bestowing_nt is gram_parnt =
let f (rn,dp) bnt =
try
let inh = List.assoc (dp+1) gram_parnt.(rn) in
Some inh
with Not_found -> bnt
in
Intc_set.fold f is.kernel_nt None
let close_state is_trace_tok is_trace_tok_nl is_trace_nt is_trace_nt_nl succ_states_array gram_rhs gram_lhs gram_parnt r_L non_kernel_array symb (vl, succ_states_list) is =
count_trans := !count_trans+1;
let it_nb =
(Int_set.cardinal is.reducible)
+ (Intc_set.cardinal is.kernel_nt)
+ (Intc_set.cardinal is.kernel_t)
in
let old_reducible = is.reducible in
try
let v1 = match symb with
| Ps_Ter t -> Map_is.find (is, it_nb) is_trace_tok.(t)
| Ps_Non_ter (nt, _, _) -> Map_is.find (is, it_nb) is_trace_nt.(nt)
| Ps_Ter_NL t -> Map_is.find (is, it_nb) is_trace_tok_nl.(t)
| Ps_Non_ter_NL (nt, _, _) -> Map_is.find (is, it_nb) is_trace_nt_nl.(nt)
in
let succ_states_list =
if ga_get succ_states_array (v1.number) then succ_states_list
else (ga_set succ_states_array (v1.number) true;
(v1, 0)::succ_states_list)
in
vl, succ_states_list
with Not_found ->
let f1 (rn, dp) (nt_to_add, predict) = match gram_rhs.(rn).(dp) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt ->
let g1 nt_to_add ind =
Int_set.add ind nt_to_add
in
let g2 nt_to_add (nt, _) =
let ind_list = r_L.(nt) in
List.fold_left g1 nt_to_add ind_list
in
(g2 nt_to_add nt), Predict.add nt predict
| _ -> assert false
in
let nt_to_add, predict =
Intc_set.fold f1 is.kernel_nt (Int_set.empty,Predict.empty)
in
let g nk rn =
if non_kernel_array.(rn) then nk
else (non_kernel_array.(rn) <- true; rn::nk)
in
let f ind (nk, red) = match gram_lhs.(ind) with
| rn_l, None ->
List.fold_left g nk rn_l, red
| rn_l, (Some rn) ->
List.fold_left g nk rn_l,
Int_set.add rn red
in
let non_kernel, reducible =
Int_set.fold f nt_to_add ([], is.reducible)
in
List.iter (fun rn -> non_kernel_array.(rn) <- false) non_kernel;
let predict =
List.fold_left
(fun predict rn -> try (match gram_rhs.(rn).(0) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt -> Predict.add nt predict
| _ -> predict)
with Invalid_argument _ -> assert false)
predict non_kernel
in
is.predict <- predict;
is.reducible <- reducible;
is.non_kernel <- non_kernel;
let v1 = {
li = lit_of_symb symb;
items = is;
number = !countst;
mergeable = is_mergeable is gram_parnt;
bestowing_nt = make_bestowing_nt is gram_parnt;
succ_states = [] }
in
incr countst;
let old_is = { is with reducible = old_reducible } in
let () = match symb with
| Ps_Ter t -> is_trace_tok.(t) <-
Map_is.add (old_is,it_nb) v1 is_trace_tok.(t)
| Ps_Non_ter (nt,_,_) -> is_trace_nt.(nt) <-
Map_is.add (old_is,it_nb) v1 is_trace_nt.(nt)
| Ps_Ter_NL t -> is_trace_tok_nl.(t) <-
Map_is.add (old_is,it_nb) v1 is_trace_tok_nl.(t)
| Ps_Non_ter_NL (nt,_,_) -> is_trace_nt_nl.(nt) <-
Map_is.add (old_is,it_nb) v1 is_trace_nt_nl.(nt)
in
ga_set succ_states_array (v1.number) true;
v1::vl, (v1, 0)::succ_states_list
let aux_move_nt array_lt_nt (is_list, lt_list) (nt, _) =
match array_lt_nt.(nt) with
| None ->
let is = new_item_set () in
array_lt_nt.(nt) <- Some is;
is::is_list, (Ps_Non_ter (nt, 0, nt))::lt_list
| Some is -> is::is_list, lt_list
let aux_move_nt_nl array_lt_nt_nl (is_list, lt_list, nl_list) (nt, _) =
match array_lt_nt_nl.(nt) with
| None ->
let is = new_item_set () in
array_lt_nt_nl.(nt) <- Some is;
let symb = Ps_Non_ter_NL (nt, 0, nt) in
let nl_list = (symb,is)::nl_list in
is::is_list, symb::lt_list, nl_list
| Some is -> is::is_list, lt_list, nl_list
let add_items_to_nl_state array_lt_ter array_lt_nt nl_list =
let add_to_is is0 = function
| None -> ()
| Some is1 ->
is0.kernel_nt <- Intc_set.union is0.kernel_nt is1.kernel_nt;
is0.kernel_t <- Intc_set.union is0.kernel_t is1.kernel_t;
is0.reducible <- Int_set.union is0.reducible is1.reducible
in
List.iter (function
| (Ps_Ter_NL t), is0 -> add_to_is is0 array_lt_ter.(t)
| (Ps_Non_ter_NL (_,_,ind)), is0 -> add_to_is is0 array_lt_nt.(ind)
| _ -> assert false) nl_list
let succ_states_count = ref 0
let move_LR0 s is_trace gram_rhs gram_lhs (gram_parnt:(int*int) list array) _ r_L array_lt_ter array_lt_ter_nl array_lt_nt array_lt_nt_nl _ succ_states_array non_kernel_array =
let (is_trace_tok, is_trace_tok_nl), (is_trace_nt, is_trace_nt_nl) =
is_trace in
let f1 (rn, dp) (lt_list, nl_list) =
let rhs = gram_rhs.(rn) in
let (is_list, lt_list), nl_list = match rhs.(dp) with
| (Ps_Ter t) as symb -> (match array_lt_ter.(t) with
| None ->
let is = new_item_set () in
array_lt_ter.(t) <- Some is;
([is], symb::lt_list), nl_list
| Some is -> ([is], lt_list), nl_list)
| Ps_Non_ter nt ->
(aux_move_nt array_lt_nt) ([], lt_list) nt, nl_list
| (Ps_Ter_NL t) as symb -> (match array_lt_ter_nl.(t) with
| None ->
let is = new_item_set () in
array_lt_ter_nl.(t) <- Some is;
let nl_list = (symb, is)::nl_list in
([is], symb::lt_list), nl_list
| Some is -> ([is], lt_list), nl_list)
| Ps_Non_ter_NL nt ->
let a, b, c =
(aux_move_nt_nl array_lt_nt_nl)
([], lt_list, nl_list) nt
in (a, b), c
in
List.iter
(if dp+1 = Array.length rhs then
(fun is -> is.reducible <- Int_set.add rn is.reducible)
else
match rhs.(dp+1) with
| Ps_Ter _ | Ps_Ter_NL _ ->
(fun is -> is.kernel_t <- Intc_set.add (rn, dp+1) is.kernel_t)
| Ps_Non_ter _ | Ps_Non_ter_NL _ ->
(fun is -> (is.kernel_nt <- Intc_set.add (rn, dp+1) is.kernel_nt)))
is_list;
lt_list, nl_list
in
let lt_list, nl_list = Intc_set.fold f1 s.items.kernel_nt ([], []) in
let lt_list, nl_list = Intc_set.fold f1 s.items.kernel_t (lt_list, nl_list) in
let f3 (lt_list, nl_list) rn = f1 (rn, 0) (lt_list, nl_list) in
let lt_list, nl_list =
List.fold_left f3 (lt_list, nl_list) s.items.non_kernel in
add_items_to_nl_state array_lt_ter array_lt_nt nl_list;
List.iter (fun (s, _) -> ga_set succ_states_array (s.number) true)
s.succ_states;
let f2 (vl, succ_states_list) symb =
let is_opt = match symb with
| Ps_Ter t -> array_lt_ter.(t)
| Ps_Non_ter (nt, _, _) -> array_lt_nt.(nt)
| Ps_Ter_NL t -> array_lt_ter_nl.(t)
| Ps_Non_ter_NL (nt, _, _) -> array_lt_nt_nl.(nt)
in
match is_opt with Some is ->
close_state
is_trace_tok is_trace_tok_nl is_trace_nt is_trace_nt_nl
succ_states_array gram_rhs gram_lhs gram_parnt
r_L non_kernel_array symb
(vl, succ_states_list) is
| None -> assert false
in
let vl, succ_states_list = List.fold_left f2 ([], s.succ_states) lt_list in
s.succ_states <- succ_states_list;
List.iter (fun (s, _) -> !succ_states_array.(s.number) <- false)
s.succ_states;
let clear_array symb = match symb with
| Ps_Ter t -> array_lt_ter.(t) <- None
| Ps_Non_ter (nt, _, _) -> array_lt_nt.(nt) <- None
| Ps_Ter_NL t -> array_lt_ter_nl.(t) <- None
| Ps_Non_ter_NL (nt, _, _) -> array_lt_nt_nl.(nt) <- None
in
List.iter clear_array lt_list;
vl
let init_is gram_rhs rule_list r_L non_kernel_array =
let is = new_item_set () in
let aux (non_kernel, nt_to_add) rn =
let non_kernel =
if non_kernel_array.(rn) then non_kernel
else (non_kernel_array.(rn) <- true; rn::non_kernel)
in
try (match gram_rhs.(rn).(0) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt ->
let g1 nt_to_add ind =
Int_set.add ind nt_to_add
in
let g2 nt_to_add ind =
let ind_list = r_L.(ind) in
List.fold_left g1 nt_to_add ind_list
in
non_kernel, g2 nt_to_add (fst nt)
| _ -> non_kernel,nt_to_add)
with Invalid_argument _ -> assert false
in
let non_kernel, nt_to_add =
List.fold_left aux ([], Int_set.empty) rule_list
in
is.non_kernel <- non_kernel;
is, nt_to_add
let merge_non_kernel nk1 nk2 non_kernel_array =
let f nk rn =
if non_kernel_array.(rn) then nk
else (non_kernel_array.(rn) <- true; rn::nk)
in
let g rn = non_kernel_array.(rn) <- false in
let nk = List.fold_left f [] nk1 in
let nk = List.fold_left f nk nk2 in
List.iter g nk;
nk
let map_succ gram_rhs gram_lhs gram_parnt bnt_array _ _ _ _ r_L array_lt_ter array_lt_ter_nl array_lt_nt array_lt_nt_nl array_lhs succ_states_array non_kernel_array is_trace_state_list vl =
let rec map_succ_aux (is_trace, state_list) = function
| v::tl ->
let vl =
move_LR0 v is_trace gram_rhs gram_lhs gram_parnt bnt_array r_L
array_lt_ter array_lt_ter_nl array_lt_nt
array_lt_nt_nl array_lhs succ_states_array non_kernel_array
in
map_succ_aux (is_trace, v::state_list) (vl@tl)
| [] -> is_trace, state_list
in
map_succ_aux is_trace_state_list vl
let make_entry_point_state entry_points stations _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ (is_trace, state_list) ep =
match stations.(ep) with Some s ->
(Hashtbl.add entry_points ep s;
is_trace, state_list)
| None -> assert false
let build_automaton_LR0 is_trace (gram_rhs:rhs array) (gram_lhs:((int list) * (int option)) array) gram_lhs' gram_parnt bnt_array _ r_L lhs_table _ token_nb str_non_ter str_ter entry_points_list regexp_array implicit_rule =
let array_lt_ter = Array.make token_nb None in
let array_lt_ter_nl = Array.make token_nb None in
let nt_nb = Array.length gram_lhs in
let array_lt_nt = Array.make nt_nb None in
let array_lt_nt_nl = Array.make nt_nb None in
let succ_states_array = ref (Array.make 10000 false) in
let non_kernel_array = Array.make (Array.length gram_rhs) false in
let array_lhs = Array.make nt_nb false in
let lhs_nb = Array.length gram_lhs in
let stations = Array.make lhs_nb None in
let rec map_nt n state_list =
if n>=lhs_nb then state_list else
let is, nt_to_add =
init_is gram_rhs (fst gram_lhs.(n)) r_L non_kernel_array
in
(match snd gram_lhs.(n) with
| None -> ()
| Some rn -> is.reducible <- Int_set.add rn Int_set.empty);
closure_v0_LR0 is gram_rhs gram_lhs' nt_to_add non_kernel_array;
is.non_kernel <- List.filter (fun rn -> not implicit_rule.(rn)) is.non_kernel;
let predict =
List.fold_left
(fun predict rn -> try (match gram_rhs.(rn).(0) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt -> Predict.add nt predict
| _ -> predict)
with Invalid_argument _ -> assert false)
Predict.empty is.non_kernel
in
let predict =
Intc_set.fold
(fun (rn,dp) predict -> try (match gram_rhs.(rn).(dp) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt -> Predict.add nt predict
| _ -> assert false)
with Invalid_argument _ -> assert false)
is.kernel_nt predict
in
is.predict <- predict;
let v = {
li = Ps_Ter (-1);
items = is;
number = n;
mergeable = true;
bestowing_nt = None;
succ_states = [] }
in
stations.(n) <- Some v;
map_nt (n+1) (v::state_list)
in
let state_list = map_nt 0 [] in
countst := nt_nb;
let map_succ is_trace_state_list vl =
map_succ gram_rhs gram_lhs' gram_parnt bnt_array lhs_table
str_non_ter
str_ter regexp_array r_L
array_lt_ter array_lt_ter_nl array_lt_nt
array_lt_nt_nl array_lhs succ_states_array non_kernel_array
is_trace_state_list vl
in
let is_trace, state_list = map_succ (is_trace, []) state_list in
let entry_points = Hashtbl.create (List.length entry_points_list) in
let is_trace, state_list =
List.fold_left
(make_entry_point_state entry_points stations gram_rhs gram_lhs
gram_parnt bnt_array lhs_table str_non_ter str_ter regexp_array r_L
array_lt_ter array_lt_ter_nl array_lt_nt
array_lt_nt_nl array_lhs succ_states_array non_kernel_array)
(is_trace, state_list) entry_points_list
in
is_trace, state_list, stations, entry_points
let build_automaton is_trace gram_rhs gram_lhs gram_lhs' (gram_parnt:(int * int) list array) bnt_array verbose it_nb r_L lhs_table ist_nt_nb token_nb str_non_ter str_ter entry_points_list regexp_array implicit_rule =
countst := 0;
count_trans := 0;
let time1 = Sys.time () in
let is_trace, state_list, stations, entry_points =
let is_trace, state_list, stations, entry_points =
build_automaton_LR0 is_trace gram_rhs gram_lhs gram_lhs'
gram_parnt bnt_array
it_nb r_L
lhs_table ist_nt_nb token_nb str_non_ter str_ter entry_points_list
regexp_array implicit_rule
in is_trace, state_list, stations, entry_points
in
let time2 = Sys.time () in
if verbose>0 then
(Printf.fprintf !log_channel
"LR(0) automaton built, %d states, %d transitions, %.3f sec\n"
(!countst) (!count_trans+1) (time2-.time1);
flush stdout) else ();
state_list, !countst, stations, entry_points, is_trace