forked from gene-sis/lime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lime.php
executable file
·1406 lines (1155 loc) · 31.7 KB
/
lime.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
#!/usr/bin/php -q
<?php
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
namespace Genesis\Lime;
define('LIME_CALL_PROTOCOL', '$tokens, &$result');
function emit($str) {
fputs(STDERR, $str . PHP_EOL);
}
class Bug extends \Exception {
}
function bug($gripe = 'Bug found.') {
throw new Bug($gripe);
}
function bug_if($fallacy, $gripe = 'Bug found.') {
if ($fallacy) {
throw new Bug($gripe);
}
}
function bug_unless($assertion, $gripe = 'Bug found.') {
if (!$assertion) {
throw new Bug($gripe);
}
}
require __DIR__ . '/src/LimeParser.php';
require __DIR__ . '/src/ParseBug.php';
require __DIR__ . '/src/ParseEngine.php';
require __DIR__ . '/src/ParseError.php';
require __DIR__ . '/src/ParsePrematureEof.php';
require __DIR__ . '/src/ParseStack.php';
require __DIR__ . '/src/ParseUnexpectedToken.php';
require __DIR__ . '/Set.php';
require __DIR__ . '/FlexScanner.php';
function lime_token_reference($pos) {
return '$tokens[' . $pos . ']';
}
function lime_token_reference_callback($foo) {
if ($foo[1] === '$') {
// always
return '$result';
} elseif (!ctype_digit($foo[1])) {
return '$' . $foo[1];
}
return lime_token_reference($foo[1] - 1);
}
function lime_export($var) {
if (is_array($var)) {
$i = is_indexed($var);
$out = array();
foreach($var as $k => $v) {
$out[] = (!$i ? lime_export($k).' => ' : '') . lime_export($v);
}
$result = 'array(' . PHP_EOL . preg_replace('~^~m', INDENT, implode(',' . PHP_EOL, $out)) . PHP_EOL . ')';
} elseif (is_int($var) || is_float($var)) {
$result = (string)$var;
} elseif (is_string($var)) {
$opt1 = '\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $var) . '\'';
$opt2 = $opt1;
if (strpos($var, '$') === false) {
$opt2 = '"' . str_replace(array('\\', '"'), array('\\\\', '\"'), $var) . '"';
}
if (strlen($opt1) <= strlen($opt2)) {
$result = $opt1;
} else {
$result = $opt2;
}
} elseif (is_bool($var)) {
$result = $var ? 'true' : 'false';
} else {
bug('Wrong type: ' . gettype($var));
}
return $result;
}
function is_indexed(array $array) {
$i = 0;
foreach($array as $k => $v) {
if ($k !== $i++) {
return false;
}
}
return true;
}
function unindent($text) {
if (preg_match('{\A[\r\n]*([ \t]+)[^\r\n]*+(?:[\r\n]++(?>\1[^\r\n]*+(?:[\r\n]+|\z)|[\r\n]+)+)?\z}', rtrim($text), $match)) {
$text = preg_replace('{^' . $match[1] . '}m', '', $text);
}
return $text;
}
class cf_action {
protected $code;
public function __construct($code) {
$this->code = $code;
}
}
/**
* Base class for parse table instructions. The main idea is to make the
* subclasses responsible for conflict resolution among themselves. It also
* forms a sort of interface to the parse table.
*/
abstract class step {
public $sym;
public function __construct(sym $sym) {
$this->sym = $sym;
}
public function glyph() {
return $this->sym->name;
}
public function sane() {
return true;
}
abstract public function instruction();
abstract public function decide($that);
}
class error extends step {
public function sane() {
return false;
}
public function instruction() {
bug('This should not happen.');
}
public function decide($that) {
// An error shall remain one
return array($this);
}
}
class shift extends step {
public $q;
public $rule;
public function __construct(sym $sym, $q, rule $rule) {
parent::__construct($sym);
$this->q = $q;
$this->rule = $rule;
}
public function instruction() {
return 's ' . $this->q;
}
public function decide($that) {
// shift-shift conflicts are impossible.
// shift-accept conflicts are a bug.
// so we can infer:
bug_unless($that instanceof reduce);
// That being said, the resolution is a matter of precedence.
$shift_prec = $this->rule->right_prec;
$reduce_prec = $that->rule->left_prec;
// If we don't have defined precedence levels for both options,
// then we default to shifting:
if (!($shift_prec and $reduce_prec)) {
return array($this, 1);
}
// Otherwise, use the step with higher precedence.
if ($shift_prec > $reduce_prec) {
return array($this, 0);
}
if ($reduce_prec > $shift_prec) {
return array($that, 0);
}
// The "nonassoc" works by giving equal precedence to both options,
// which means to put an error instruction in the parse table.
return array(new error($this->sym), 0);
}
}
class reduce extends step {
public function __construct($sym, rule $rule) {
parent::__construct($sym);
$this->rule = $rule;
}
public function instruction() {
return 'r ' . $this->rule->id;
}
function decide($that) {
// This means that the input grammar has a reduce-reduce conflict.
// Such things are considered an error in the input.
throw new RRC($this, $that);
// BISON would go with the first encountered reduce thus:
// return $this;
}
}
class accept extends step {
public function __construct(sym $sym) {
parent::__construct($sym);
}
public function instruction() {
return 'a ' . $this->sym->name;
}
public function decide($that) {
return array($this, 0);
}
}
class RRC extends \Exception {
public function __construct($a, $b) {
parent::__construct('Reduce-Reduce Conflict');
$this->a = $a;
$this->b = $b;
}
function make_noise() {
emit(sprintf(
'Reduce-Reduce Conflict:' . PHP_EOL . '%s' . PHP_EOL . '%s' . PHP_EOL . 'Lookahead is (%s)',
$this->a->rule->text(),
$this->b->rule->text(),
$this->a->glyph()
));
}
}
class state {
public $id;
public $key;
public $close;
public $conflicts = 0;
public $action = array();
public function __construct($id, $key, $close) {
$this->id = $id;
$this->key = $key;
$this->close = $close; // config key -> object
ksort($this->close);
}
public function dump() {
echo ' * ' . $this->id . ' / ' . $this->key . PHP_EOL;
foreach ($this->close as $config) {
$config->dump();
}
}
public function add_shift(sym $sym, $state, $rule) {
$this->add_instruction(new shift($sym, $state->id, $rule));
}
public function add_reduce(sym $sym, $rule) {
$this->add_instruction(new reduce($sym, $rule));
}
public function add_accept(sym $sym) {
$this->add_instruction(new accept($sym));
}
public function add_instruction(step $step) {
$this->action[] = $step;
}
function find_reductions($lime) {
// rightmost configurations followset yields reduce.
foreach($this->close as $c) {
if ($c->rightmost) {
foreach ($c->follow->all() as $glyph) {
$this->add_reduce($lime->sym($glyph), $c->rule);
}
}
}
}
function resolve_conflicts() {
// For each possible lookahead, find one (and only one) step to take.
$table = array();
foreach ($this->action as $step) {
$glyph = $step->glyph();
if (isset($table[$glyph])) {
// There's a conflict. The shifts all came first, which
// simplifies the coding for the step->decide() methods.
try {
list($table[$glyph], $conflict) = $table[$glyph]->decide($step);
$this->conflicts += $conflict;
} catch (RRC $e) {
emit('State ' . $this->id . ':');
$e->make_noise();
}
} else {
// This glyph is yet unprocessed, so the step at hand is
// our best current guess at what the grammar indicates.
$table[$glyph] = $step;
}
}
// Now that we have the correct steps chosen, this routine is oddly
// also responsible for turning that table into the form that will
// eventually be passed to the parse engine. (So FIXME?)
$out = array();
foreach ($table as $glyph => $step) {
if ($step->sane()) {
$out[$glyph] = $step->instruction();
}
}
return $out;
}
function segment_config() {
// Filter $this->close into categories based on the symbol_after_the_dot.
$f = array();
foreach ($this->close as $c) {
if (($p = $c->symbol_after_the_dot)) {
$f[$p->name][] = $c;
}
}
return $f;
}
}
class sym {
public function __construct($name, $id) {
$this->name = $name;
$this->id = $id;
$this->term = true; // Until proven otherwise.
$this->rule = array();
$this->config = array();
$this->lambda = false;
$this->first = new Set();
$this->left_prec = $this->right_prec = 0;
}
public function summary() {
$out = '';
foreach ($this->rule as $rule) {
$out .= $rule->text() . PHP_EOL;
}
return $out;
}
}
class rule {
public function __construct($id, $sym, $rhs, $code, $look, $replace, $prec_sym) {
bug_unless(is_int($look));
$this->id = $id;
$this->sym = $sym;
$this->rhs = $rhs;
$this->code = $code;
$this->look = $look;
$this->replace = $replace;
$this->prec_sym = $prec_sym;
$this->left_prec = 0;
$this->right_prec = 0;
$this->first = array();
$this->epsilon = count($rhs);
}
public function lhs_glyph() {
return $this->sym->name;
}
public function determine_precedence() {
// We may eventually expand to allow explicit prec_symbol declarations.
// Until then, we'll go with the rightmost terminal, which is what
// BISON does. People probably expect that. The leftmost terminal
// is a reasonable alternative behaviour, but I don't see the big
// deal just now.
$prec_sym = $this->prec_sym;
if (!$prec_sym) {
$prec_sym = $this->rightmost_terminal();
}
if (!$prec_sym) {
return;
}
$this->left_prec = $prec_sym->left_prec;
$this->right_prec = $prec_sym->right_prec;
}
private function rightmost_terminal() {
$symbol = null;
$rhs = $this->rhs;
while ($rhs) {
$symbol = array_pop($rhs);
if ($symbol->term) {
break;
}
}
return $symbol;
}
public function text() {
$t = '(' . $this->id . ') ' . $this->lhs_glyph() . ' :=';
foreach($this->rhs as $s) {
$t .= ' ' . $s->name;
}
if (!$this->rhs) {
$t .= ' ε';
}
return $t;
}
public function table(lime_language $lang) {
return array(
'symbol' => $this->lhs_glyph(),
'len' => $this->look,
'replace' => $this->replace,
'code' => $lang->fixup($this->code),
'text' => $this->text(),
);
}
public function lambda() {
foreach ($this->rhs as $sym) {
if (!$sym->lambda) {
return false;
}
}
return true;
}
public function find_first() {
$dot = count($this->rhs);
$last = $this->first[$dot] = new Set();
while ($dot--) {
$symbol_after_the_dot = $this->rhs[$dot];
$first = $symbol_after_the_dot->first->all();
bug_if(empty($first) and !$symbol_after_the_dot->lambda and $symbol_after_the_dot->name != 'error');
$set = new Set($first);
if ($symbol_after_the_dot->lambda) {
$set->union($last);
if ($this->epsilon == $dot + 1) {
$this->epsilon = $dot;
}
}
$last = $this->first[$dot] = $set;
}
}
public function teach_symbol_of_first_set() {
$go = false;
foreach ($this->rhs as $sym) {
if ($this->sym->first->union($sym->first)) {
$go = true;
}
if (!$sym->lambda) {
break;
}
}
return $go;
}
public function lambda_from($dot) {
return $this->epsilon <= $dot;
}
public function leftmost($follow) {
return new config($this, 0, $follow);
}
public function dotted_text($dot) {
$out = $this->lhs_glyph() . ' :=';
$idx = -1;
foreach($this->rhs as $idx => $s) {
if ($idx == $dot) {
$out .= ' .';
}
$out .= ' ' . $s->name;
}
if (!$this->rhs) {
$out .= ' ε';
}
if ($dot > $idx) {
$out .= ' .';
}
return $out;
}
}
class config {
public function __construct($rule, $dot, $follow) {
$this->rule = $rule;
$this->dot = $dot;
$this->key = $rule->id . '.' . $dot;
$this->rightmost = count($rule->rhs) <= $dot;
$this->symbol_after_the_dot = $this->rightmost ? null : $rule->rhs[$dot];
$this->_blink = array();
$this->follow = new Set($follow);
$this->_flink = array();
bug_unless($this->rightmost or @count($rule));
}
public function text() {
return $this->rule->dotted_text($this->dot)
. ' [ ' . implode(' ', $this->follow->all()) . ' ]';
}
public function blink($config) {
$this->_blink[] = $config;
}
public function next() {
bug_if($this->rightmost);
$c = new config($this->rule, $this->dot+1, array());
// Anything in the follow set for this config will also be in the next.
// However, we link it backwards because we might wind up selecting a
// pre-existing state, and the housekeeping is easier in the first half
// of the program. We'll fix it before doing the propagation.
$c->blink($this);
return $c;
}
public function copy_links_from($that) {
foreach($that->_blink as $c) {
$this->blink($c);
}
}
public function lambda() {
return $this->rule->lambda_from($this->dot);
}
public function simple_follow() {
return $this->rule->first[$this->dot + 1]->all();
}
public function epsilon_follows() {
return $this->rule->lambda_from($this->dot + 1);
}
public function fixlinks() {
foreach ($this->_blink as $that) {
$that->_flink[] = $this;
}
$this->blink = array();
}
public function dump() {
echo ' * ';
echo $this->key . ' : ';
echo $this->rule->dotted_text($this->dot);
echo $this->follow->text();
foreach ($this->_flink as $c) {
echo $c->key . ' / ';
}
echo PHP_EOL;
}
}
class lime {
public $parser_class = 'parser';
public $parser_namespace = null;
public $descr = array();
public function __construct() {
$this->p_next = 1;
$this->sym = array();
$this->rule = array();
$this->start_symbol_set = array();
$this->state = array();
$this->stop = $this->sym('#');
$this->expect_conflicts = null;
$this->conflicts = 0;
if ($err = $this->sym('error')) {
$err->term = false;
}
$this->lang = new lime_language_php();
}
function language() {
return $this->lang;
}
function build_parser() {
$this->add_start_rule();
foreach ($this->rule as $r) {
$r->determine_precedence();
}
$this->find_sym_lamdba();
$this->find_sym_first();
foreach ($this->rule as $rule) {
$rule->find_first();
}
$initial = $this->find_states();
$this->fixlinks();
// $this->dump_configurations();
$this->find_follow_sets();
foreach($this->state as $s) {
$s->find_reductions($this);
}
$i = $this->resolve_conflicts();
$a = $this->rule_table();
$qi = $initial->id;
$d = $this->descr;
if ($this->expect_conflicts !== null && $this->expect_conflicts != $this->conflicts) {
throw new Bug($this->expect_conflicts .' conflicts expected, got ' . $this->conflicts);
}
return $this->lang->ptab_to_class($this->parser_class, $this->parser_namespace, compact('a', 'qi', 'i', 'd'));
}
function rule_table() {
$s = array();
foreach ($this->rule as $i => $r) {
$s[$i] = $r->table($this->lang);
}
return $s;
}
function add_rule($symbol, $rhs, $code) {
$this->add_raw_rule($symbol, $rhs, $code, count($rhs), true);
}
function trump_up_bogus_lhs($real) {
return "'{$real}'" . count($this->rule);
}
function add_raw_rule($lhs, $rhs, $code, $look, $replace, $prec_sym) {
$sym = $this->sym($lhs);
$sym->term = false;
if (!$rhs) {
$sym->lambda = true;
}
$rs = array();
foreach ($rhs as $str) {
$rs[] = $this->sym($str);
}
$rid = count($this->rule);
$r = new rule($rid, $sym, $rs, $code, $look, $replace, $prec_sym);
$this->rule[$rid] = $r;
$sym->rule[] = $r;
}
function sym($str, $description = null) {
if (!isset($this->sym[$str])) {
$this->sym[$str] = new sym($str, count($this->sym));
}
if ($description) {
$this->descr[$str] = $description;
}
return $this->sym[$str];
}
function summary() {
$out = '';
foreach ($this->sym as $sym) {
if (!$sym->term) {
$out .= $sym->summary();
}
}
return $out;
}
private function find_sym_lamdba() {
do {
$go = false;
foreach ($this->sym as $sym) {
if (!$sym->lambda) {
foreach ($sym->rule as $rule) {
if ($rule->lambda()) {
$go = true;
$sym->lambda = true;
}
}
}
}
} while ($go);
}
private function teach_terminals_first_set() {
foreach ($this->sym as $sym) {
if ($sym->term) {
$sym->first->add($sym->name);
}
}
}
private function find_sym_first() {
$this->teach_terminals_first_set();
do {
$go = false;
foreach ($this->rule as $r) {
if ($r->teach_symbol_of_first_set()) {
$go = true;
}
}
} while ($go);
}
function add_start_rule() {
$rewrite = new lime_rewrite("'start'");
$rhs = new lime_rhs();
$rhs->add(new lime_glyph($this->deduce_start_symbol()->name, null));
$rewrite->add_rhs($rhs);
$rewrite->update($this);
}
protected function deduce_start_symbol() {
$candidate = current($this->start_symbol_set);
// Did the person try to set a start symbol at all?
if (!is_object($candidate)) {
return $this->first_rule_lhs();
}
// Do we actually have such a symbol on the left of a rule?
if ($candidate->terminal) {
return $this->first_rule_lhs();
}
// Ok, it's a decent choice. We need to return the symbol entry.
return $this->sym($candidate);
}
private function first_rule_lhs() {
reset($this->rule);
$r = current($this->rule);
return $r->sym;
}
/**
* Build an initial state. This is a recursive process which digs out
* the LR(0) state graph.
*/
function find_states() {
$start_glyph = "'start'";
$sym = $this->sym($start_glyph);
$basis = array();
foreach($sym->rule as $rule) {
$c = $rule->leftmost(array('#'));
$basis[$c->key] = $c;
}
$initial = $this->get_state($basis);
$initial->add_accept($sym);
return $initial;
}
function get_state($basis) {
$key = array_keys($basis);
sort($key);
$key = implode(' ', $key);
if (isset($this->state[$key])) {
// Copy all the links around...
$state = $this->state[$key];
foreach($basis as $config) {
$state->close[$config->key]->copy_links_from($config);
}
return $state;
} else {
$close = $this->state_closure($basis);
$this->state[$key] = $state = new state(count($this->state), $key, $close);
$this->build_shifts($state);
return $state;
}
}
private function state_closure($q) {
// $q is a list of config.
$close = array();
while ($config = array_pop($q)) {
if (isset($close[$config->key])) {
$close[$config->key]->copy_links_from($config);
$close[$config->key]->follow->union($config->follow);
continue;
}
$close[$config->key] = $config;
$symbol_after_the_dot = $config->symbol_after_the_dot;
if (!$symbol_after_the_dot) {
continue;
}
if (!$symbol_after_the_dot->term) {
foreach ($symbol_after_the_dot->rule as $r) {
$station = $r->leftmost($config->simple_follow());
if ($config->epsilon_follows()) {
$station->blink($config);
}
$q[] = $station;
}
}
}
return $close;
}
function build_shifts($state) {
foreach ($state->segment_config() as $glyph => $segment) {
$basis = array();
foreach ($segment as $preshift) {
$postshift = $preshift->next();
$basis[$postshift->key] = $postshift;
}
$dest = $this->get_state($basis);
$state->add_shift($this->sym($glyph), $dest, $segment[0]->rule);
}
}
function fixlinks() {
foreach ($this->state as $s) {
foreach ($s->close as $c) {
$c->fixlinks();
}
}
}
function find_follow_sets() {
$q = array();
foreach ($this->state as $s) {
foreach ($s->close as $c) {
$q[] = $c;
}
}
while ($q) {
$c = array_shift($q);
foreach ($c->_flink as $d) {
if ($d->follow->union($c->follow)) {
$q[] = $d;
}
}
}
}
private function set_assoc($ss, $l, $r) {
$p = ($this->p_next++) * 2;
foreach ($ss as $glyph) {
$s = $this->sym($glyph);
$s->left_prec = $p + $l;
$s->right_prec = $p + $r;
}
}
function left_assoc($ss) {
$this->set_assoc($ss, 1, 0);
}
function right_assoc($ss) {
$this->set_assoc($ss, 0, 1);
}
function non_assoc($ss) {
$this->set_assoc($ss, 0, 0);
}
private function resolve_conflicts() {
// For each state, try to find one and only one
// thing to do for any given lookahead.
$i = array();
foreach ($this->state as $s) {
$i[$s->id] = $s->resolve_conflicts();
$this->conflicts += $s->conflicts;
}
return $i;
}
function dump_configurations() {
foreach ($this->state as $q) {
$q->dump();
}
}
function dump_first_sets() {
foreach ($this->sym as $s) {
echo ' * ';
echo $s->name . ' : ';
echo $s->first->text();
echo PHP_EOL;
}
}
function add_rule_with_actions($lhs, $rhs) {
// First, make sure this thing is well-formed.
if(!is_object(end($rhs))) {
$rhs[] = new cf_action('');
}
// Now, split it into chunks based on the actions.
$look = -1;
$subrule = array();
$subsymbol = '';
while ($rhs) {
$it = array_shift($rhs);
++$look;
if (is_string($it)) {
$subrule[] = $it;
} else {
$code = $it->code;
// It's an action.
// Is it the last one?
if ($rhs) {
// no.