-
Notifications
You must be signed in to change notification settings - Fork 6
/
Main.php
4113 lines (3905 loc) · 149 KB
/
Main.php
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
<?php
define('NO_OFFSET', -2147483647);
define('DEBUG', 0);
class LemonStateNode
{
public $key;
public $data;
public $from = 0;
public $next = 0;
}
/**
* The state of the yy_action table under construction is an instance of
* the following structure
*/
class LemonActtab
{
public $nAction = 0; /* Number of used slots in aAction[] */
public $aAction = /* The yy_action[] table under construction */
array(array(
'lookahead' => -1, /* Value of the lookahead token */
'action' => -1 /* Action to take on the given lookahead */
));
public $aLookahead = /* A single new transaction set */
array(array(
'lookahead' => 0, /* Value of the lookahead token */
'action' => 0 /* Action to take on the given lookahead */
));
public $mnLookahead = 0; /* Minimum aLookahead[].lookahead */
public $mnAction = 0; /* Action associated with mnLookahead */
public $mxLookahead = 0; /* Maximum aLookahead[].lookahead */
public $nLookahead = 0; /* Used slots in aLookahead[] */
/**
* Add a new action to the current transaction set
* @param int
* @param int
*/
function acttab_action($lookahead, $action)
{
if ($this->nLookahead === 0) {
$this->aLookahead = array();
$this->mxLookahead = $lookahead;
$this->mnLookahead = $lookahead;
$this->mnAction = $action;
} else {
if ($this->mxLookahead < $lookahead) {
$this->mxLookahead = $lookahead;
}
if ($this->mnLookahead > $lookahead) {
$this->mnLookahead = $lookahead;
$this->mnAction = $action;
}
}
$this->aLookahead[$this->nLookahead] = array(
'lookahead' => $lookahead,
'action' => $action);
$this->nLookahead++;
}
/**
* Add the transaction set built up with prior calls to acttab_action()
* into the current action table. Then reset the transaction set back
* to an empty set in preparation for a new round of acttab_action() calls.
*
* Return the offset into the action table of the new transaction.
*/
function acttab_insert()
{
if ($this->nLookahead <= 0) {
throw new Exception('nLookahead is not set up?');
}
/* Scan the existing action table looking for an offset where we can
** insert the current transaction set. Fall out of the loop when that
** offset is found. In the worst case, we fall out of the loop when
** i reaches $this->nAction, which means we append the new transaction set.
**
** i is the index in $this->aAction[] where $this->mnLookahead is inserted.
*/
for ($i = 0; $i < $this->nAction + $this->mnLookahead; $i++) {
if (!isset($this->aAction[$i])) {
$this->aAction[$i] = array(
'lookahead' => -1,
'action' => -1,
);
}
if ($this->aAction[$i]['lookahead'] < 0) {
for ($j = 0; $j < $this->nLookahead; $j++) {
if (!isset($this->aLookahead[$j])) {
$this->aLookahead[$j] = array(
'lookahead' => 0,
'action' => 0,
);
}
$k = $this->aLookahead[$j]['lookahead'] -
$this->mnLookahead + $i;
if ($k < 0) {
break;
}
if (!isset($this->aAction[$k])) {
$this->aAction[$k] = array(
'lookahead' => -1,
'action' => -1,
);
}
if ($this->aAction[$k]['lookahead'] >= 0) {
break;
}
}
if ($j < $this->nLookahead ) {
continue;
}
for ($j = 0; $j < $this->nAction; $j++) {
if (!isset($this->aAction[$j])) {
$this->aAction[$j] = array(
'lookahead' => -1,
'action' => -1,
);
}
if ($this->aAction[$j]['lookahead'] == $j +
$this->mnLookahead - $i) {
break;
}
}
if ($j == $this->nAction) {
break; /* Fits in empty slots */
}
} elseif ($this->aAction[$i]['lookahead'] == $this->mnLookahead) {
if ($this->aAction[$i]['action'] != $this->mnAction) {
continue;
}
for ($j = 0; $j < $this->nLookahead; $j++) {
$k = $this->aLookahead[$j]['lookahead'] -
$this->mnLookahead + $i;
if ($k < 0 || $k >= $this->nAction) {
break;
}
if (!isset($this->aAction[$k])) {
$this->aAction[$k] = array(
'lookahead' => -1,
'action' => -1,
);
}
if ($this->aLookahead[$j]['lookahead'] !=
$this->aAction[$k]['lookahead']) {
break;
}
if ($this->aLookahead[$j]['action'] !=
$this->aAction[$k]['action']) {
break;
}
}
if ($j < $this->nLookahead) {
continue;
}
$n = 0;
for ($j = 0; $j < $this->nAction; $j++) {
if (!isset($this->aAction[$j])) {
$this->aAction[$j] = array(
'lookahead' => -1,
'action' => -1,
);
}
if ($this->aAction[$j]['lookahead'] < 0) {
continue;
}
if ($this->aAction[$j]['lookahead'] == $j +
$this->mnLookahead - $i) {
$n++;
}
}
if ($n == $this->nLookahead) {
break; /* Same as a prior transaction set */
}
}
}
/* Insert transaction set at index i. */
for ($j = 0; $j < $this->nLookahead; $j++) {
if (!isset($this->aLookahead[$j])) {
$this->aLookahead[$j] = array(
'lookahead' => 0,
'action' => 0,
);
}
$k = $this->aLookahead[$j]['lookahead'] - $this->mnLookahead + $i;
$this->aAction[$k] = $this->aLookahead[$j];
if ($k >= $this->nAction) {
$this->nAction = $k + 1;
}
}
$this->nLookahead = 0;
$this->aLookahead = array();
/* Return the offset that is added to the lookahead in order to get the
** index into yy_action of the action */
return $i - $this->mnLookahead;
}
}
/* Symbols (terminals and nonterminals) of the grammar are stored
** in the following: */
class LemonSymbol
{
const TERMINAL = 1;
const NONTERMINAL = 2;
const MULTITERMINAL = 3;
const LEFT = 1;
const RIGHT = 2;
const NONE = 3;
const UNK = 4;
public $name; /* Name of the symbol */
public $index; /* Index number for this symbol */
/* enum {
TERMINAL,
NONTERMINAL,
MULTITERMINAL
} */
public $type; /* Symbols are all either TERMINALS or NTs */
/**
* @var LemonRule
*/
public $rule; /* Linked list of rules of this (if an NT) */
/**
* @var LemonSymbol
*/
public $fallback; /* fallback token in case this token doesn't parse */
public $prec; /* Precedence if defined (-1 otherwise) */
/* enum e_assoc {
LEFT,
RIGHT,
NONE,
UNK
} */
public $assoc; /* Associativity if predecence is defined */
public $firstset; /* First-set for all rules of this symbol */
/**
* @var boolean
*/
public $lambda; /* True if NT and can generate an empty string */
public $destructor = 0; /* Code which executes whenever this symbol is
** popped from the stack during error processing */
public $destructorln; /* Line number of destructor code */
public $datatype; /* The data type of information held by this
** object. Only used if type==NONTERMINAL */
public $dtnum; /* The data type number. In the parser, the value
** stack is a union. The .yy%d element of this
** union is the correct data type for this object */
/* The following fields are used by MULTITERMINALs only */
public $nsubsym; /* Number of constituent symbols in the MULTI */
/**
* @var array an array of {@link LemonSymbol} objects
*/
public $subsym = array(); /* Array of constituent symbols */
private static $symbol_table = array();
/**
* Return a pointer to the (terminal or nonterminal) symbol "x".
* Create a new symbol if this is the first time "x" has been seen.
* (this is a singleton)
* @param string
* @return LemonSymbol
*/
public static function Symbol_new($x)
{
if (isset(self::$symbol_table[$x])) {
return self::$symbol_table[$x];
}
$sp = new LemonSymbol;
$sp->name = $x;
$sp->type = preg_match('/[A-Z]/', $x[0]) ? self::TERMINAL : self::NONTERMINAL;
$sp->rule = 0;
$sp->fallback = 0;
$sp->prec = -1;
$sp->assoc = self::UNK;
$sp->firstset = array();
$sp->lambda = false;
$sp->destructor = 0;
$sp->datatype = 0;
self::$symbol_table[$sp->name] = $sp;
return $sp;
}
/**
* Return the number of unique symbols
* @return int
*/
public static function Symbol_count()
{
return count(self::$symbol_table);
}
public static function Symbol_arrayof()
{
return array_values(self::$symbol_table);
}
public static function Symbol_find($x)
{
if (isset(self::$symbol_table[$x])) {
return self::$symbol_table[$x];
}
return 0;
}
/**
* Sort function helper for symbols
*
* Symbols that begin with upper case letters (terminals or tokens)
* must sort before symbols that begin with lower case letters
* (non-terminals). Other than that, the order does not matter.
*
* We find experimentally that leaving the symbols in their original
* order (the order they appeared in the grammar file) gives the
* smallest parser tables in SQLite.
* @param LemonSymbol
* @param LemonSymbol
*/
public static function sortSymbols($a, $b)
{
$i1 = $a->index + 10000000*(ord($a->name[0]) > ord('Z'));
$i2 = $b->index + 10000000*(ord($b->name[0]) > ord('Z'));
return $i1 - $i2;
}
/**
* Return true if two symbols are the same.
*/
public static function same_symbol(LemonSymbol $a, LemonSymbol $b)
{
if ($a === $b) return 1;
if ($a->type != self::MULTITERMINAL) return 0;
if ($b->type != self::MULTITERMINAL) return 0;
if ($a->nsubsym != $b->nsubsym) return 0;
for ($i = 0; $i < $a->nsubsym; $i++) {
if ($a->subsym[$i] != $b->subsym[$i]) return 0;
}
return 1;
}
}
/* Each production rule in the grammar is stored in the following
** structure. */
class LemonRule {
/**
* @var array an array of {@link LemonSymbol} objects
*/
public $lhs; /* Left-hand side of the rule */
public $lhsalias = array(); /* Alias for the LHS (NULL if none) */
public $ruleline; /* Line number for the rule */
public $nrhs; /* Number of RHS symbols */
/**
* @var array an array of {@link LemonSymbol} objects
*/
public $rhs; /* The RHS symbols */
public $rhsalias = array(); /* An alias for each RHS symbol (NULL if none) */
public $line; /* Line number at which code begins */
public $code; /* The code executed when this rule is reduced */
/**
* @var LemonSymbol
*/
public $precsym; /* Precedence symbol for this rule */
public $index; /* An index number for this rule */
public $canReduce; /* True if this rule is ever reduced */
/**
* @var LemonRule
*/
public $nextlhs; /* Next rule with the same LHS */
/**
* @var LemonRule
*/
public $next; /* Next rule in the global list */
}
/* A configuration is a production rule of the grammar together with
** a mark (dot) showing how much of that rule has been processed so far.
** Configurations also contain a follow-set which is a list of terminal
** symbols which are allowed to immediately follow the end of the rule.
** Every configuration is recorded as an instance of the following: */
class LemonConfig {
const COMPLETE = 1;
const INCOMPLETE = 2;
/**
* @var LemonRule
*/
public $rp; /* The rule upon which the configuration is based */
public $dot; /* The parse point */
public $fws; /* Follow-set for this configuration only */
/**
* @var LemonPlink
*/
public $fplp; /* Follow-set forward propagation links */
/**
* @var LemonPlink
*/
public $bplp; /* Follow-set backwards propagation links */
/**
* @var LemonState
*/
public $stp; /* Pointer to state which contains this */
/* enum {
COMPLETE, /* The status is used during followset and
INCOMPLETE /* shift computations
} */
public $status;
/**
* Index of next LemonConfig object
* @var int
*/
public $next; /* Next configuration in the state */
/**
* Index of the next basis configuration LemonConfig object
* @var int
*/
public $bp; /* The next basis configuration */
/**
* @var LemonConfig
*/
static public $current; /* Top of list of configs */
/**
* @var LemonConfig
*/
static public $currentend; /* Last on list of configs */
/**
* @var LemonConfig
*/
static public $basis; /* Top of list of basis configs */
/**
* @var LemonConfig
*/
static public $basisend; /* Last on list of basis configs */
static public $x4a = array();
/**
* Return a pointer to a new configuration
* @return LemonConfig
*/
private static function newconfig()
{
return new LemonConfig;
}
static function Configshow(LemonConfig $cfp)
{
$fp = fopen('php://output', 'w');
while ($cfp) {
if ($cfp->dot == $cfp->rp->nrhs) {
$buf = sprintf('(%d)', $cfp->rp->index);
fprintf($fp, ' %5s ', $buf);
} else {
fwrite($fp,' ');
}
$cfp->ConfigPrint($fp);
fwrite($fp, "\n");
if (0) {
//SetPrint(fp,cfp->fws,$this);
//PlinkPrint(fp,cfp->fplp,"To ");
//PlinkPrint(fp,cfp->bplp,"From");
}
$cfp = $cfp->next;
}
fwrite($fp, "\n");
fclose($fp);
}
/**
* Initialized the configuration list builder
*/
static function Configlist_init()
{
self::$current = 0;
self::$currentend = &self::$current;
self::$basis = 0;
self::$basisend = &self::$basis;
self::$x4a = array();
}
/**
* Remove all data from the table. Pass each data to the function "f"
* as it is removed. ("f" may be null to avoid this step.)
*/
static function Configtable_reset($f)
{
self::$current = 0;
self::$currentend = &self::$current;
self::$basis = 0;
self::$basisend = &self::$basis;
self::Configtable_clear(0);
}
/**
* Remove all data from the table. Pass each data to the function "f"
* as it is removed. ("f" may be null to avoid this step.)
*/
static function Configtable_clear($f)
{
if (!count(self::$x4a)) {
return;
}
if ($f) {
for ($i = 0; $i < count(self::$x4a); $i++) {
call_user_func($f, self::$x4a[$i]->data);
}
}
self::$x4a = array();
}
/**
* Initialized the configuration list builder
*/
static function Configlist_reset()
{
self::Configtable_clear(0);
}
/**
* Add another configuration to the configuration list
* @param LemonRule the rule
* @param int Index into the RHS of the rule where the dot goes
* @return LemonConfig
*/
static function Configlist_add($rp, $dot)
{
$model = new LemonConfig;
$model->rp = $rp;
$model->dot = $dot;
$cfp = self::Configtable_find($model);
if ($cfp === 0) {
$cfp = self::newconfig();
$cfp->rp = $rp;
$cfp->dot = $dot;
$cfp->fws = array();
$cfp->stp = 0;
$cfp->fplp = $cfp->bplp = 0;
$cfp->next = 0;
$cfp->bp = 0;
self::$currentend = $cfp;
self::$currentend = &$cfp->next;
self::Configtable_insert($cfp);
}
return $cfp;
}
/**
* Add a basis configuration to the configuration list
* @param LemonRule
* @param int
* @return LemonConfig
*/
static function Configlist_addbasis($rp, $dot)
{
$model = new LemonConfig;
$model->rp = $rp;
$model->dot = $dot;
$cfp = self::Configtable_find($model);
if ($cfp === 0) {
$cfp = self::newconfig();
$cfp->rp = $rp;
$cfp->dot = $dot;
$cfp->fws = array();
$cfp->stp = 0;
$cfp->fplp = $cfp->bplp = 0;
$cfp->next = 0;
$cfp->bp = 0;
self::$currentend = $cfp;
self::$currentend = &$cfp->next;
self::$basisend = $cfp;
self::$basisend = &$cfp->bp;
self::Configtable_insert($cfp);
}
return $cfp;
}
/* Compute the closure of the configuration list */
static function Configlist_closure(LemonData $lemp)
{
for ($cfp = self::$current; $cfp; $cfp = $cfp->next) {
$rp = $cfp->rp;
$dot = $cfp->dot;
if ($dot >= $rp->nrhs) {
continue;
}
$sp = $rp->rhs[$dot];
if ($sp->type == LemonSymbol::NONTERMINAL) {
if ($sp->rule === 0 && $sp !== $lemp->errsym) {
Lemon::ErrorMsg($lemp->filename, $rp->line,
"Nonterminal \"%s\" has no rules.", $sp->name);
$lemp->errorcnt++;
}
for ($newrp = $sp->rule; $newrp; $newrp = $newrp->nextlhs) {
$newcfp = self::Configlist_add($newrp, 0);
for ($i = $dot + 1; $i < $rp->nrhs; $i++) {
$xsp = $rp->rhs[$i];
if ($xsp->type == LemonSymbol::TERMINAL) {
$newcfp->fws[$xsp->index] = 1;
break;
} elseif ($xsp->type == LemonSymbol::MULTITERMINAL) {
for ($k = 0; $k < $xsp->nsubsym; $k++) {
$newcfp->fws[$xsp->subsym[k]->index] = 1;
}
break;
} else {
$a = array_diff_key($xsp->firstset, $newcfp->fws);
$newcfp->fws += $a;
if ($xsp->lambda === false) {
break;
}
}
}
if ($i == $rp->nrhs) {
LemonPlink::Plink_add($cfp->fplp, $newcfp);
}
}
}
}
}
/**
* Sort the configuration list
*/
static function Configlist_sort()
{
$a = 0;
//self::Configshow(self::$current);
self::$current = Lemon::msort(self::$current,'next', array('LemonConfig', 'Configcmp'));
//self::Configshow(self::$current);
self::$currentend = &$a;
self::$currentend = 0;
}
/**
* Sort the configuration list
*/
static function Configlist_sortbasis()
{
$a = 0;
self::$basis = Lemon::msort(self::$current,'bp', array('LemonConfig', 'Configcmp'));
self::$basisend = &$a;
self::$basisend = 0;
}
/** Return a pointer to the head of the configuration list and
* reset the list
* @return LemonConfig
*/
static function Configlist_return()
{
$old = self::$current;
self::$current = 0;
self::$currentend = &self::$current;
return $old;
}
/** Return a pointer to the head of the basis list and
* reset the list
* @return LemonConfig
*/
static function Configlist_basis()
{
$old = self::$basis;
self::$basis = 0;
self::$basisend = &self::$basis;
return $old;
}
/**
* Free all elements of the given configuration list
* @param LemonConfig
*/
static function Configlist_eat($cfp)
{
for(; $cfp; $cfp = $nextcfp){
$nextcfp = $cfp->next;
if ($cfp->fplp !=0) {
throw new Exception('fplp of configuration non-zero?');
}
if ($cfp->bplp !=0) {
throw new Exception('bplp of configuration non-zero?');
}
if ($cfp->fws) {
$cfp->fws = array();
}
}
}
static function Configcmp($a, $b)
{
$x = $a->rp->index - $b->rp->index;
if (!$x) {
$x = $a->dot - $b->dot;
}
return $x;
}
function ConfigPrint($fp)
{
$rp = $this->rp;
fprintf($fp, "%s ::=", $rp->lhs->name);
for ($i = 0; $i <= $rp->nrhs; $i++) {
if ($i === $this->dot) {
fwrite($fp,' *');
}
if ($i === $rp->nrhs) {
break;
}
$sp = $rp->rhs[$i];
fprintf($fp,' %s', $sp->name);
if ($sp->type == LemonSymbol::MULTITERMINAL) {
for ($j = 1; $j < $sp->nsubsym; $j++) {
fprintf($fp, '|%s', $sp->subsym[$j]->name);
}
}
}
}
/**
* Hash a configuration
*/
private static function confighash(LemonConfig $a)
{
$h = 0;
$h = $h * 571 + $a->rp->index * 37 + $a->dot;
return $h;
}
/**
* Insert a new record into the array. Return TRUE if successful.
* Prior data with the same key is NOT overwritten
*/
static function Configtable_insert(LemonConfig $data)
{
//typedef struct s_x4node {
// struct config *data; /* The data */
// struct s_x4node *next; /* Next entry with the same hash */
// struct s_x4node **from; /* Previous link */
//} x4node;
//
// x4node *np;
// int h;
// int ph;
$h = self::confighash($data);
if (isset(self::$x4a[$h])) {
$np = self::$x4a[$h];
} else {
$np = 0;
}
while ($np) {
if (self::Configcmp($np->data, $data) == 0) {
/* An existing entry with the same key is found. */
/* Fail because overwrite is not allows. */
return 0;
}
$np = $np->next;
}
/* Insert the new data */
$np = array('data' => $data, 'next' => 0, 'from' => 0);
$np = new LemonStateNode;
$np->data = $data;
// as you might notice, "from" always points to itself.
// this bug is in the original lemon parser, but from is never actually accessed
// so it don't much matter now, do it?
if (isset(self::$x4a[$h])) {
self::$x4a[$h]->from = $np->next;
$np->next = self::$x4a[$h];
}
$np->from = $np;
self::$x4a[$h] = $np;
return 1;
}
/**
* Return a pointer to data assigned to the given key. Return NULL
* if no such key.
* @return LemonConfig|0
*/
static function Configtable_find(LemonConfig $key)
{
$h = self::confighash($key);
if (!isset(self::$x4a[$h])) {
return 0;
}
$np = self::$x4a[$h];
while ($np) {
if (self::Configcmp($np->data, $key) == 0) {
break;
}
$np = $np->next;
}
return $np ? $np->data : 0;
}
}
/* Every shift or reduce operation is stored as one of the following */
class LemonAction {
const SHIFT = 1, ACCEPT = 2, REDUCE = 3, ERROR = 4, CONFLICT = 5, SH_RESOLVED = 6,
RD_RESOLVED = 7, NOT_USED = 8;
/**
* @var LemonSymbol
*/
public $sp; /* The look-ahead symbol */
/* enum e_action {
SHIFT,
ACCEPT,
REDUCE,
ERROR,
CONFLICT, /* Was a reduce, but part of a conflict
SH_RESOLVED, /* Was a shift. Precedence resolved conflict
RD_RESOLVED, /* Was reduce. Precedence resolved conflict
NOT_USED /* Deleted by compression
} */
public $type;
/* union {
struct state *stp; /* The new state, if a shift
struct rule *rp; /* The rule, if a reduce
} */
public $x = array('stp' => null, 'rp' => null);
/**
* @var LemonAction
*/
public $next; /* Next action for this state */
/**
* @var LemonAction
*/
public $collide; /* Next action with the same hash */
/* Compare two actions */
static function actioncmp(LemonAction $ap1, LemonAction $ap2)
{
$rc = $ap1->sp->index - $ap2->sp->index;
if ($rc === 0) {
$rc = $ap1->type - $ap2->type;
}
if ($rc === 0) {
if ($ap1->type != LemonAction::REDUCE &&
$ap1->type != LemonAction::RD_RESOLVED &&
$ap1->type != LemonAction::CONFLICT) {
throw new Exception('action has not been processed: ' .
$ap1->sp->name);
}
if ($ap2->type != LemonAction::REDUCE &&
$ap2->type != LemonAction::RD_RESOLVED &&
$ap2->type != LemonAction::CONFLICT) {
throw new Exception('action has not been processed: ' .
$ap2->sp->name);
}
$rc = $ap1->x->index - $ap2->x->index;
}
return $rc;
}
/**
* create linked list of LemonActions
*
* @param LemonAction|null
* @param int one of the constants from LemonAction
* @param LemonSymbol
* @param LemonSymbol|LemonRule
*/
static function Action_add(&$app, $type, LemonSymbol $sp, $arg)
{
$new = new LemonAction;
$new->next = $app;
$app = $new;
$new->type = $type;
$new->sp = $sp;
$new->x = $arg;
}
/* Sort parser actions */
static function Action_sort(LemonAction $ap)
{
$ap = Lemon::msort($ap, 'next', array('LemonAction', 'actioncmp'));
return $ap;
}
/**
* Print an action to the given file descriptor. Return FALSE if
* nothing was actually printed.
*/
function PrintAction($fp, $indent)
{
$result = 1;
switch ($this->type)
{
case self::SHIFT:
fprintf($fp, "%${indent}s shift %d", $this->sp->name, $this->x->statenum);
break;
case self::REDUCE:
fprintf($fp, "%${indent}s reduce %d", $this->sp->name, $this->x->index);
break;
case self::ACCEPT:
fprintf($fp, "%${indent}s accept", $this->sp->name);
break;
case self::ERROR:
fprintf($fp, "%${indent}s error", $this->sp->name);
break;
case self::CONFLICT:
fprintf($fp, "%${indent}s reduce %-3d ** Parsing conflict **", $this->sp->name, $this->x->index);
break;
case self::SH_RESOLVED:
case self::RD_RESOLVED:
case self::NOT_USED:
$result = 0;
break;
}
return $result;
}
}
/* A followset propagation link indicates that the contents of one
** configuration followset should be propagated to another whenever
** the first changes. */
class LemonPlink {
/**
* @var LemonConfig
*/
public $cfp; /* The configuration to which linked */
/**
* @var LemonPlink
*/
public $next = 0; /* The next propagate link */
/**
* Add a plink to a plink list
* @param LemonPlink|null
* @param LemonConfig
*/
static function Plink_add(&$plpp, LemonConfig $cfp)
{
$new = new LemonPlink;
$new->next = $plpp;
$plpp = $new;
$new->cfp = $cfp;
}
/* Transfer every plink on the list "from" to the list "to" */
static function Plink_copy(LemonPlink &$to, LemonPlink $from)
{
while ($from) {
$nextpl = $from->next;
$from->next = $to;
$to = $from;
$from = $nextpl;
}
}
/**
* Delete every plink on the list
* @param LemonPlink|0
*/
static function Plink_delete($plp)
{
while ($plp) {
$nextpl = $plp->next;
$plp->next = 0;
$plp = $nextpl;
}
}
}
/* Each state of the generated parser's finite state machine
** is encoded as an instance of the following structure. */
class LemonState {
/**
* @var LemonConfig
*/
public $bp; /* The basis configurations for this state */
/**
* @var LemonConfig
*/
public $cfp; /* All configurations in this set */
public $statenum; /* Sequencial number for this state */
/**
* @var LemonAction
*/
public $ap; /* Array of actions for this state */
public $nTknAct, $nNtAct; /* Number of actions on terminals and nonterminals */
public $iTknOfst, $iNtOfst; /* yy_action[] offset for terminals and nonterms */
public $iDflt; /* Default action */
public static $x3a = array();
public static $states = array();
/**
* Compare two states for sorting purposes. The smaller state is the
* one with the most non-terminal actions. If they have the same number
* of non-terminal actions, then the smaller is the one with the most
* token actions.
*/
static function stateResortCompare($a, $b)
{
$n = $b->nNtAct - $a->nNtAct;
if ($n === 0) {
$n = $b->nTknAct - $a->nTknAct;
}