From ab902b4ac67f62bceeab43da51dc1ac1229b38bd Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Sat, 20 Apr 2013 14:58:25 +0400 Subject: [PATCH] Compress reduce rules --- lime.php | 16 ++++++++++++++++ parse_engine.php | 12 +++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lime.php b/lime.php index 603fe3b..bd5a5cc 100755 --- a/lime.php +++ b/lime.php @@ -666,9 +666,25 @@ class lime { throw new Bug($this->expect_conflicts .' conflicts expected, got ' . $this->conflicts); } + $this->compress($i); + return $this->lang->ptab_to_class($this->parser_class, compact('a', 'qi', 'i', 'd')); } + function compress(&$i) { + foreach ($i as &$inst) { + $act = reset($inst); + if (substr($act, 0, 2) == 'r ') { + foreach ($inst as $k => $a) { + if ($a !== $act) { + continue(2); + } + } + $inst = array('' => $act); + } + } + } + function rule_table() { $s = array(); diff --git a/parse_engine.php b/parse_engine.php index 1ca6b3c..1bad776 100644 --- a/parse_engine.php +++ b/parse_engine.php @@ -263,11 +263,13 @@ class parse_engine { private function step_for($type) { $row = $this->current_row(); - if (!isset($row[$type])) { - return array('e', $this->stack->q); + if (isset($row[$type])) { + return explode(' ', $row[$type]); } - - return explode(' ', $row[$type]); + if (isset($row[''])) { + return explode(' ', $row['']); + } + return array('e', $this->stack->q); } private function get_steps() { @@ -281,7 +283,7 @@ class parse_engine { list($opcode) = explode(' ', $row, 2); if ($opcode != 'e') { - $out[] = $type; + $out[] = $type === '' ? '$default' : $type; } }