-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiface.c
1460 lines (1323 loc) · 54.9 KB
/
iface.c
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
/* Foma: a finite-state toolkit and library. */
/* Copyright © 2008-2011 Mans Hulden */
/* This file is part of foma. */
/* Foma is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License version 2 as */
/* published by the Free Software Foundation. */
/* Foma is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* You should have received a copy of the GNU General Public License */
/* along with foma. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
//#define LLONG_MAX 0x7fffffffffffffffLL/* max value for a long long */
#include "foma.h"
#include "zlib.h"
extern int g_show_flags;
extern int g_obey_flags;
extern int g_flag_is_epsilon;
extern int g_print_space;
extern int g_print_pairs;
extern int g_minimal;
extern int g_name_nets;
extern int g_print_sigma;
extern int g_quit_on_fail;
extern int g_quote_special;
extern int g_recursive_define;
extern int g_sort_arcs;
extern int g_verbose;
extern int g_minimize_hopcroft;
extern int g_list_limit;
extern int g_list_random_limit;
extern int g_compose_tristate;
extern int g_med_limit ;
extern int g_med_cutoff ;
extern char *g_att_epsilon;
extern int foma_net_print(struct fsm *net, gzFile *outfile);
static char *sigptr(struct sigma *sigma, int number);
static int print_dot(struct fsm *net, char *filename);
static int print_net(struct fsm *net, char *filename);
static int print_sigma(struct sigma *sigma, FILE *out);
static int view_net(struct fsm *net);
#define FVAR_BOOL 1
#define FVAR_INT 2
#define FVAR_STRING 3
#define LINE_LIMIT 8192
struct g_v {
void *ptr;
char *name;
int type;
} global_vars[] = {
{&g_flag_is_epsilon, "flag-is-epsilon", FVAR_BOOL},
{&g_minimal, "minimal", FVAR_BOOL},
{&g_name_nets, "name-nets", FVAR_BOOL},
{&g_obey_flags, "obey-flags", FVAR_BOOL},
{&g_print_pairs, "print-pairs", FVAR_BOOL},
{&g_print_sigma, "print-sigma", FVAR_BOOL},
{&g_print_space, "print-space", FVAR_BOOL},
{&g_quit_on_fail, "quit-on-fail", FVAR_BOOL},
{&g_recursive_define, "recursive-define", FVAR_BOOL},
{&g_quote_special, "quote-special", FVAR_BOOL},
{&g_show_flags, "show-flags", FVAR_BOOL},
{&g_sort_arcs, "sort-arcs", FVAR_BOOL},
{&g_verbose, "verbose", FVAR_BOOL},
{&g_minimize_hopcroft,"hopcroft-min", FVAR_BOOL},
{&g_compose_tristate, "compose-tristate", FVAR_BOOL},
{&g_med_limit, "med-limit", FVAR_INT},
{&g_med_cutoff, "med-cutoff", FVAR_INT},
{&g_att_epsilon, "att-epsilon", FVAR_STRING},
{NULL, NULL, 0}
};
char warranty[] = "\nThis program is free software; you can redistribute it and/or modify\nit under the terms of the GNU General Public License version 2 as published by\nthe Free Software Foundation.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\nGNU General Public License for more details.\nYou should have received a copy of the GNU General Public License\nalong with this program. If not, see <http://www.gnu.org/licenses/>.\n\n";
struct global_help {
char *name;
char *help;
char *longhelp;
} global_help[] = {
{"regex <regex>", "read a regular expression","Enter a regular expression and add result to top of stack.\nShort form: re\nSee `help operator' for operators, or `help precedence' for operator precedence."},
{"ambiguous upper","returns the input words which have multiple paths in a transducer","Short form: ambiguous\n"},
{"apply up <string>","apply <string> up to the top network on stack","Short form: up <string>\n"},
{"apply down <string>","apply <string> down to the top network on stack","Short form: down <string>\n" },
{"apply med <string>","find approximate matches to string in top network by minimum edit distance","Short form: med <string>\n" },
{"apply up","enter apply up mode (Ctrl-D exits)","Short form: up\n"},
{"apply down","enter apply down mode (Ctrl-D exits)","Short form: down\n"},
{"apply med","enter apply med mode (Ctrl-D exits)","Short form: med\n"},
{"apropos <string>","search help for <string>",""},
{"clear stack","clears the stack",""},
{"compact sigma","removes redundant symbols from FSM","" },
{"complete net","completes the FSM","" },
{"compose net","composes networks on stack",""},
{"concatenate","concatenates networks on stack","" },
{"crossproduct net","cross-product of top two FSMs on stack","See ×\n" },
{"define <name> <r.e.>","define a network","Example: \ndefine A x -> y;\n and\nA = x -> y;\n\nare equivalent\n"},
{"define <fname>(<v1,..,vn>) <r.e.>","define function","Example: define Remove(X) [X -> 0].l;"},
{"determinize net","determinizes top FSM on stack",""},
{"echo <string>","echo a string",""},
{"eliminate flag <name>","eliminate flag <name> diacritics from the top network",""},
{"eliminate flags","eliminate all flag diacritics from the top network",""},
{"export cmatrix (filename)","export the confusion matrix as an AT&T transducer",""},
{"extract ambiguous","extracts the ambiguous paths of a transducer","Short form: examb"},
{"extract unambiguous","extracts the unambiguous paths of a transducer","Short form: exunamb"},
{"help license","prints license",""},
{"help warranty","prints warranty information",""},
{"ignore net","applies ignore to top two FSMs on stack","See /\n"},
{"intersect net","intersects FSMs on stack","See ∩ (or &)\n" },
{"invert net","inverts top FSM","See ⁻¹ (or .i)\n"},
{"label net","extracts all attested symbol pairs from FSM","See also: sigma net"},
{"letter machine","Converts top FSM to a letter machine","See also: _lm(L)"},
{"load stack <filename>","Loads networks and pushes them on the stack","Short form: load"},
{"load defined <filename>","Restores defined networks from file","Short form: loadd"},
{"lower-side net","takes lower projection of top FSM","See ₂ (or .l)\n"},
{"minimize net","minimizes top FSM","Minimization can be controlled through the variable minimal: when set to OFF FSMs are never minimized.\nAlso, hopcroft-min can be set to OFF in which case minimization is done by double reversal and determinization (aka Brzozowski's algorithm). It is likely to be much slower.\n"},
{"name net <string>","names top FSM",""},
{"negate net","complements top FSM","See ¬\n" },
{"one-plus net","Kleene plus on top FSM","See +\n" },
{"pop stack","remove top FSM from stack","" },
{"print cmatrix","prints the confusion matrix associated with the top network in tabular format",""},
{"print defined","prints defined symbols and functions",""},
{"print dot (>filename)","prints top FSM in Graphviz dot format",""},
{"print lower-words","prints words on the lower-side of top FSM",""},
{"print name","prints the name of the top FSM","" },
{"print net","prints all information about top FSM","Short form: net\n" },
{"print random-lower","prints random words from lower side","Short form: random-lower\n" },
{"print random-upper","prints random words from upper side","Short form: random-upper" },
{"print random-words","prints random words from top FSM","Short form: random-words\n"},
{"print sigma","prints the alphabet of the top FSM","Short form: sigma\n"},
{"print size","prints size information about top FSM","Short form: size\n"},
{"print shortest-string","prints the shortest string of the top FSM","Short form: pss\n"},
{"print shortest-string-size","prints length of shortest string","Short form: psz\n"},
{"prune net","makes top network coaccessible",""},
{"push (defined) <name>","adds a defined FSM to top of stack",""},
{"quit","exit foma",""},
{"read att <filename>","read a file in AT&T FSM format and add to top of stack","Short form: ratt"},
{"read cmatrix <filename>","read a confusion matrix and associate it with the network on top of the stack",""},
{"read prolog <filename>","reads prolog format file",""},
{"read lexc <filename>","read and compile lexc format file",""},
{"read spaced-text <filename>","compile space-separated words/word-pairs separated by newlines into a FST",""},
{"read text <filename>","compile a list of words separated by newlines into an automaton",""},
{"reverse net","reverses top FSM","Short form: rev\nSee .r\n"},
{"rotate stack","rotates stack",""},
{"save defined <filename>","save all defined networks to binary file","Short form: saved" },
{"save stack <filename>","save stack to binary file","Short form: ss" },
{"set <variable> <ON|OFF>","sets a global variable (see show variables)","" },
{"show variables","prints all variable/value pairs",""},
{"shuffle net","asynchronous product on top two FSMs on stack","See ∥ (or <>)\n"},
{"sigma net","Extracts the alphabet and creates a FSM that accepts all single symbols in it","See also: label net"},
{"source <file>","read and compile script file",""},
{"sort net","sorts arcs lexicographically on top FSM",""},
{"substitute defined X for Y","substitutes defined network X at all arcs containing Y ",""},
{"substitute symbol X for Y","substitutes all occurrences of Y in an arc with X",""},
{"system <cmd>","execute a system command","" },
{"test unambiguous","test if top FST is unambiguous","Short form: tunam\n"},
{"test equivalent","test if the top two FSMs are equivalent","Short form: equ\nNote: equivalence is undecidable for transducers in the general case. The result is reliable only for recognizers.\n"},
{"test functional","test if the top FST is functional (single-valued)","Short form: tfu\n"},
{"test identity","test if top FST represents identity relations only","Short form: tid\n"},
{"test lower-universal","test if lower side is Σ*","Short form: tlu\n"},
{"test upper-universal","test if upper side is Σ*","Short form: tuu\n"},
{"test non-null","test if top machine is not the empty language","Short form:tnn\n" },
{"test null","test if top machine is the empty language (∅)","Short form: tnu\n" },
{"test sequential","tests if top machine is sequential","Short form: tseq\n"},
{"test star-free","test if top FSM is star-free","Short form: tsf\n"},
{"turn stack","turns stack upside down","" },
{"twosided flag-diacritics","changes flags to always be identity pairs","Short form: tfd" },
{"undefine <name>","remove <name> from defined networks","See define\n"},
{"union net","union of top two FSMs","See ∪ (or |)\n"},
{"upper-side net","upper projection of top FSM","See ₁ (or .u)\n"},
{"view net","display top network (if supported)",""},
{"zero-plus net","Kleene star on top fsm","See *\n"},
{"variable compose-tristate","use the tristate composition algorithm","Default value: OFF\n"},
{"variable show-flags","show flag diacritics in `apply'","Default value: ON\n"},
{"variable obey-flags","obey flag diacritics in `apply'","Default value: ON\n"},
{"variable minimal","minimize resulting FSMs","Default value: ON\n"},
{"variable print-pairs","always print both sides when applying","Default value: OFF\n"},
{"variable print-space","print spaces between symbols","Default value: OFF\n"},
{"variable print-sigma","print the alphabet when printing network","Default value: ON\n"},
{"quit-on-fail","Abort operations when encountering errors","Default value: ON\n"},
{"variable recursive-define","Allow recursive definitions","Default value: OFF\n"},
{"variable verbose","Verbosity of interface","Default value: ON\n"},
{"variable hopcroft-min","ON = Hopcroft minimization, OFF = Brzozowski minimization","Default value: ON\n"},
{"variable med-limit","the limit on number of matches in apply med","Default value: 3\n"},
{"variable med-cutoff","the cost limit for terminating a search in apply med","Default value: 3\n"},
{"variable att-epsilon","the EPSILON symbol when reading/writing AT&T files","Default value: @0@\n"},
{"write prolog (> filename)","writes top network to prolog format file/stdout","Short form: wpl"},
{"write att (> <filename>)","writes top network to AT&T format file/stdout","Short form: watt"},
{"re operator: (∀<var name>)(F)","universal quantification","Example: $.A is equivalent to:\n(∃x)(x ∈ A ∧ (∀y)(¬(y ∈ A ∧ ¬(x = y))))"},
{"re operator: (∃<var name>)(F)","existential quantification","Example: $.A is equivalent to:\n(∃x)(x ∈ A ∧ ¬(∃y)(y ∈ A ∧ ¬(x = y)))"},
{"logic re operator: ∈","`in' predicate for logical formulae",""},
{"logic re operator: S(t1,t2)","successor-of predicate for logical formulae",""},
{"logic re operator: ≤","less-than or equal-to","Refers to position of quantified substring\n" },
{"logic re operator: ≥","more-than or equal-to","Refers to position of quantified substring\n" },
{"logic re operator: ≺","precedes","Refers to position of quantified substring\n"},
{"logic re operator: ≻","follows","Refers to position of quantified substring\n"},
{"logic re operator: ∧","conjunction","Operationally equivalent to ∩\n"},
{"logic re operator: ∨","disjunction","Operationally equivalent to ∪\n"},
{"logic re operator: →","implication","A → B is equivalent to ¬A ∨ B "},
{"logic re operator: ↔","biconditional","A ↔ B is equivalent to (¬A ∨ B) ∧ (¬B ∨ A)"},
{"re operator: ∘ (or .o.) ","compose","A .o. B is the composition of transducers/recognizers A and B\nThe composition algorithm can be controlled with the variable\ncompose-tristate. The default algorithm is a `bistate' composition that eliminates redundant paths but may fail to find the shortest path.\n"},
{"re operator: × (or .x.) ","cross-product","A × B (where A and B are recognizers, not transducers\nyields the cross-product of A and B.\n"},
{"re operator: .O. ","`lenient' composition","Lenient composition as defined in Karttunen(1998) A .O. B = [A ∘ B] .P. B\n"},
{"re operator: ∥ (or <>) ","shuffle (asynchronous product)","A ∥ B yields the asynchronous (or shuffle) product of FSM A and B.\n" },
{"re operator: => ","context restriction, e.g. A => B _ C, D _ E","A => B _ C yields the language where every instance of a substring drawn from A is surrounded by B and C. Multiple contexts can be specified if separated by commas, e.g.: A => B _ C, D _ E"},
{"re operator: ->, <-, <->, etc.","replacement operators",""},
{"re operator: @->, @>, etc.","directed replacement operators",""},
{"re operator: (->), (@->), etc. ","optional replacements","Optional replacement operators variants. Note that the directional modes leftmost/rightmost/longest/shortest are not affected by optionality, i.e. only replacement is optional, not mode. Hence A (@->) B is not in general equivalent to the parallel rule A @-> B, A -> ... "},
{"re operator: ||,\\/,\\\\,// ","replacement direction specifiers","Rewrite rules direction specifier meaning is:\nA -> B || C _ D (replace if C and D match on upper side)\nA -> B // C _ D (replace if C matches of lower side and D matches on upper side)\nA -> B \\\\ C _ D (replace if C matches on upper side and D matches on lower side)\nA -> B \\/ C _ D (replace if C and D match on lower side)\n"},
{"re operator: _ ","replacement or restriction context specifier",""},
{"re operator: ,,","parallel context replacement operator","Separates parallel rules, e.g.:\nA -> B , C @-> D || E _ F ,, G -> H \\/ I _ J\n"},
{"re operator: ,","parallel replacement operator","Separates rules and contexts. Example: A -> B, C <- D || E _ F"},
{"re operator: [.<r.e.>.]","single-epsilon control in replacement LHS, e.g. [..] -> x","If the LHS contains the empty string, as does [.a*.] -> x, the rule yields a transducer where the empty string is assumed to occur exactly once between each symbol."},
{"re operator: ...","markup replacement control (e.g. A -> B ... C || D _ E)","A -> B ... C yields a replacement transducer where the center A is left untouched and B and C inserted around A." },
{"re operator: ","concatenation","Binary operator: A B\nConcatenation is performed implicitly according to its precedence level without overt specification\n"},
{"re operator: ∪ (or |) ","union","Binary operator: A|B"},
{"re operator: ∩ (or &) ","intersection","Binary operator: A&B" },
{"re operator: - ","set minus","Binary operator A-B"},
{"re operator: .P.","priority union (upper)","Binary operator A .P. B\nEquivalent to: A .P. B = A ∪ [¬[A₁] ∘ B]\n" },
{"re operator: .p.","priority union (lower)","Binary operator A .p. B\nEquivalent to: A .p. B = A ∪ [¬[A₂] ∘ B]" },
{"re operator: <","precedes","Binary operator A < B\nYields the language where no instance of A follows an instance of B."},
{"re operator: >","follows","Binary operator A > B\nYields the language where no instance of A precedes an instance of B."},
{"re operator: /","ignore","Binary operator A/B\nYield the language/transducer where arbitrary sequences of strings/mappings from B are interspersed in A. For single-symbol languages B, A/B = A ∥ B*"},
{"re operator: ./.","ignore except at edges","Yields the language where arbitrary sequences from B are interspersed in A, except as the very first and very last symbol."},
{"re operator: \\\\\\","left quotient","Binary operator: A\\\\\\B\nInformally: the set of suffixes one can add to A to get strings in B\n"},
{"re operator: ///","right quotient","Binary operator A///B\nInformally: the set of prefixes one can add to B to get a string in A\n"},
{"re operator: /\\/","interleaving quotient","Binary operator A/\\/B\nInformally: the set of strings you can interdigitate (non-continuously) to B to get strings in A\n"},
{"re operator: ¬ (or ~) ","complement","Unary operator ~A, equivalent to Σ* - A\n"},
{"re operator: $","contains a factor of","Unary operator $A\nEquivalent to: Σ* A Σ*\n"},
{"re operator: $.","contains exactly one factor of","Unary operator $.A\nYields the language that contains exactly one factor from A.\nExample: if A = [a b|b a], $.A contains strings ab, ba, abb, bba, but not abab, baba, aba, bab, etc.\n"},
{"re operator: $?","contains maximally one factor of","Unary operator: $?A, yields the language that contains zero or one factors from A. See also $.A."},
{"re operator: +","Kleene plus","Unary operator A+\n"},
{"re operator: *","Kleene star","Unary operator A*\n" },
{"re operator: ^n ^<n ^>n ^{m,n}","m, n-ary concatenations","A^n: A concatenated with itself exactly n times\nA^<n: A concatenated with itself less than n times\nA^>n: A concatenated with itself more than n times\nA^{m,n}: A concatenated with itself between m and n times\n"},
{"re operator: ₁ (or .1 or .u)","upper projection","Unary operator A.u\n"},
{"re operator: ₂ (or .2 or .l)","lower projection","Unary operator A.l\n"},
{"re operator: ⁻¹ (or .i)","inverse of transducer","Unary operator A.i\n"},
{"re operator: .f","eliminate all flags","Unary operator A.f: eliminates all flag diacritics in A"},
{"re operator: .r","reverse of FSM","Unary operator A.r\n"},
{"re operator: :","cross-product","Binary operator A:B, see also A × B\n"},
{"re operator: \\","term complement (\\x = [Σ-x])","Unary operator \\A\nSingle symbols not in A. Equivalent to [Σ-A]\n"},
{"re operator: `","substitution/homomorphism","Ternary operator `[A,B,C] Replace instances of symbol B with symbol C in language A. Also removes the substituted symbol from the alphabet.\n"},
{"re operator: { ... }","concatenate symbols","Single-symbol-concatenation\nExample: {abcd} is equivalent to a b c d\n"},
{"re operator: (A)","optionality","Equivalent to A | ε\nNote: parentheses inside logical formulas function as grouping, see ∀,∃\n"},
{"re operator: @\"filename\"","read saved network from file","Note: loads networks stored with e.g. \"save stack\" but if file contains more than one network, only the first one is used in the regular expression. See also \"load stack\" and \"load defined\"\n"},
{"special symbol: Σ (or ?)","`any' symbol in r.e.",""},
{"special symbol: ε (or 0, [])","epsilon symbol in r.e.",""},
{"special symbol: ∅","the empty language symbol in r.e.",""},
{"special symbol: .#.","word boundary symbol in replacements, restrictions","Signifies both end and beginning of word/string\nExample: A => B _ .#. (allow A only between B and end-of-string)\nExample: A -> B || .#. _ C (replace A with B if it occurs in the beginning of a word and is followed by C)\n"},
{"operator precedence: ","see: `help precedence'","\\ `\n:\n+ * ^ ₁ ₂ ⁻¹ .f .r\n¬ $ $. $?\n(concatenation)\n> <\n∪ ∩ - .P. .p.\n=> -> (->) @-> etc.\n∥\n× ∘ .O.\nNote: compatibility variants (i.e. | = ∪ etc.) are not listed."},
{NULL,NULL,NULL}
};
void iface_help() {
struct global_help *gh;
int i, maxlen;
for (maxlen = 0, gh = global_help; gh->name != NULL; gh++) {
maxlen = maxlen < utf8strlen(gh->name) ? utf8strlen(gh->name) : maxlen;
}
for (gh = global_help; gh->name != NULL; gh++) {
printf("%s",gh->name);
for (i = maxlen - utf8strlen(gh->name); i>=0; i--) {
printf("%s"," ");
}
printf("%s\n",gh->help);
}
}
void iface_ambiguous_upper() {
if (iface_stack_check(1))
stack_add(fsm_extract_ambiguous_domain(stack_pop()));
}
void iface_apropos(char *s) {
struct global_help *gh;
int i, maxlen;
for (maxlen = 0, gh = global_help; gh->name != NULL; gh++) {
if (strcasestr(gh->name,s) != NULL || strcasestr(gh->help,s) != NULL) {
maxlen = maxlen < utf8strlen(gh->name) ? utf8strlen(gh->name) : maxlen;
}
}
for (gh = global_help; gh->name != NULL; gh++) {
if (strcasestr(gh->name,s) != NULL || strcasestr(gh->help,s) != NULL) {
printf("%s",gh->name);
for (i = maxlen - utf8strlen(gh->name); i>=0; i--) {
printf("%s"," ");
}
printf("%s\n",gh->help);
}
}
}
void iface_help_search(char *s) {
struct global_help *gh;
for (gh = global_help; gh->name != NULL; gh++) {
if (strcasestr(gh->name,s) != NULL || strcasestr(gh->help,s) != NULL) {
printf("##\n");
printf("%-32.32s%s\n%s\n",gh->name,gh->help,gh->longhelp);
}
}
}
void iface_print_bool(int value) {
printf("%i (1 = TRUE, 0 = FALSE)\n",value);
}
void iface_warranty() {
printf("%s",warranty);
}
void iface_apply_med(char *word) {
if (iface_stack_check(1))
apply_med(stack_find_top()->fsm, word);
}
int iface_apply_file(char *infilename, char *outfilename, int direction) {
char *result, inword[LINE_LIMIT];
struct fsm *net;
struct apply_handle *ah;
FILE *OUTFILE, *INFILE;
if (direction != AP_D && direction != AP_U) {
perror("Invalid direction in iface_apply_file().\n");
return 1;
}
if (!iface_stack_check(1)) { return 0; }
INFILE = fopen(infilename, "r");
if (INFILE == NULL) {
fprintf(stderr, "%s: ", infilename);
perror("Error opening file");
return 1;
}
if (outfilename == NULL) {
OUTFILE = stdout;
} else {
OUTFILE = fopen(outfilename, "w");
printf("Writing output to file %s.\n", outfilename);
if (OUTFILE == NULL) {
fprintf(stderr, "%s: ", outfilename);
perror("Error opening output file.");
return 1;
}
}
net = stack_find_top()->fsm;
ah = stack_get_ah();
while ((fgets(inword,LINE_LIMIT,INFILE)) != NULL) {
if (inword[strlen(inword)-1] == '\n') {
inword[strlen(inword)-1] = '\0';
}
fprintf(OUTFILE,"\n%s\n", inword);
if (direction == AP_D)
result = apply_down(ah,inword);
else
result = apply_up(ah,inword);
if (result == NULL) {
fprintf(OUTFILE,"???\n");
continue;
} else {
fprintf(OUTFILE,"%s\n",result);
}
for (;;) {
if (direction == AP_D)
result = apply_down(ah,NULL);
if (direction == AP_U)
result = apply_up(ah,NULL);
if (result == NULL)
break;
fprintf(OUTFILE,"%s\n", result);
}
}
if (outfilename != NULL)
fclose(OUTFILE);
return 0;
}
void iface_apply_down(char *word) {
int i;
char *result;
struct apply_handle *ah;
struct fsm *net;
if (!iface_stack_check(1)) {
return;
}
net = stack_find_top()->fsm;
ah = stack_get_ah();
result = apply_down(ah, word);
if (result == NULL) {
printf("???\n");
return;
} else {
printf("%s\n",result);
}
for (i = g_list_limit; i > 0; i--) {
result = apply_down(ah, NULL);
if (result == NULL)
break;
printf("%s\n",result);
}
}
void iface_apply_up(char *word) {
int i;
char *result;
struct fsm *net;
struct apply_handle *ah;
if (!iface_stack_check(1)) {
return;
}
net = stack_find_top()->fsm;
ah = stack_get_ah();
result = apply_up(ah, word);
if (result == NULL) {
printf("???\n");
return;
} else {
printf("%s\n",result);
}
for (i = g_list_limit; i > 0; i--) {
result = apply_up(ah, NULL);
if (result == NULL)
break;
printf("%s\n",result);
}
}
void iface_compact() {
if (iface_stack_check(1)) {
fsm_compact(stack_find_top()->fsm);
sigma_sort(stack_find_top()->fsm);
stack_add(fsm_topsort(fsm_minimize(stack_pop())));
}
}
void iface_complete() {
if (iface_stack_check(1))
stack_add(fsm_complete(stack_pop()));
}
void iface_compose() {
struct fsm *one, *two;
if (iface_stack_check(2)) {
while (stack_size()>1) {
one = stack_pop();
two = stack_pop();
stack_add(fsm_topsort(fsm_minimize(fsm_compose(one,two))));
}
}
}
void iface_conc() {
struct fsm *one, *two;
if (iface_stack_check(2)) {
while (stack_size()>1) {
printf("dd");
one = stack_pop();
two = stack_pop();
stack_add(fsm_topsort(fsm_minimize(fsm_concat(one,two))));
}
}
}
void iface_crossproduct() {
struct fsm *one, *two;
if (iface_stack_check(2)) {
one = stack_pop();
two = stack_pop();
stack_add(fsm_topsort(fsm_minimize(fsm_cross_product(one,two))));
}
}
void iface_determinize() {
if (iface_stack_check(1))
stack_add(fsm_determinize(stack_pop()));
}
void iface_eliminate_flags() {
if (iface_stack_check(1))
stack_add(flag_eliminate(stack_pop(), NULL));
}
void iface_extract_ambiguous() {
if (iface_stack_check(1))
stack_add(fsm_extract_ambiguous(stack_pop()));
}
void iface_extract_unambiguous() {
if (iface_stack_check(1))
stack_add(fsm_extract_unambiguous(stack_pop()));
}
int iface_extract_number(char *s) {
int i;
for (i=0; *(s+i) != '\0' && ((unsigned char) *(s+i) < '0' || (unsigned char) *(s+i) > '9'); i++) { }
return(atoi(s+i));
}
void iface_eliminate_flag(char *name) {
if (iface_stack_check(1))
stack_add(flag_eliminate(stack_pop(), name));
}
void iface_ignore() {
struct fsm *one, *two;
if (iface_stack_check(2)) {
one = stack_pop();
two = stack_pop();
stack_add(fsm_topsort(fsm_minimize(fsm_ignore(one,two,OP_IGNORE_ALL))));
}
}
void iface_intersect() {
if (iface_stack_check(2)) {
while (stack_size()>1)
stack_add(fsm_topsort(fsm_minimize(fsm_intersect(stack_pop(),stack_pop()))));
}
}
void iface_invert() {
if (iface_stack_check(1))
stack_add(fsm_invert(stack_pop()));
}
void iface_label_net() {
if (iface_stack_check(1))
stack_add(fsm_sigma_pairs_net(stack_pop()));
}
void iface_letter_machine() {
if (iface_stack_check(1))
stack_add(fsm_topsort(fsm_minimize(fsm_letter_machine(stack_pop()))));
}
void iface_load_defined(char *filename) {
load_defined(filename);
}
void iface_load_stack(char *filename) {
struct fsm *net;
fsm_read_binary_handle fsrh;
if ((fsrh = fsm_read_binary_file_multiple_init(filename)) == NULL) {
fprintf(stderr, "%s: ", filename);
perror("File error");
return;
}
while ((net = fsm_read_binary_file_multiple(fsrh)) != NULL)
stack_add(net);
return;
}
void iface_lower_side() {
if (iface_stack_check(1))
stack_add(fsm_topsort(fsm_minimize(fsm_lower(stack_pop()))));
}
void iface_minimize() {
int store_minimal_var;
if (iface_stack_check(1)) {
store_minimal_var = g_minimal;
g_minimal = 1;
stack_add(fsm_topsort(fsm_minimize(stack_pop())));
g_minimal = store_minimal_var;
}
}
void iface_one_plus() {
if (iface_stack_check(1))
stack_add(fsm_topsort(fsm_minimize(fsm_kleene_plus(stack_pop()))));
}
void iface_pop() {
struct fsm *net;
if (stack_size() < 1)
printf("Stack is empty.\n");
else {
net = stack_pop();
fsm_destroy(net);
}
}
void iface_lower_words(int limit) {
char *result;
struct apply_handle *ah;
int i;
if (!iface_stack_check(1)) {
return;
}
limit = (limit == -1) ? g_list_limit : limit;
if (iface_stack_check(1)) {
ah = stack_get_ah();
for (i = limit; i > 0; i--) {
result = apply_lower_words(ah);
if (result == NULL)
break;
printf("%s\n",result);
}
apply_reset_enumerator(ah);
}
}
void iface_name_net(char *name) {
if (iface_stack_check(1)) {
strncpy(stack_find_top()->fsm->name, name, 40);
iface_print_name();
}
}
void iface_negate() {
if (iface_stack_check(1))
stack_add(fsm_topsort(fsm_minimize(fsm_complement(stack_pop()))));
}
void iface_print_dot(char *filename) {
if (iface_stack_check(1)) {
if (filename != NULL)
printf("Writing dot file to %s.\n",filename);
print_dot(stack_find_top()->fsm, filename);
}
}
void iface_print_net(char *netname, char *filename) {
struct fsm *net;
if (netname != NULL) {
if ((net = find_defined(netname)) == NULL) {
printf("No defined network %s.\n", netname);
return;
}
print_net(net, filename);
} else {
if (iface_stack_check(1))
print_net(stack_find_top()->fsm, filename);
}
}
void iface_print_cmatrix_att(char *filename) {
FILE *outfile;
if (iface_stack_check(1)) {
if (stack_find_top()->fsm->medlookup == NULL || stack_find_top()->fsm->medlookup->confusion_matrix == NULL) {
printf("No confusion matrix defined.\n");
} else {
if (filename == NULL) {
outfile = stdout;
} else {
outfile = fopen(filename,"w");
printf("Writing confusion matrix to file '%s'.\n", filename);
}
cmatrix_print_att(stack_find_top()->fsm, outfile);
}
}
}
void iface_print_cmatrix() {
if (iface_stack_check(1)) {
if (stack_find_top()->fsm->medlookup == NULL || stack_find_top()->fsm->medlookup->confusion_matrix == NULL) {
printf("No confusion matrix defined.\n");
} else {
cmatrix_print(stack_find_top()->fsm);
}
}
}
void iface_print_defined() {
struct defined *defined;
struct definedf *defined_f;
if (get_defines() == NULL) {
printf("No defined symbols.\n");
}
for (defined = get_defines(); defined != NULL; defined = defined->next) {
printf("%s\t",defined->name);
print_stats(defined->net);
}
for (defined_f = get_defines_f(); defined_f != NULL; defined_f = defined_f->next) {
printf("%s@%i)\t",defined_f->name,defined_f->numargs);
printf("%s\n",defined_f->regex);
}
}
void iface_print_name() {
if (iface_stack_check(1))
printf("%s\n",stack_find_top()->fsm->name);
}
void iface_quit() {
struct fsm *net;
remove_defined(NULL);
while (!(stack_isempty())) {
net = stack_pop();
fsm_destroy(net);
}
exit(0);
}
void iface_random_lower(int limit) {
iface_apply_random(&apply_random_lower, limit);
}
void iface_random_upper(int limit) {
iface_apply_random(&apply_random_upper, limit);
}
void iface_random_words(int limit) {
iface_apply_random(&apply_random_words, limit);
}
void iface_apply_random(char *(*applyer)(), int limit) {
char *result;
struct apply_handle *ah;
int i;
struct apply_results {
char *string;
int count;
} *results, *tempresults;
limit = (limit == -1) ? g_list_random_limit : limit;
if (iface_stack_check(1)) {
results = xxcalloc(limit, sizeof(struct apply_results));
ah = stack_get_ah();
for (i = limit; i > 0; i--) {
result = applyer(ah);
if (result != NULL) {
for (tempresults = results; tempresults - results < limit; tempresults++) {
if (tempresults->string == NULL) {
tempresults->string = strdup(result);
tempresults->count = 1;
break;
}
else if (strcmp(tempresults->string, result) == 0) {
tempresults->count++;
break;
}
}
}
}
for (tempresults = results; tempresults - results < limit; tempresults++) {
if (tempresults->string != NULL) {
printf("[%i] %s\n", tempresults->count, tempresults->string);
xxfree(tempresults->string);
}
}
xxfree(results);
apply_reset_enumerator(ah);
}
}
void iface_print_sigma() {
if (iface_stack_check(1))
print_sigma(stack_find_top()->fsm->sigma,stdout);
}
void iface_print_stats() {
if (iface_stack_check(1))
print_stats(stack_find_top()->fsm);
}
void iface_print_shortest_string() {
/* L - ?+ [[L .o. [?:"@TMP@"]*].l .o. "@TMP@":?*].l; */
struct fsm *Result, *ResultU, *ResultL, *one, *onel, *oneu;
struct apply_handle *ah;
char *word;
if (iface_stack_check(1)) {
one = fsm_copy(stack_find_top()->fsm);
/* L - ?+ [[L .o. [?:"@TMP@"]*].l .o. "@TMP@":?*].l; */
if (stack_find_top()->fsm->arity == 1) {
Result = fsm_minimize(fsm_minus(fsm_copy(one),fsm_concat(fsm_kleene_plus(fsm_identity()),fsm_lower(fsm_compose(fsm_lower(fsm_compose(fsm_copy(one),fsm_kleene_star(fsm_cross_product(fsm_identity(),fsm_symbol("@TMP@"))))),fsm_kleene_star(fsm_cross_product(fsm_symbol("@TMP@"),fsm_identity())))))));
ah = apply_init(Result);
word = apply_words(ah);
if (word != NULL) printf("%s\n",word);
apply_clear(ah);
} else {
onel = fsm_lower(fsm_copy(one));
oneu = fsm_upper(one);
ResultU = fsm_minimize(fsm_minus(fsm_copy(oneu),fsm_concat(fsm_kleene_plus(fsm_identity()),fsm_lower(fsm_compose(fsm_lower(fsm_compose(fsm_copy(oneu),fsm_kleene_star(fsm_cross_product(fsm_identity(),fsm_symbol("@TMP@"))))),fsm_kleene_star(fsm_cross_product(fsm_symbol("@TMP@"),fsm_identity())))))));
ResultL = fsm_minimize(fsm_minus(fsm_copy(onel),fsm_concat(fsm_kleene_plus(fsm_identity()),fsm_lower(fsm_compose(fsm_lower(fsm_compose(fsm_copy(onel),fsm_kleene_star(fsm_cross_product(fsm_identity(),fsm_symbol("@TMP@"))))),fsm_kleene_star(fsm_cross_product(fsm_symbol("@TMP@"),fsm_identity())))))));
ah = apply_init(ResultU);
word = apply_words(ah);
if (word == NULL) word = "";
printf("Upper: %s\n",word);
apply_clear(ah);
ah = apply_init(ResultL);
word = apply_words(ah);
if (word == NULL) word = "";
printf("Lower: %s\n",word);
apply_clear(ah);
}
}
}
void iface_print_shortest_string_size() {
struct fsm *Result, *ResultU, *ResultL, *one, *onel, *oneu;
if (iface_stack_check(1)) {
one = fsm_copy(stack_find_top()->fsm);
/* [L .o. [?:a]*].l; */
if (stack_find_top()->fsm->arity == 1) {
Result = fsm_minimize(fsm_lower(fsm_compose(one,fsm_kleene_star(fsm_cross_product(fsm_identity(),fsm_symbol("a"))))));
printf("Shortest acyclic path length: %i\n",Result->statecount-1);
} else {
onel = fsm_lower(fsm_copy(one));
oneu = fsm_upper(one);
ResultU = fsm_minimize(fsm_lower(fsm_compose(oneu,fsm_kleene_star(fsm_cross_product(fsm_identity(),fsm_symbol("a"))))));
ResultL = fsm_minimize(fsm_lower(fsm_compose(onel,fsm_kleene_star(fsm_cross_product(fsm_identity(),fsm_symbol("a"))))));
printf("Shortest acyclic upper path length: %i\n",(ResultU->statecount)-1);
printf("Shortest acyclic lower path length: %i\n",(ResultL->statecount)-1);
}
}
}
int iface_read_att(char *filename) {
struct fsm *tempnet;
printf("Reading AT&T file: %s\n",filename);
tempnet = read_att(filename);
if (tempnet == NULL) {
fprintf(stderr, "%s: ", filename);
perror("Error opening file");
return 1;
} else {
stack_add(tempnet);
return 0;
}
}
int iface_read_prolog(char *filename) {
struct fsm *tempnet;
printf("Reading prolog: %s\n",filename);
tempnet = fsm_read_prolog(filename);
if (tempnet == NULL) {
fprintf(stderr, "%s: ", filename);
perror ("Error opening file");
return 1;
} else {
stack_add(tempnet);
return 0;
}
}
int iface_read_spaced_text(char *filename) {
struct fsm *net;
net = fsm_read_spaced_text_file(filename);
if (net == NULL) {
fprintf(stderr, "%s: ", filename);
perror("File error");
return 1;
}
stack_add(fsm_topsort(fsm_minimize(net)));
return 0;
}
int iface_read_text(char *filename) {
struct fsm *net;
net = fsm_read_text_file(filename);
if (net == NULL) {
fprintf(stderr, "%s: ", filename);
perror("File error");
return 1;
}
stack_add(fsm_topsort(fsm_minimize(net)));
return 0;
}
int iface_stack_check (int size) {
if (stack_size() < size) {
printf("Not enough networks on stack. Operation requires at least %i.\n",size);
return 0;
}
return 1;
}
void iface_substitute_symbol (char *original, char *substitute) {
if (iface_stack_check(1)) {
dequote_string(original);
dequote_string(substitute);
stack_add(fsm_topsort(fsm_minimize(fsm_substitute_symbol(stack_pop(), original, substitute))));
printf("Substituted '%s' for '%s'.\n", substitute, original);
}
}
void iface_substitute_defined (char *original, char *substitute) {
struct fsm *subnet;
struct fsm *newnet;
if (iface_stack_check(1)) {
dequote_string(original);
dequote_string(substitute);
if ((subnet = find_defined(substitute)) == NULL) {
printf("No defined network '%s'.\n",substitute);
} else {
newnet = fsm_substitute_label(stack_find_top()->fsm, original, subnet);
if (newnet == NULL) {
printf("Symbol '%s' does not occur.\n", original);
} else {
stack_pop();
printf("Substituted network '%s' for '%s'.\n", substitute, original);
stack_add(fsm_topsort(fsm_minimize(newnet)));
}
}
}
}
void iface_upper_words(int limit) {
char *result;
struct apply_handle *ah;
int i;
limit = (limit == -1) ? g_list_limit : limit;
if (iface_stack_check(1)) {
ah = stack_get_ah();
for (i = limit; i > 0; i--) {
result = apply_upper_words(ah);
if (result == NULL)
break;
printf("%s\n",result);
}
apply_reset_enumerator(ah);
}
}
void iface_prune() {
if (iface_stack_check(1))
stack_add(fsm_topsort(fsm_coaccessible(stack_pop())));
}
void iface_reverse() {
if (iface_stack_check(1))
stack_add(fsm_topsort(fsm_determinize(fsm_reverse((stack_pop())))));
}
void iface_rotate() {
if (iface_stack_check(1))
stack_rotate();
}
void iface_save_defined(char *filename) {
save_defined(filename);
}
void iface_save_stack(char *filename) {
gzFile *outfile;
struct stack_entry *stack_ptr;
if (iface_stack_check(1)) {
if ((outfile = gzopen(filename, "wb")) == NULL) {
printf("Error opening file %s for writing.\n", filename);
return;
}
printf("Writing to file %s.\n", filename);
for (stack_ptr = stack_find_bottom(); stack_ptr->next != NULL; stack_ptr = stack_ptr->next) {
foma_net_print(stack_ptr->fsm, outfile);
}
gzclose(outfile);
return;
}
}
void iface_show_variables() {
int i;
for (i=0; global_vars[i].name != NULL; i++) {
if (global_vars[i].type == FVAR_BOOL) {
printf("%-17.17s: %s\n",global_vars[i].name, *((int *)(global_vars[i].ptr)) == 1 ? "ON" : "OFF");
}
if (global_vars[i].type == FVAR_INT) {
printf("%-17.17s: %i\n",global_vars[i].name, *((int *)(global_vars[i].ptr)));
}
if (global_vars[i].type == FVAR_STRING) {
printf("%-17.17s: %s\n",global_vars[i].name, *((char **)(global_vars[i].ptr)) );
}
}
}
void iface_show_variable(char *name) {
int i;
for (i=0; global_vars[i].name != NULL; i++) {
if (strncmp(name,global_vars[i].name,8) == 0) {
printf("%s = %s\n",global_vars[i].name, ((int)(global_vars[i].ptr)) == 1 ? "ON" : "OFF");
return;
}
}
printf("*There is no global variable '%s'.\n",name);
}
void iface_set_variable(char *name, char *value) {
int i,j;