-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwParser.pm
1241 lines (1174 loc) · 41 KB
/
wParser.pm
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
####################
## wParser.pm
####################
##
## parse an entropy expression
##
###########################################################################
# This code is part of wITIP (a web based Information Theoretic Prover)
#
# Copyright 2017-2024 Laszlo Csirmaz, UTIA, Prague
# This program is free, open-source software. You may redistribute it
# and/or modify under the terms of the GNU General Public License (GPL).
# There is ABSOLUTELY NO WARRANTY, use at your own risk.
###########################################################################
package wParser;
use wUtils; # EPS, read_user_macros()
use strict;
use constant { EPS => wUtils::EPS };
#######################################################
=pod
=head1 wITIP perl modules
=head2 wParser.pm
General routines to parse macro definitions, constraints, queries.
=head2 Data structures
=over 2
=item Random variable representation
Random variables are represented as bits from 1..MAX_ID_NO; and a
variable list as bitmaps: bit $i is set if variable $i is in that
list.
=item Entropy expression
An entropy expression is a hash of { I<varlist> => I<coeff>} pairs;
I<varlist> is an integer representing the variable list, and I<coeff> is
the corresponding coefficient. Neither value is zero.
=item Macro
A macro is a hash of the following fields
std => 0/1 (1 if standard macro, don't report, don't delete)
argno => number of arguments, at least one
septype => bitmask for separators (separator or pipe character)
name => 'A' .. 'Z'
text => entropy expression; the $i-th argument is (1<<($i-1))
raw => the original textual form
label => a unique number identifying the macro
=item Constraint
A constraint is a hash with the following fields:
rel => "=", ">=", "markov", "common"
text => entropy expression or an array of expressions
skip => 0/1 (1 when the constraint is disabled)
raw => the original textual form
label => a unique number identifying the macro
when I<rel> is "markov" or "common", I<text> is an array of expressions,
all =0; otherwise I<text> is the entropy expression.
=item Identifier table
Random variable names used in constraints are stored in the identifier
table. This table can store at most `max_id_no' entries (variable lists
are handled as bitmasks, this number is either 30 or 60 depending on the
machine architecture). The table is an array [0 .. max_id_no-1] where the
i-th entry contains the string of the i+1-st variable, or "" or undef if
that slot is free. The id table is loaded and used when parsing a
query with constraints, or when a new constraint is added. It can start
empty when checking without constraints, or when adding a new macro. The id
table must be adjusted when a constrain is deleted, or when a new
constrain is added.
=back
=head2 Procedures
=over 2
=item $parser = new wParser($session)
Loads the default values and user macro definitions
=item $parser->parse_relation($result,$string,$standalone)
Parses the given $string as a relation. When $standalone is set, the relation
is to be checked without constraints, so the id table is not loaded. On success
the following fields of the hash $result are filled:
$result->{rel} = the relation of the query, one of "=" ">=" or "=?"
$result->{text}= the expression
When the relation is ">=" the expression should be checked
for >=0; if the relation is "=" then it should be checked for ==0; for
relation "=?" $parser->print_expression($result->{text}) gives the
unrolled result to be printed out.
Call $parser->errmsg() to find whether there was any error.
=item $parser->parse_constraint($result,$string)
Parses the given $string as a constraint. On success the following
fields of the hash $result are filled:
$result->{rel} = one of "markov", "common", "=", ">="
$result->{text} = the unrolled constraint depending on "rel"
$result->{raw} = the original string
When I<rel> is "=" (check for ==0) or ">=" (check for >=0) then I<text>
is an expression. When I<rel> is "markov" or "common", then I<text> is
an array of expressions, all of which must be ==0. To recover the new
id table, use $parser->get_id_table(); to find out whether there was
an error call $parser->errmsg().
=item $parser->parse_macro_definition($macro,$string)
Parses the macro definition given in $string. On success sets the
following fields in the $macro hash:
$macro->{name} = the macro name
$macro->{argno} = number of arguments
$macro->{septype} = argument separator bitmask
$macro->{std} = 0
$macro->{text} = the expanded macro text
$macro->{raw} = the original string
These fields are set properly only when there were no errors.
=back
=head2 Auxiliary procedures
=over 2
=item $parser->errmsg($aux)
Without argument returns the error string, which is "" if there were no
errors. When $aux is defined, the auxiliary error output is returned;
valid only when there was an error message. Typically the unrolled form of
the relation when complaining about its triviality.
=item $parser->errpos()
Returns the error position; valid only when errmsg() returns a non-empty
string.
=item $parser->print_expression($expr)
The input is an expression hash, the returned value is the unrolled
string representation using the most recent variable table and style.
=item $parser->find_macro($macrohead)
Returns the index of the macro with the corresponding name, argno,
and septype, or -1 if no such a macro exists.
=item $parser->print_macro($macro)
The argument is a macro structure. Returns the text of the given macro
using the recent style. Clobbers the variable table.
=item $parser->load_id_table()
Populates the local copy if the id table from the saved one. Use before
calling print_expression() for a showing the internal form of a constraint.
=item $parser->get_id_table()
Returns the local copy of the id table filled with the latest variable
names. Use when adding a new constraint.
=back
=cut
#######################################################
sub new {
my($class,$session)=@_;
my $self={session => $session};
bless $self, $class;
## configuration
my $config = {
style => 1, # 0/1 : 0 - full, 1 - simple
revIng => 0, # 0/1 : 1 - reverse Ingleton notation
parent => 1, # 0/1 : allow () for grouping
braces => 1, # 0/1 : allow {} for grouping
varprime => 1, # 0/1 : prime(s) at the end
vardig => 0, # 0/1/2 : simple style var: a1, a123
var_dig => 0, # 0/1/2 simple style var: a_1, a_123
sepchar => ',',# separator character
macroarg => 1, # 0/1 : all macro arguments must be used
# maximal values
max_id_no => 20,
max_id_length => 20,
};
foreach my $key (keys %$config){
my $v=$session->getconf($key);
$config->{$key}=$v if(defined $v);
}
$config->{sepchar} = ';' if(!$config->{style});
$self->{config}=$config;
## syntax error
$self->{syntax_error} = {
softerr => "", softerrpos => 0, # out of bound error
harderr => "", harderrpos => 0, # syntax error
auxtext => "", # additional text
};
## string to be parsed
$self->{Xstr}=[]; # the string as a char array, closed by \0
$self->{Xpos}=-1; # the position we are looking at
$self->{Xchr}=' '; # this character
## id table
$self->{id_table} =[]; # the id string 0 .. MAX_ID_NO-1
$self->{no_new_id} =""; # error message if no new id is allowed
## macros, always get the full set
$self->{macros} = wUtils::read_user_macros($session);
return $self;
}
#############################################################
sub getconf {
my($self,$key)=@_;
return $self->{config}->{$key};
}
sub getmacros {
my($self)=@_;
return $self->{macros};
}
#############################################################
## Error handling
##
sub clear_errors { # clear all errors
my($self)=@_;
$self->{syntax_error}->{softerr}="";
$self->{syntax_error}->{harderr}="";
$self->{syntax_error}->{auxtext}="";
}
sub was_harderr {
my($self)=@_;
return $self->{syntax_error}->{harderr};
}
sub errmsg {
my($self,$aux)=@_;
return $self->{syntax_error}->{auxtext}
if(defined $aux);
return $self->{syntax_error}->{harderr} ||
$self->{syntax_error}->{softerr};
}
sub errpos {
my($self)=@_;
return $self->{syntax_error}->{harderr} ?
$self->{syntax_error}->{harderrpos} :
$self->{syntax_error}->{softerrpos};
}
sub harderr {
my($self,$err,$aux)=@_;
return if($self->was_harderr());
$self->{syntax_error}->{harderr} = $err;
$self->{syntax_error}->{harderrpos} = $self->{Xpos};
$self->{syntax_error}->{auxtext}=$aux if(defined $aux);
}
sub softerr {
my($self,$err,$aux)=@_;
return if($self->{syntax_error}->{softerr});
$self->{syntax_error}->{softerr} = $err;
$self->{syntax_error}->{softerrpos} = $self->{Xpos};
return if($self->was_harderr());
$self->{syntax_error}->{auxtext}=$aux if(defined $aux);
}
##
use constant { ## the error messages
e_TOO_MANY_ID => "too many different random variables",
e_TOO_LONG_ID => "too long identifier",
e_NO_CHECK_INPUT => "please enter the relation to be checked",
e_NO_CONSTR_INPUT=> "please enter a new constraint",
e_DIGIT_EXPECTED => "a digit is expected after the underscore",
e_VAR_EXPECTED => "variable is expected after a comma ','",
e_GREATER => "> should be followed by =",
e_LESS => "< should be followed by =",
e_EQUAL => "? should be followed by =",
e_INGLETONVAR => "in Ingleton expression a variable list is expected here",
e_INGLETONSEP => "in Ingleton expression a separator character is expected here",
e_INGLETONCLOSE => "in Ingleton expression closing ] is missing here",
e_IEXPR2 => "variable list is missing after | symbol",
e_CLOSING => "a closing ')' is expected here",
e_COMMA_OR_BAR => "either a list separator or '|' is expected here",
e_VARLIST => "variable list is expected here",
e_CONDEXPR => "( should be followed by a variable list",
e_NOMACRO => "no macro with this name is defined",
e_NOMACROARG => "no macro with this name and pattern is defined",
e_ENTROPYBRACE => "missing or wrong entropy expression between { and }",
e_BRACEEXPECTED => "a closing '}' is expected here",
e_ENTROPYPAREN => "missing or wrong entropy expression between ( and )",
e_PARENEXPECTED => "a closing ')' is expected here",
e_COEFFTERM => "a coefficient or an entropy term is expected here",
e_ENTROPYTERM => "an entropy term is expected here",
e_NOLHS => "the left hand side must be either the constant zero, or an entropy expression",
e_NORELATION => "there must be an '=', '<=', '>=', or '=?' here",
e_RELEXPECTED => "there must be an '=', '<=' or '>=' here",
e_NORHS => "the right hand side must be either the constant zero, or an entropy expossion",
e_EXTRATEXT => "extra characters at the end",
e_SIMPLIFIESTO => "the query simplifies to ",
e_SIMPLIFIESEND => ", thus it is always TRUE",
e_POSCOMB => "the relation is TRUE: a nonnegative combination of entropy values is always >=0",
e_SINGLETERM => "the expression simplifies to a single term, no check is performed",
e_FUNC_EQUAL => "the first variable set is a function of the second one",
e_INDEP_AGAIN => "this variable set occurs earlier - cannot be independent",
e_INDEP_FUNC => "the variable set \"",
e_INDEP_FUNCEND => "\" is a function of others - cannot be independent",
e_MARKOV => "a Markov chain must contain at least three tags",
e_NOMARKOV => "no need to add as a constraint: this sequence always forms a Markov chain",
e_COMMONBIG => "common information can be stipulated for at most 12 variable sets",
e_NOCOMMON => "no need to add as a constraint: the first set is the common information of the others",
e_MDEF_NAME => "macro definition starts with the macro name - an upper case letter - followed by a '('",
e_MDEF_NOPAR => "missing macro argument: a single variable is expected here",
e_MDEF_SAMEPAR => "all macro arguments must be different",
e_MDEF_PARSEPAR => "a ')', a list separator, or '|' is expected here",
e_MDEF_NOEQ => "macro text should start with an '=' symbol",
e_MDEF_NOTEXT => "cannot parse the macro text",
e_MDEF_SIMP0 => "the macro text simplifies to 0, not stored",
e_MDEF_UNUSED => "this argument is not used in the final macro text, which is:",
e_NEWID_IN_MACRO => "only macro arguments can be used as random variables",
};
sub _extrachar {
my($chr)=@_;
return e_EXTRATEXT if(ord($chr)>0x20);
return e_EXTRATEXT." (newline)" if($chr eq "\r" || $chr eq "\n");
return e_EXTRATEXT." (tab)" if($chr eq "\t");
return e_EXTRATEXT." (control char \x".hex(ord($chr)).")";
}
#############################################################
## Identifiers
##
sub get_id_table {
my($self)=@_;
return $self->{id_table};
}
sub clear_id_table { # clear all stored ID's
my($self)=@_;
my $id_table=$self->get_id_table();
for my $i(0 .. $self->getconf("max_id_no")-1){
$id_table->[$i]="";
}
}
sub load_id_table { # load user-defined id table
my($self)=@_;
my $id_table = $self->get_id_table();
my $utable = wUtils::read_user_id_table($self->{session});
for my $i(0 .. $self->getconf("max_id_no")-1){
$id_table->[$i]= ($utable->[$i] || "");
}
}
sub no_new_id { # no more new entries, use what is available
my($self,$msg)=@_;
$self->{no_new_id} = $msg;
}
sub search_id { # search an ID in the id_table; return the index
my ($self,$var)=@_;
my $id_table = $self->get_id_table();
my $empty_slot=-1;
my $maxid = $self->getconf("max_id_no")-1;
for my $i( 0 .. $maxid){
return $i if( $var eq $id_table->[$i] );
$empty_slot=$i if($empty_slot<0 && !$id_table->[$i]);
}
# not found
if($self->{no_new_id}) {
$self->harderr($self->{no_new_id});
return 0;
}
if($empty_slot<0){
$self->softerr(e_TOO_MANY_ID);
return $maxid;
}
$id_table->[$empty_slot]=$var;
return $empty_slot;
}
sub get_idlist_repr { # representation of a list of ID's
my($self,$v)=@_;
my @unsorted=();
my $id_table = $self->get_id_table();
my $style = $self->getconf("style");
my $i=0;
while($v){
if($v&1){
push @unsorted, $id_table->[$i] || "?";
}
$v>>=1; $i++;
}
my $res="";
foreach my $id(sort @unsorted){
$res .= "," if($res && !$style);
$res .= $id;
}
if(length($res)>200){
$res=substr($res,0,197)."...";
}
return $res;
}
#############################################################
## Entropy expressions
##
sub H1 { # $e += $v*H(a)
my ($e,$a,$v)=@_;
$e->{$a}=$v+($e->{$a}||0);
}
sub H2 { # e += v*H(a|b)
my($e,$a,$b,$v)=@_;
$e->{$b}= -$v + ($e->{$b}||0);
$e->{$a|$b}=$v+($e->{$a|$b}||0);
}
sub I2 { # e += v*I(a,b)
my($e,$a,$b,$v)=@_;
$e->{$a}=$v+($e->{$a}||0);
$e->{$b}=$v+($e->{$b}||0);
$e->{$a|$b}=-$v+($e->{$a|$b}||0);
}
sub I3 { # e += v*I(a,b|c)
my($e,$a,$b,$c,$v)=@_;
$e->{$a|$c}=$v+($e->{$a|$c}||0);
$e->{$b|$c}=$v+($e->{$b|$c}||0);
$e->{$c}=-$v+($e->{$c}||0);
$e->{$a|$b|$c}=-$v+($e->{$a|$b|$c}||0);
}
sub collapse_expr { # number and negative entries in $e
my ($e,$negate)=@_;
my $n=0; my $g=0;
foreach my $k (keys %$e){
my $v=$e->{$k};
if( -EPS <= $v && $v<= EPS ){
delete $e->{$k};
next;
}
if($negate){ $v=-$v; $e->{$k}=$v; }
$n++;
$g++ if($v<0);
}
return ($n,$g);
}
#############################################################
## Macros
##
# macro:
# { std => 0/1 (1 if standard, don't report)
# argno => number of arguments
# septype => bitmask for separator ( ; or | )
# name => 'A' .. 'Z'
# text => entropy expression, hash of
# { var1=>coeff1, var2=>coeff2, ...}
sub find_macro { # find macro with the given argno, septype and name
my($self,$what) = @_;
my $i=0;
foreach my $m(@{$self->getmacros()}){
return $i if($m->{name} eq $what->{name} &&
$m->{septype}==$what->{septype} &&
$m->{argno} == $what->{argno});
$i++;
}
return -1;
}
sub find_macro_partial { # find macro with the next separator ; or |
my($self,$what,$pt)=@_;
my $mask=(1<<$what->{argno})-1;
my $type=$what->{septype};
if($pt){ $type |= (1<<($what->{argno}-1)); }
foreach my $m (@{$self->getmacros()}){
return 0 if($m->{name} eq $what->{name} &&
$m->{argno} > $what->{argno} &&
($m->{septype}&$mask)== $type);
}
return -1;
}
sub _create_macrovar {
my($pattern,$args)=@_;
my $i=0; my $res=0; while($pattern){
if($pattern&1){$res |= $args->[$i]; }
$i++; $pattern >>=1;
}
return $res;
}
sub add_macrotext { # e += d* (the evaluated macro)
my($self,$e,$macrono,$args,$d)=@_;
return if($macrono<0);
my $text = $self->getmacros()->[$macrono]->{text};
foreach my $v (keys %$text){
my $w=_create_macrovar($v,$args);
$e->{$w}=$d*$text->{$v}+($e->{$w}||0);
}
}
#############################################################
## print an expression or a macro
##
sub _bitsof {
my $n=shift; my $v=0;
while($n){
$v++ if($n&1); $n>>=1;
}
return $v;
}
sub print_expression {
my($self,$e)=@_;
my %repr=(); my $total=0;
foreach my $k (keys %$e){
$repr{$k}=[_bitsof($k),$self->get_idlist_repr($k)];
$total++;
}
if($total==0){ return "0"; }
my $res="";
foreach my $k (sort
{ $repr{$a}->[0] <=> $repr{$b}->[0] || $repr{$a}->[1] cmp $repr{$b}->[1] }
keys %repr){
my $d=$e->{$k};
if($d<1.0+EPS && $d>1.0-EPS) { $res .="+"; }
elsif($d<-1.0+EPS && $d>-1.0-EPS) { $res .="-"; }
else { $res .= sprintf("%+g",$d); }
if($self->getconf("style")){ # simple
$res .= $repr{$k}->[1];
} else {
$res .= "H(".$repr{$k}->[1].")";
}
}
$res =~ s/^\+//;
return $res;
}
sub print_macro { # clobbers the id table
my ($self,$macro)=@_;
my $X_sep=$self->getconf("sepchar");
return "" if(!defined $macro);
my $res = $macro->{name}."(";
my $baseletter= $self->getconf("style") ? ord('a') : ord('A');
my $septype = $macro->{septype};
# one can save the id table here and restore later
$self->clear_id_table();
for my $v(0..$macro->{argno}-1){
my $arg=chr($v+$baseletter);
$self->search_id($arg);
$res .= $arg . ($v==$macro->{argno}-1 ? ")" : ($septype&1) ? "|" : $X_sep);
$septype >>=1 ;
}
$res .= "=" . $self->print_expression($macro->{text});
return $res;
}
#############################################################
## Character parsing
##
sub next_chr { # get next visible char
my($self)=@_;
do {
$self->{Xpos}++;
$self->{Xchr}=$self->{Xstr}->[$self->{Xpos}];
} while ( $self->{Xchr} eq ' ' );
}
sub next_visible { # skip to the next visible char
my($self)=@_;
while($self->{Xchr} eq ' '){
$self->{Xpos}++;
$self->{Xchr}=$self->{Xstr}->[$self->{Xpos}];
}
}
sub next_idchr { # next ID character
my($self)=@_;
$self->{Xpos}++;
$self->{Xchr}=$self->{Xstr}->[$self->{Xpos}];
}
sub save_pos {
my($self)=@_;
return $self->{Xpos};
}
sub restore_pos {
my($self,$oldpos)=@_;
$self->{Xpos}=$oldpos;
$self->{Xchr}=$self->{Xstr}->[$self->{Xpos}];
}
sub init_parse {
my($self,$string)=@_;
$string =~ s/\t/ /g;
my @X_str = split('',$string."\x0");
$self->{Xstr}=\@X_str;
$self->{Xpos}=-1;
$self->next_chr();
}
sub R { # check if the next char is chr
my($self,$chr)=@_;
return 0 if($self->{Xchr} ne $chr);
## $self->next_chr();
do {
$self->{Xpos}++;
$self->{Xchr}=$self->{Xstr}->[$self->{Xpos}];
} while ( $self->{Xchr} eq ' ' );
return 1;
}
sub expect_oneof { # expect one of three characters
my($self,$c1,$c2,$c3)=@_;
return 1 if($c1 && $self->R($c1));
return 2 if($c2 && $self->R($c2));
return 3 if($c3 && $self->R($c3));
return 0 if($self->was_harderr());
my $n=0; $n++ if($c1); $n++ if($c2); $n++ if ($c3);
if($n==1){
$self->harderr(sprintf("the symbol %s is expected here",$c1||$c2||$c3));
} elsif($n==2){
$self->harderr(sprintf("either %s or %s is expected here",($c1||$c2),($c3||$c2)));
} else {
$self->harderr("one of $c1, $c2, or $c3 is expected here");
}
return 0;
}
#############################################################
## Numbers
##
sub is_digit { # when a digit, return the value
my($self)=@_;
return 0 if ($self->{Xchr} lt '0' || $self->{Xchr} gt '9');
$_[1]=ord($self->{Xchr})-ord('0');
## $self->next_chr();
do {
$self->{Xpos}++;
$self->{Xchr}=$self->{Xstr}->[$self->{Xpos}];
} while ( $self->{Xchr} eq ' ' );
return 1;
}
sub frac_part { # /\.\d+/
my($self)=@_;
my $oldpos=$self->save_pos(); my $i=0;
if($self->R('.') && $self->is_digit($i)){
my $scale=0.1; my $v=$scale * $i;
while($self->is_digit($i)){
$scale *=0.1; $v+= $scale *$i;
}
$_[1]=$v;
return 1;
}
$self->restore_pos($oldpos);
return 0;
}
sub is_number { # \d+ | \d?\.\d+
my($self)=@_;
my $i=0;
if($self->is_digit($i)){
my $v=$i;
while($self->is_digit($i)){ $v=$v*10.0+$i; }
my $w=0.0; $self->frac_part($w);
$_[1]=$v+$w;
return 1;
}
return $self->frac_part($_[1]);
}
# single +, -, or a number optionally followed by a *
sub is_signed_number {
my($self)=@_;
my $v=1.0;
if($self->R('+')){
$self->is_number($v) && $self->R('*'); $_[1]=$v;
return 1;
}
if($self->R('-')){
$self->is_number($v) && $self->R('*'); $_[1]=-$v;
return 1;
}
if($self->is_number($_[1])){
$self->R('*');
return 1;
}
return 0;
}
sub is_zero { # zero number
my($self)=@_;
my $v=0; my $oldpos=$self->save_pos();
return 1 if($self->is_number($v) && -EPS<=$v && $v <= EPS);
$self->restore_pos($oldpos);
return 0;
}
#############################################################
## Identifier, list of identifiers
##
sub is_variable {
my($self)=@_;
my $var="";
if($self->getconf("style")){ ## simple style
if($self->{Xchr} =~ /[a-z]/ ){
$var=$self->{Xchr}; $self->next_idchr();
if( $self->getconf("var_dig") && $self->{Xchr} eq '_'){
$self->next_idchr(); if($self->{Xchr} =~ /\d/){
$var .= "_$self->{Xchr}"; $self->next_idchr();
while($self->getconf("var_dig")>1 && $self->{Xchr} =~ /\d/){
$var .= $self->{Xchr}; $self->next_idchr();
}
} else { $self->harderr(e_DIGIT_EXPECTED); }
} elsif($self->getconf("vardig") && $self->{Xchr} =~ /\d/ ){
$var .= $self->{Xchr}; $self->next_idchr();
while($self->getconf("vardig")>1 && $self->{Xchr} =~ /\d/){
$var .= $self->{Xchr}; $self->next_idchr();
}
}
}
} else { # full style
if( $self->{Xchr} =~ /[A-Za-z]/ ){
$var=$self->{Xchr}; $self->next_idchr();
while( $self->{Xchr} =~ /^\w/ ){
$var .= $self->{Xchr}; $self->next_idchr();
}
}
}
return 0 if(!$var);
if($self->getconf("varprime")){
while($self->{Xchr} eq '\''){ $var .= '\''; $self->next_idchr(); }
}
if(length($var)>$self->getconf("max_id_length")){
softerr(e_TOO_LONG_ID);
}
$self->next_visible();
$_[1]=1<< ($self->search_id($var));
return 1;
}
sub is_varlist {
my ($self)=@_;
my($v,$w)=(0,0);
if($self->is_variable($v)){
if($self->getconf("style")){ #simple
while($self->is_variable($w)){ $v |= $w; }
} else { # full
while($self->R(',')){
$self->is_variable($w)||$self->harderr(e_VAR_EXPECTED);
$v |= $w;
}
}
$_[1]=$v;
return 1;
}
return 0;
}
sub is_macro_name {
my($self)=@_;
if($self->{Xchr} =~ /[A-Z]/){
$_[1]=$self->{Xchr};
## $self->next_chr();
do {
$self->{Xpos}++;
$self->{Xchr}=$self->{Xstr}->[$self->{Xpos}];
} while ( $self->{Xchr} eq ' ' );
return 1;
}
return 0;
}
#############################################################
## Relation symbol
##
sub is_zapped { # will it result in =?
my($str)=@_;
return 1 if($str =~ /=\s*[\?=]/ );
return 1 if($str =~ /\?\s*=/ );
return 0;
}
sub is_relation { # one of =, <=, >=, =?
my($self)=@_;
my $relsym="";
if($self->R('=')){
$relsym = "=";
$relsym .= "?" if( $self->R('?')||$self->R('='));
} elsif( $self->R('<') ){
$relsym = "<=";
$self->R('=') || $self->harderr(e_LESS);
} elsif( $self->R('>') ){
$relsym = ">=";
$self->R('=') || $self->harderr(e_GREATER);
} elsif( $self->R('?') ){
$relsym="=?";
$self->R('=') || $self->harderr(e_EQUAL);
}
if($relsym){ $_[1]=$relsym; return 1; }
return 0;
}
#############################################################
## Entropy terms
##
sub is_Ingleton {
my ($self,$e,$v)=@_;
if($self->R('[')){
my $X_sep =$self->getconf("sepchar");
my($a,$b,$c,$d)=(0,0,0,0);
$self->is_varlist($a) || $self->harderr(e_INGLETONVAR);
$self->R($X_sep) || $self->harderr(e_INGLETONSEP);
$self->is_varlist($b) || $self->harderr(e_INGLETONVAR);
$self->R($X_sep) || $self->harderr(e_INGLETONSEP);
$self->is_varlist($c) || $self->harderr(e_INGLETONVAR);
$self->R($X_sep) || $self->harderr(e_INGLETONSEP);
$self->is_varlist($d) || $self->harderr(e_INGLETONVAR);
$self->R(']') || $self->harderr(e_INGLETONCLOSE);
if($self->getconf("revIng")){
# [a,b,c,d] =-(c,d)+(c,d|a)+(c,d|b)+(a,b)
I2($e,$c,$d,-$v); I3($e,$c,$d,$a,$v);
I3($e,$c,$d,$b,$v); I2($e,$a,$b,$v);
} else {
# [a,b,c,d] =-(a,b)+(a,b|c)+(a,b|d)+(c,d)
I2($e,$a,$b,-$v); I3($e,$a,$b,$c,$v);
I3($e,$a,$b,$d,$v); I2($e,$c,$d,$v);
}
return 1;
}
return 0;
}
sub is_par_expression { # (a,b) (a|b) (a,b|c)
my ($self,$e,$v)=@_;
return 0 if(!$self->getconf("style"));
my $oldpos=$self->save_pos(); my($a,$b,$c)=(0,0,0);
if($self->R('(') && $self->is_varlist($a)){
if($self->R('|')){ # (a|b)
$self->is_varlist($b) || $self->harderr(e_IEXPR2);
$self->R(')') || $self->harderr(e_CLOSING);
H2($e,$a,$b,$v);
return 1;
}
if($self->R($self->getconf("sepchar"))){
$self->is_varlist($b) || $self->harderr(e_VARLIST);
if($self->R('|')){ # (a,b|c)
$self->is_varlist($c) || $self->harderr(e_VARLIST);
I3($e,$a,$b,$c,$v);
} else { # (a,b)
I2($e,$a,$b,$v);
}
$self->R(')') || $self->harderr(e_CLOSING);
return 1;
}
$self->getconf("parent") || $self->harderr(e_COMMA_OR_BAR);
}
$self->restore_pos($oldpos);
return 0;
}
sub is_simple_expression { # a a,b a|b a,b|c
my ($self,$e,$v)=@_;
return 0 if(!$self->getconf("style"));
my($a,$b,$c)=(0,0,0);
if($self->is_varlist($a)){
if($self->R('|')){
$self->is_varlist($b) || $self->harderr(e_IEXPR2);
H2($e,$a,$b,$v);
} elsif($self->R($self->getconf("sepchar"))){ # a,b
$self->is_varlist($b) || $self->harderr(e_VARLIST);
if($self->R('|')){ # a,b|c
$self->is_varlist($c) || $self->harderr(e_VARLIST);
I3($e,$a,$b,$c,$v);
} else {
I2($e,$a,$b,$v);
}
} else { # a
H1($e,$a,$v);
}
return 1;
}
return 0;
}
sub is_macro_invocation { # 3.14*A(ab,ac|ad|ae)
my ($self,$e,$coeff)=@_; my $name="";
my $oldpos=$self->save_pos();
if($self->is_macro_name($name) && $self->R('(')){
my $what={name => $name, argno => 0, septype => 0, args => [] };
if($self->find_macro_partial($what,0)<0){
$self->restore_pos($oldpos);
$self->harderr(e_NOMACRO);
return 0;
}
for(my $done=0;!$done;){
my $v=0; $self->is_varlist($v) || $self->harderr($what->{argno}?e_VARLIST : e_CONDEXPR);
push @{$what->{args}},$v;
$what->{argno}++;
my $nextch=$self->expect_oneof(
($self->find_macro($what)>=0 ? ')' : ""), # we have such a macro
($self->find_macro_partial($what,0)>=0 ? $self->getconf("sepchar") : ""),
($self->find_macro_partial($what,1)>=0 ? '|' : ""));
if($nextch==1){ # )
$done=1;
} elsif($nextch==2){ # sep
} elsif($nextch==3){ # |
$what->{septype} |= (1<<($what->{argno}-1));
} else { # error
$done=1;
}
}
my $macrono=$self->find_macro($what);
$macrono>=0 || $self->harderr(e_NOMACROARG);
$self->add_macrotext($e,$macrono,$what->{args},$coeff);
return 1;
}
$self->restore_pos($oldpos);
return 0;
}
#############################################################
## Entropy expression
##
sub coeff_term { # term preceeded by an optional coefficient
my($self,$e,$d)=@_;
my $oldpos=$self->save_pos();
my $coeff=1.0; $self->is_signed_number($coeff);
$coeff *= $d;
return 1 if(
$self->is_Ingleton($e,$coeff) ||
$self->is_par_expression($e,$coeff) ||
$self->is_simple_expression($e,$coeff) ||
$self->is_macro_invocation($e,$coeff) );
if($self->getconf("braces") && $self->R('{')){
$self->entropy_expression($e,$coeff) || $self->harderr(e_ENTROPYBRACE);
$self->R('}') || $self->harderr(e_BRACEEXPECTED);
return 1;
}
if($self->getconf("parent") && $self->R('(')){
$self->entropy_expression($e,$coeff) || $self->harderr(e_ENTROPYPAREN);
$self->R(')') || $self->harderr(e_PARENEXPECTED);
return 1;
}
$self->restore_pos($oldpos);
return 0;
}
sub entropy_expression { # sequence of entropy terms separated by +/-
my ($self,$e,$d)=@_;
return 0 if($self->was_harderr());
if($self->coeff_term($e,$d)){
while(!$self->was_harderr() && $self->{Xchr} =~ /[+\-]/){
$self->coeff_term($e,$d) ||
$self->harderr($self->is_signed_number($d) ? e_ENTROPYTERM : e_COEFFTERM);
}
return 1;
}
return 0;
}
#############################################################
## Entropy relation
##
# $result->{text} -- the expression reduced to =,>=, =?
# $result->{rel} -- the relation, one of =, >=, =?
# $result->{n} -- number of entries
sub _parse_relation { # relation or constraint
my($self,$result,$zap)=@_;
my $e={}; # the expression
my $relsym=""; # relation, one of "=" "<=" ">=" or "=?" when $zap
$self->entropy_expression($e,1.0) || $self->is_zero() || $self->harderr(e_NOLHS);
$self->is_relation($relsym) || $self->harderr($zap ? e_NORELATION : e_RELEXPECTED);
$zap || $relsym ne "=?" || $self->harderr(e_RELEXPECTED);
$self->entropy_expression($e,-1.0) || $self->is_zero() || $self->harderr(e_NORHS);
$self->{Xchr} eq "\0" || $self->harderr(_extrachar($self->{Xchr}));
# convert it to =0, >=0, =?
my ($n,$g)=collapse_expr($e, $relsym eq "<=");
if( $relsym ne "=?" && !($zap && $n==0)){ # = or >= or <=
$n || $self->softerr(e_SIMPLIFIESTO . "0 $relsym 0" . e_SIMPLIFIESEND);
$g || $relsym eq "=" || $self->softerr(e_POSCOMB,$self->print_expression($e));
!$zap || $n>1 || $self->softerr(e_SINGLETERM,$self->print_expression($e));
}
$result->{text}= $e;
$result->{rel} = ($relsym eq "<=" ? ">=" : $relsym);
$result->{n} = $n;
}
sub parse_relation {
my($self,$result,$str,$standalone)=@_;
$self->clear_errors();
# clear or load the id table
if($standalone || is_zapped($str) ){ $self->clear_id_table(); }
else { $self->load_id_table(); }
$self->init_parse($str);
$str !~ /^\s*$/ || $self->harderr(e_NO_CHECK_INPUT);
$self->_parse_relation($result,1); # =? allowed
}
#############################################################
## Constraint
##
# it can be a RELATION or one of the following:
# a : b or
# a << b functional dependence
# a . b . c or
# a || b || c totally independent
# a / b / c or
# a -> b-> c Markov chain
# a << b / c common information
# $result->{text}-- expression for "=" and ">=";
# array of expressions for "markov" or "common"
# $result->{rel} -- one of "=", ">=", "markov", "common"
sub _funcdep {
my($self,$result,$v1,$v2)=@_;
$v1 |= $v2;
$v1!=$v2 || $self->harderr(e_FUNC_EQUAL);
$self->{Xchr} eq "\0" || $self->harderr(_extrachar($self->{Xchr}));
$result->{rel} ="=";
$result->{text} = { "$v1" => 1, "$v2" => -1 };
}
sub _indep {
my($self,$result,$sep,$v1,$v2)=@_;