-
Notifications
You must be signed in to change notification settings - Fork 2
/
grammar.h
2134 lines (1885 loc) · 70.1 KB
/
grammar.h
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
/**
* grammar.h
*
* Created on: Jul 15, 2015
* Author: asaparov
*/
#ifndef GRAMMAR_H_
#define GRAMMAR_H_
#include <math.h>
#include <limits>
#include <core/array.h>
#include <core/map.h>
#include <core/io.h>
#include <core/utility.h>
#include <math/features.h>
#define NEW_TERMINAL (UINT_MAX - 3)
using namespace core;
/* forward declarations */
template<typename Semantics> struct syntax_node;
template<typename T>
struct weighted {
T object;
double log_probability;
static inline void swap(weighted<T>& first, weighted<T>& second) {
core::swap(first.object, second.object);
core::swap(first.log_probability, second.log_probability);
}
static inline void move(const weighted<T>& src, weighted<T>& dst) {
core::move(src.object, dst.object);
dst.log_probability = src.log_probability;
}
static inline void free(weighted<T>& form) {
core::free(form.object);
}
};
template<typename T>
inline bool operator < (
const weighted<T>& first,
const weighted<T>& second)
{
/* we want to sort in descending order */
return first.log_probability > second.log_probability;
}
template<typename Semantics>
struct transformation {
typedef typename Semantics::function function;
function* functions;
unsigned int function_count;
static inline unsigned int hash(const transformation<Semantics>& t) {
unsigned int hash_value = default_hash(t.function_count);
for (unsigned int i = 0; i < t.function_count; i++)
hash_value ^= hasher<function>::hash(t.functions[i]);
return hash_value;
}
static inline void free(transformation<Semantics>& t) {
core::free(t.functions);
}
};
template<typename Semantics>
inline bool init(transformation<Semantics>& t, const transformation<Semantics>& src) {
typedef typename Semantics::function function;
t.function_count = src.function_count;
t.functions = (function*) malloc(max((size_t) 1, sizeof(function) * src.function_count));
if (t.functions == nullptr) {
fprintf(stderr, "init ERROR: Insufficient memory for `transformation.functions`.\n");
return false;
}
for (unsigned int i = 0; i < src.function_count; i++)
t.functions[i] = src.functions[i];
return true;
}
template<typename Semantics>
inline bool operator != (
const transformation<Semantics>& first,
const transformation<Semantics>& second)
{
if (first.function_count != second.function_count) return true;
for (unsigned int i = 0; i < first.function_count; i++)
if (first.functions[i] != second.functions[i]) return true;
return false;
}
template<typename Semantics>
inline bool operator < (
const transformation<Semantics>& first,
const transformation<Semantics>& second)
{
if (first.function_count < second.function_count) return true;
else if (first.function_count > second.function_count) return false;
for (unsigned int i = 0; i < first.function_count; i++) {
if (first.functions[i] < second.functions[i]) return true;
else if (second.functions[i] < first.functions[i]) return false;
}
return false;
}
template<typename Semantics, typename Stream>
inline bool read(transformation<Semantics>& t, Stream& in) {
typedef typename Semantics::function function;
if (!read(t.function_count, in)) return false;
t.functions = (function*) malloc(max((size_t) 1, sizeof(function) * t.function_count));
if (t.functions == nullptr) return false;
if (!Semantics::read(t.functions, in, t.function_count)) {
free(t.functions);
return false;
}
return true;
}
template<typename Semantics, typename Stream>
inline bool write(const transformation<Semantics>& t, Stream& out) {
return write(t.function_count, out)
&& Semantics::write(t.functions, out, t.function_count);
}
template<typename Semantics, typename Stream>
inline bool print(const transformation<Semantics>& t, Stream&& out) {
if (t.function_count == 0) return true;
if (!Semantics::print(t.functions[0], out)) return false;
for (unsigned int i = 1; i < t.function_count; i++) {
if (!print(',', out) || !Semantics::print(t.functions[i], out)) return false;
}
return true;
}
template<typename Semantics>
inline bool apply(
const transformation<Semantics>& t,
const Semantics& src, Semantics& out)
{
if (!apply(t.functions[0], src, out)) return false;
Semantics& temp = *((Semantics*) alloca(sizeof(Semantics)));
for (unsigned int i = 1; i < t.function_count; i++) {
swap(out, temp);
if (!apply(t.functions[i], temp, out)) {
free(temp);
return false;
}
free(temp);
}
return true;
}
template<typename Semantics>
inline bool invert(
Semantics*& inverse, unsigned int& inverse_count,
const transformation<Semantics>& t,
const Semantics& first, const Semantics& second)
{
/* apply the function forward first */
Semantics* outputs = (Semantics*) malloc(max((size_t) 1, sizeof(Semantics) * t.function_count));
if (outputs == nullptr) {
fprintf(stderr, "invert ERROR: Out of memory.\n");
return false;
}
outputs[0] = first;
for (unsigned int i = 1; i < t.function_count; i++) {
if (!apply(t.functions[i - 1], outputs[i - 1], outputs[i])) {
for (unsigned int k = 0; k < i; k++) free(outputs[k]);
free(outputs); return false;
}
}
unsigned int count;
array<Semantics>& old_inverse = *((array<Semantics>*) alloca(sizeof(array<Semantics>)));
if (!invert(old_inverse.data, count, t.functions[t.function_count - 1], outputs[t.function_count - 1], second)) {
for (unsigned int k = 0; k < t.function_count; k++) free(outputs[k]);
free(outputs); return false;
}
old_inverse.length = count;
old_inverse.capacity = count;
for (unsigned int i = t.function_count - 1; i > 0; i--)
{
array<Semantics> new_inverse(1 << (core::log2(old_inverse.length) + 1));
for (unsigned int j = 0; j < old_inverse.length; j++) {
Semantics* next_inverse; unsigned int next_inverse_count;
if (!invert(next_inverse, next_inverse_count, t.functions[i - 1], outputs[i - 1], old_inverse[j])) {
continue;
} else if (!new_inverse.ensure_capacity(new_inverse.length + next_inverse_count)) {
for (unsigned int k = 0; k < new_inverse.length; k++) free(new_inverse[k]);
for (unsigned int k = 0; k < old_inverse.length; k++) free(old_inverse[k]);
for (unsigned int k = 0; k < t.function_count; k++) free(outputs[k]);
for (unsigned int k = 0; k < next_inverse_count; k++) free(next_inverse[k]);
free(outputs); free(old_inverse); free(next_inverse); return false;
}
for (unsigned int k = 0; k < next_inverse_count; k++) {
new_inverse[new_inverse.length++] = next_inverse[k];
free(next_inverse[k]);
}
free(next_inverse);
}
for (unsigned int k = 0; k < old_inverse.length; k++) free(old_inverse[k]);
swap(old_inverse, new_inverse);
}
for (unsigned int k = 0; k < t.function_count; k++) free(outputs[k]);
free(outputs);
if (old_inverse.length == 0) {
free(old_inverse);
return false;
}
resize(old_inverse.data, old_inverse.length);
inverse = old_inverse.data;
inverse_count = old_inverse.length;
return true;
}
enum class rule_type : uint_fast8_t {
EMPTY = 0,
NONTERMINAL,
TERMINAL
};
template<typename Stream>
inline bool read(rule_type& type, Stream& in) {
uint8_t v;
if (!read(v, in)) return false;
type = (rule_type) v;
return true;
}
template<typename Stream>
inline bool write(const rule_type& type, Stream& out) {
return write((uint8_t) type, out);
}
template<typename Semantics>
struct nonterminal_rule {
unsigned int* nonterminals;
transformation<Semantics>* transformations;
unsigned int length;
static inline void move(const nonterminal_rule<Semantics>& src, nonterminal_rule<Semantics>& dst) {
dst.nonterminals = src.nonterminals;
dst.transformations = src.transformations;
dst.length = src.length;
}
static inline unsigned int hash(const nonterminal_rule<Semantics>& rule) {
unsigned int hash = default_hash(rule.nonterminals, rule.length);
for (unsigned int i = 0; i < rule.length; i++)
hash ^= hasher<transformation<Semantics>>::hash(rule.transformations[i]);
return hash;
}
static inline void free(nonterminal_rule<Semantics>& rule) {
core::free(rule.nonterminals);
for (unsigned int i = 0; i < rule.length; i++)
core::free(rule.transformations[i]);
core::free(rule.transformations);
}
};
template<typename Semantics>
inline bool init(nonterminal_rule<Semantics>& rule, const nonterminal_rule<Semantics>& src) {
rule.length = src.length;
rule.nonterminals = (unsigned int*) malloc(max((size_t) 1, sizeof(unsigned int) * src.length));
if (rule.nonterminals == nullptr) {
fprintf(stderr, "init ERROR: Insufficient memory for `nonterminal_rule.nonterminals`.\n");
return false;
}
for (unsigned int i = 0; i < src.length; i++)
rule.nonterminals[i] = src.nonterminals[i];
rule.transformations = (transformation<Semantics>*) malloc(
max((size_t) 1, sizeof(transformation<Semantics>) * src.length));
if (rule.transformations == nullptr) {
fprintf(stderr, "init ERROR: Insufficient memory for `nonterminal_rule.transformations`.\n");
core::free(rule.nonterminals); return false;
}
for (unsigned int i = 0; i < src.length; i++) {
if (!init(rule.transformations[i], src.transformations[i])) {
fprintf(stderr, "init ERROR: Insufficient memory for `nonterminal_rule.transformations[%u]`.\n", i);
for (unsigned int j = 0; j < i; j++) core::free(rule.transformations[j]);
core::free(rule.transformations); core::free(rule.nonterminals);
return false;
}
}
return true;
}
template<typename Semantics>
inline bool operator == (const nonterminal_rule<Semantics>& first, const nonterminal_rule<Semantics>& second) {
if (first.nonterminals == nullptr || first.length != second.length)
return false;
/* compare nonterminal list */
for (unsigned int i = 0; i < first.length; i++)
if (first.nonterminals[i] != second.nonterminals[i]) return false;
/* compare transformation functions */
for (unsigned int i = 0; i < first.length; i++)
if (first.transformations[i] != second.transformations[i]) return false;
return true;
}
template<typename Semantics>
inline bool operator < (const nonterminal_rule<Semantics>& first, const nonterminal_rule<Semantics>& second) {
if (first.length < second.length) return true;
else if (first.length > second.length) return false;
/* compare the nonterminal list */
for (unsigned int i = 0; i < first.length; i++) {
if (first.nonterminals[i] < second.nonterminals[i]) return true;
else if (first.nonterminals[i] > second.nonterminals[i]) return false;
}
/* compare the transformation functions */
for (unsigned int i = 0; i < first.length; i++) {
if (first.transformations[i] < second.transformations[i]) return true;
else if (second.transformations[i] < first.transformations[i]) return false;
}
return false;
}
template<typename Semantics, typename Stream, typename Printer>
inline bool print(const nonterminal_rule<Semantics>& r, Stream& out, Printer& printer) {
if (!print('(', out)) return false;
for (unsigned int i = 0; i < r.length; i++) {
if (i > 0 && !print(' ', out)) return false;
if (!print(r.nonterminals[i], out, printer) || !print(':', out) || !print(r.transformations[i], out))
return false;
}
return print(')', out);
}
template<typename Semantics, typename Stream>
inline bool read(nonterminal_rule<Semantics>& r, Stream& in, hash_map<string, unsigned int>& token_map) {
if (!read(r.length, in))
return false;
r.nonterminals = (unsigned int*) malloc(max((size_t) 1, sizeof(unsigned int) * r.length));
if (r.nonterminals == nullptr) {
fprintf(stderr, "read ERROR: Insufficient memory for `nonterminal_rule.nonterminals`.\n");
return false;
} if (!read(r.nonterminals, in, r.length)) {
core::free(r.nonterminals);
return false;
}
r.transformations = (transformation<Semantics>*) malloc(
max((size_t) 1, sizeof(transformation<Semantics>) * r.length));
if (r.transformations == nullptr) {
fprintf(stderr, "read ERROR: Insufficient memory for `nonterminal_rule.transformations`.\n");
core::free(r.nonterminals); return false;
} if (!read(r.transformations, in, r.length)) {
core::free(r.nonterminals); core::free(r.transformations);
return false;
}
return true;
}
template<typename Semantics, typename Stream>
inline bool write(const nonterminal_rule<Semantics>& r, Stream& out, const string** token_map) {
return write(r.length, out)
&& write(r.nonterminals, out, r.length)
&& write(r.transformations, out, r.length);
}
struct terminal_rule {
unsigned int* terminals;
unsigned int length;
unsigned int* inflected;
unsigned int inflected_length;
static inline unsigned int hash(const terminal_rule& rule) {
unsigned int hash = default_hash(rule.terminals, rule.length);
if (rule.inflected != nullptr)
hash ^= default_hash(rule.inflected, rule.inflected_length);
return hash;
}
static inline void move(const terminal_rule& src, terminal_rule& dst) {
dst.terminals = src.terminals;
dst.length = src.length;
dst.inflected = src.inflected;
dst.inflected_length = src.inflected_length;
}
static inline void free(terminal_rule& rule) {
core::free(rule.terminals);
if (rule.inflected != nullptr)
core::free(rule.inflected);
}
};
inline bool init(terminal_rule& rule, const terminal_rule& src) {
rule.length = src.length;
rule.inflected_length = src.inflected_length;
rule.terminals = (unsigned int*) malloc(max((size_t) 1, sizeof(unsigned int) * src.length));
if (rule.terminals == nullptr) {
fprintf(stderr, "init ERROR: Insufficient memory for `terminal_rule.terminals`.\n");
return false;
}
for (unsigned int i = 0; i < src.length; i++)
rule.terminals[i] = src.terminals[i];
if (src.inflected == nullptr) {
rule.inflected = nullptr;
} else {
rule.inflected = (unsigned int*) malloc(max((size_t) 1, sizeof(unsigned int) * src.inflected_length));
if (rule.inflected == nullptr) {
fprintf(stderr, "init ERROR: Insufficient memory for `terminal_rule.inflected`.\n");
free(rule.terminals); return false;
}
for (unsigned int i = 0; i < src.inflected_length; i++)
rule.inflected[i] = src.inflected[i];
}
return true;
}
inline bool init(terminal_rule& rule, const sequence& terminal) {
rule.length = terminal.length;
rule.terminals = (unsigned int*) malloc(max((size_t) 1, sizeof(unsigned int) * terminal.length));
if (rule.terminals == NULL) {
fprintf(stderr, "init ERROR: Insufficient memory for `terminal_rule.terminals`.\n");
return false;
}
for (unsigned int i = 0; i < terminal.length; i++)
rule.terminals[i] = terminal.tokens[i];
rule.inflected = nullptr;
rule.inflected_length = 0;
return true;
}
inline bool operator == (const terminal_rule& first, const terminal_rule& second) {
if (first.terminals == nullptr
|| first.length != second.length
|| first.inflected_length != second.inflected_length)
return false;
/* compare terminal list */
for (unsigned int i = 0; i < first.length; i++)
if (first.terminals[i] != second.terminals[i]) return false;
/* compare inflected terminal list */
if (first.inflected == nullptr) {
if (second.inflected != nullptr) return false;
} else {
if (second.inflected == nullptr) return false;
for (unsigned int i = 0; i < first.inflected_length; i++)
if (first.inflected[i] != second.inflected[i]) return false;
}
return true;
}
inline bool operator < (const terminal_rule& first, const terminal_rule& second) {
if (first.length < second.length) return true;
else if (first.length > second.length) return false;
else if (first.inflected_length < second.inflected_length) return true;
else if (first.inflected_length > second.inflected_length) return false;
/* first compare the terminals */
for (unsigned int i = 0; i < first.length; i++) {
if (first.terminals[i] < second.terminals[i]) return true;
else if (first.terminals[i] > second.terminals[i]) return false;
}
/* compare inflected terminal list */
if (first.inflected == nullptr) {
if (second.inflected != nullptr) return true;
} else {
if (second.inflected == nullptr) return false;
for (unsigned int i = 0; i < first.inflected_length; i++) {
if (first.inflected[i] < second.inflected[i]) return true;
else if (first.inflected[i] > second.inflected[i]) return false;
}
}
return false;
}
template<typename Stream, typename Printer>
inline bool print(const terminal_rule& rule, Stream& out, Printer& printer) {
if (!print('"', out) || !print(sequence(rule.terminals, rule.length), out, printer) || !print('"', out))
return false;
if (rule.inflected == nullptr) return true;
return print("/\"", out) && print(sequence(rule.inflected, rule.inflected_length), out, printer) && print('"', out);
}
template<typename Stream>
inline bool read(terminal_rule& r, Stream& in, hash_map<string, unsigned int>& token_map) {
if (!read(r.length, in))
return false;
r.terminals = (unsigned int*) malloc(max((size_t) 1, sizeof(unsigned int) * r.length));
if (r.terminals == nullptr) {
fprintf(stderr, "read ERROR: Insufficient memory for `terminal_rule.terminals`.\n");
return false;
} if (!read(r.terminals, in, r.length)) {
core::free(r.terminals);
return false;
}
if (!read(r.inflected_length, in))
return false;
if (r.inflected_length == 0) {
r.inflected = nullptr;
return true;
}
r.inflected = (unsigned int*) malloc(max((size_t) 1, sizeof(unsigned int) * r.inflected_length));
if (r.inflected == nullptr) {
fprintf(stderr, "read ERROR: Insufficient memory for `terminal_rule.inflected`.\n");
return false;
} if (!read(r.inflected, in, r.length)) {
core::free(r.terminals); core::free(r.inflected);
return false;
}
return true;
}
template<typename Stream>
inline bool write(const terminal_rule& r, Stream& out, const string** token_map) {
if (!write(r.length, out) || !write(r.terminals, out, r.length) || !write(r.inflected_length, out))
return false;
if (r.inflected == nullptr)
return true;
else return write(r.inflected, out, r.inflected_length);
}
/* Represents a production rule in the semantic grammar. */
template<typename Semantics>
struct rule {
rule_type type;
union {
nonterminal_rule<Semantics> nt;
terminal_rule t;
};
rule(const rule<Semantics>& src) : type(src.type) {
if (!initialize(src))
exit(EXIT_FAILURE);
}
rule(const sequence& terminal) : type(rule_type::TERMINAL) {
if (!initialize(terminal))
exit(EXIT_FAILURE);
}
rule(const syntax_node<Semantics>* const* terminals, unsigned int terminal_count) : type(rule_type::TERMINAL)
{
#if !defined(NDEBUG)
if (terminal_count == 0)
fprintf(stderr, "rule WARNING: `length` is zero.\n");
#endif
t.length = 0;
unsigned int total_length = 0;
for (unsigned int i = 0; i < terminal_count; i++) {
#if !defined(NDEBUG)
/* check if the children are terminal tokens */
if (!terminals[i]->right.is_terminal())
fprintf(stderr, "rule WARNING: This constructor should only be used with sequences of terminals.\n");
#endif
total_length += terminals[i]->right.t.length;
}
t.terminals = (unsigned int*) malloc(sizeof(unsigned int) * total_length);
if (t.terminals == nullptr) exit(EXIT_FAILURE);
for (unsigned int i = 0; i < terminal_count; i++)
for (unsigned int j = 0; j < terminals[i]->right.t.length; j++)
t.terminals[t.length++] = terminals[i]->right.t.terminals[j];
t.inflected = nullptr;
t.inflected_length = 0;
}
~rule() { free(); }
inline void operator = (const rule<Semantics>& src) {
type = src.type;
if (!initialize(src))
exit(EXIT_FAILURE);
}
inline bool is_terminal() const {
return type == rule_type::TERMINAL;
}
inline const sequence get_terminal() const {
return sequence(t.terminals, t.length);
}
static inline unsigned int hash(const rule<Semantics>& rule) {
/* TODO: precompute these and store them in a table for faster access */
unsigned int type_hash = default_hash<rule_type, 4027360733>(rule.type);
switch (rule.type) {
case rule_type::TERMINAL:
return type_hash ^ terminal_rule::hash(rule.t);
case rule_type::NONTERMINAL:
return type_hash ^ nonterminal_rule<Semantics>::hash(rule.nt);
case rule_type::EMPTY: break;
}
fprintf(stderr, "rule.hash ERROR: Unrecognized rule_type.\n");
exit(EXIT_FAILURE);
}
static inline void move(const rule<Semantics>& src, rule<Semantics>& dst) {
dst.type = src.type;
switch (src.type) {
case rule_type::TERMINAL:
core::move(src.t, dst.t); return;
case rule_type::NONTERMINAL:
core::move(src.nt, dst.nt); return;
case rule_type::EMPTY: break;
}
fprintf(stderr, "rule.move ERROR: Unrecognized rule_type.\n");
exit(EXIT_FAILURE);
}
static inline bool copy(const rule<Semantics>& src, rule<Semantics>& dst) {
return init(dst, src);
}
static inline void swap(rule<Semantics>& first, rule<Semantics>& second) {
char* first_data = (char*) &first;
char* second_data = (char*) &second;
for (unsigned int i = 0; i < sizeof(rule<Semantics>); i++)
core::swap(first_data[i], second_data[i]);
}
static inline bool is_empty(const rule<Semantics>& rule) {
return rule.type == rule_type::EMPTY;
}
static inline void set_empty(rule<Semantics>& rule) {
rule.type = rule_type::EMPTY;
}
static inline void set_empty(rule<Semantics>* rules, unsigned int count) {
memset(static_cast<void*>(rules), 0, sizeof(rule<Semantics>) * count);
}
static inline void free(rule<Semantics>& r) {
r.free();
}
private:
bool initialize(const rule<Semantics>& src) {
switch (src.type) {
case rule_type::TERMINAL:
return init(t, src.t);
case rule_type::NONTERMINAL:
return init(nt, src.nt);
case rule_type::EMPTY: break;
}
fprintf(stderr, "rule.move ERROR: Unrecognized rule_type.\n");
return false;
}
bool initialize(const sequence& terminal) {
return init(t, terminal);
}
inline void free() {
switch (type) {
case rule_type::TERMINAL:
core::free(t); return;
case rule_type::NONTERMINAL:
core::free(nt); return;
case rule_type::EMPTY: break;
}
fprintf(stderr, "rule.free ERROR: Unrecognized rule_type.\n");
exit(EXIT_FAILURE);
}
template<typename A> friend bool init(rule<A>&, const rule<A>&);
template<typename A> friend bool init(rule<A>&, const sequence&);
};
template<typename Semantics>
inline bool init(rule<Semantics>& r, const rule<Semantics>& src) {
r.type = src.type;
return r.initialize(src);
}
template<typename Semantics>
inline bool init(rule<Semantics>& r, const sequence& terminal) {
r.type = rule_type::TERMINAL;
return r.initialize(terminal);
}
template<typename Semantics>
inline bool operator == (const rule<Semantics>& first, const rule<Semantics>& second) {
if (first.type != second.type) return false;
switch (first.type) {
case rule_type::TERMINAL:
return first.t == second.t;
case rule_type::NONTERMINAL:
return first.nt == second.nt;
case rule_type::EMPTY: break;
}
fprintf(stderr, "operator == ERROR: Unrecognized rule_type.\n");
exit(EXIT_FAILURE);
}
template<typename Semantics>
inline bool operator != (const rule<Semantics>& first, const rule<Semantics>& second) {
return !(first == second);
}
template<typename Semantics>
inline bool operator < (const rule<Semantics>& first, const rule<Semantics>& second) {
if (first.type < second.type) return true;
else if (first.type > second.type) return false;
switch (first.type) {
case rule_type::TERMINAL:
return first.t < second.t;
case rule_type::NONTERMINAL:
return first.nt < second.nt;
case rule_type::EMPTY: break;
}
fprintf(stderr, "operator < ERROR: Unrecognized rule_type.\n");
exit(EXIT_FAILURE);
}
template<typename Semantics, typename Stream, typename AtomPrinter, typename NonterminalPrinter>
inline bool print(const rule<Semantics>& r, Stream&& out, pair<AtomPrinter&, NonterminalPrinter&> printers) {
switch (r.type) {
case rule_type::TERMINAL:
return print(r.t, out, printers.key);
case rule_type::NONTERMINAL:
return print(r.nt, out, printers.value);
case rule_type::EMPTY: break;
}
fprintf(stderr, "print ERROR: Unrecognized rule_type.\n");
return false;
}
template<typename Semantics, typename Stream>
bool read(rule<Semantics>& r, Stream& stream, hash_map<string, unsigned int>& token_map)
{
if (!read(r.type, stream)) return false;
switch (r.type) {
case rule_type::TERMINAL:
return read(r.t, stream, token_map);
case rule_type::NONTERMINAL:
return read(r.nt, stream, token_map);
case rule_type::EMPTY: break;
}
fprintf(stderr, "read ERROR: Unrecognized rule_type.\n");
return false;
}
template<typename Semantics, typename Stream>
bool write(const rule<Semantics>& r, Stream& stream, const string** token_map)
{
if (!write(r.type, stream)) return false;
switch (r.type) {
case rule_type::TERMINAL:
return write(r.t, stream, token_map);
case rule_type::NONTERMINAL:
return write(r.nt, stream, token_map);
case rule_type::EMPTY: break;
}
fprintf(stderr, "write ERROR: Unrecognized rule_type.\n");
return false;
}
/* forward declarations */
template<bool DiscardImpossible, bool PruneAmbiguousLogicalForms,
typename RuleDistribution, typename Semantics, typename StringMapType>
weighted<Semantics>* log_conditional(RuleDistribution&,
const rule<Semantics>&, const Semantics&, const StringMapType& token_map, unsigned int&);
template<bool PruneUnobservedTerminals, typename RuleDistribution, typename Semantics, typename StringMapType>
weighted<sequence>* get_terminals(RuleDistribution&,
const Semantics&, StringMapType&, unsigned int&);
/* TODO: i think it would be better if we didn't make a
distinction between preterminals and "internal"
nonterminals, and instead allow the user to make this
distinction in the rule distribution if they so desire;
it might make the parser cleaner */
template<typename Semantics, typename RuleDistribution>
struct nonterminal {
typedef Semantics semantics_type;
typedef RuleDistribution rule_distribution_type;
string name;
unsigned int id;
RuleDistribution rule_distribution;
inline RuleDistribution& get_rule_distribution() {
return rule_distribution;
}
inline void on_resize() {
rule_distribution.on_resize();
}
inline void clear() {
rule_distribution.clear();
}
static inline void free(nonterminal<Semantics, RuleDistribution>& N) {
core::free(N.name);
core::free(N.rule_distribution);
}
};
template<typename Semantics, typename RuleDistribution>
bool init(nonterminal<Semantics, RuleDistribution>& N,
const string& name, unsigned int id)
{
N.id = id;
return init(N.name, name);
}
template<typename Semantics, typename RuleDistribution>
bool init(nonterminal<Semantics, RuleDistribution>& N,
const nonterminal<Semantics, RuleDistribution>& src)
{
N.id = src.id;
if (!init(N.rule_distribution, src.rule_distribution)) {
return false;
} else if (!init(N.name, src.name)) {
free(N.rule_distribution);
return false;
}
return true;
}
template<typename Semantics, typename RuleDistribution>
inline bool copy(
const nonterminal<Semantics, RuleDistribution>& src,
nonterminal<Semantics, RuleDistribution>& dst)
{
dst.id = src.id;
if (!copy(src.rule_distribution, dst.rule_distribution)) {
return false;
} else if (!copy(src.name, dst.name)) {
free(dst.rule_distribution);
return false;
}
return true;
}
template<typename Semantics, typename RuleDistribution,
typename Stream, typename... Scribes,
typename std::enable_if<is_readable<Stream>::value>::type* = nullptr>
bool read(nonterminal<Semantics, RuleDistribution>& N,
Stream& stream, Scribes&&... scribes)
{
if (!read(N.id, stream)
|| !read(N.rule_distribution, stream, std::forward<Scribes>(scribes)...)) {
return false;
} else if (!read(N.name, stream)) {
free(N.rule_distribution);
return false;
}
return true;
}
template<typename Semantics, typename RuleDistribution,
typename Stream, typename... Scribes,
typename std::enable_if<is_writeable<Stream>::value>::type* = nullptr>
bool write(
const nonterminal<Semantics, RuleDistribution>& N,
Stream& stream, Scribes&&... scribes)
{
return write(N.id, stream) && write(N.rule_distribution, stream, std::forward<Scribes>(scribes)...) && write(N.name, stream);
}
template<typename Semantics, typename Distribution>
inline void sample(nonterminal<Semantics, Distribution>& N) {
sample(N.rule_distribution);
}
template<typename Semantics, typename RuleDistribution>
struct grammar
{
array<nonterminal<Semantics, RuleDistribution>> nonterminals;
hash_map<string, unsigned int> nonterminal_names;
grammar() : nonterminals(16), nonterminal_names(16) { }
~grammar() { free(); }
inline void on_resize() {
for (auto& nonterminal : nonterminals)
nonterminal.on_resize();
}
inline void clear() {
for (auto& nonterminal : nonterminals)
nonterminal.clear();
}
bool compute_nonterminal_names() {
bool contains; unsigned int index;
if (!nonterminal_names.check_size(nonterminals.length))
return false;
for (const auto& N : nonterminals) {
unsigned int& id = nonterminal_names.get(N.name, contains, index);
if (contains) {
fprintf(stderr, "grammar.compute_nonterminal_names ERROR: Nonterminal names must be unique.\n");
return false;
} else {
if (!init(nonterminal_names.table.keys[index], N.name))
return false;
nonterminal_names.table.size++;
id = N.id;
}
}
return true;
}
static inline void free(grammar<Semantics, RuleDistribution>& G) {
G.free();
core::free(G.nonterminals);
core::free(G.nonterminal_names);
}
private:
inline void free() {
for (unsigned int i = 0; i < nonterminals.length; i++)
core::free(nonterminals[i]);
for (auto entry : nonterminal_names)
core::free(entry.key);
}
};
template<typename Semantics, typename RuleDistribution>
bool copy(const grammar<Semantics, RuleDistribution>& src, grammar<Semantics, RuleDistribution>& dst) {
if (!array_init(dst.nonterminals, src.nonterminals.length)) {
return false;
} else if (!hash_map_init(dst.nonterminal_names, src.nonterminal_names.table.capacity)) {
free(dst.nonterminals);
return false;
}
for (unsigned int i = 0; i < src.nonterminals.length; i++) {
if (!copy(src.nonterminals[i], dst.nonterminals[i])) {
free(dst);
return false;
}
dst.nonterminals.length++;
}
return dst.compute_nonterminal_names();
}
template<typename Semantics, typename RuleDistribution,
typename Stream, typename... Scribes,
typename std::enable_if<is_readable<Stream>::value>::type* = nullptr>
bool read(
grammar<Semantics, RuleDistribution>& G,
Stream& stream, Scribes&&... scribes)
{
return hash_map_init(G.nonterminal_names, 64)
&& read(G.nonterminals, stream, std::forward<Scribes>(scribes)...)
&& G.compute_nonterminal_names();
}
template<typename Semantics, typename RuleDistribution,
typename Stream, typename... Scribes,
typename std::enable_if<is_writeable<Stream>::value>::type* = nullptr>
bool write(
const grammar<Semantics, RuleDistribution>& G,
Stream& stream, Scribes&&... scribes)
{
return write(G.nonterminals, stream, std::forward<Scribes>(scribes)...);
}
template<typename Semantics, typename RuleDistribution>
inline void sample_grammar(grammar<Semantics, RuleDistribution>& G) {
for (auto& nonterminal : G.nonterminals)
sample(nonterminal);
}
template<typename Semantics>
struct syntax_node {
/* if right_terminal.tokens != NULL, this is a
terminal node; otherwise, it's a nonterminal */
rule<Semantics> right;
syntax_node<Semantics>** children;
unsigned int reference_count;
syntax_node(const sequence& terminal) : right(terminal), children(NULL), reference_count(1) { }