diff --git a/Lempar Original.php b/Lempar Original.php index 505e3c2..0a6c96f 100644 --- a/Lempar Original.php +++ b/Lempar Original.php @@ -706,7 +706,6 @@ public function yy_reduce($yyruleno) //mixed $yygotominor; /* The LHS of the rule reduced */ //ParseyyStackEntry $yymsp; /* The top of the parser's stack */ //int $yysize; /* Amount to pop the stack */ - $yymsp = $this->yystack[$this->yyidx]; if (self::$yyTraceFILE && $yyruleno >= 0 && $yyruleno < count(self::$yyRuleName)) { fprintf(self::$yyTraceFILE, "%sReduce (%d) [%s].\n", @@ -715,7 +714,7 @@ public function yy_reduce($yyruleno) } $this->_retvalue = $yy_lefthand_side = null; - if (array_key_exists($yyruleno, self::$yyReduceMap)) { + if (isset(self::$yyReduceMap[$yyruleno])) { // call the action $this->_retvalue = null; $this->{'yy_r' . self::$yyReduceMap[$yyruleno]}(); diff --git a/Lempar.php b/Lempar.php index 557dcc5..9659088 100644 --- a/Lempar.php +++ b/Lempar.php @@ -139,7 +139,7 @@ public static function yy_destructor($yymajor, $yypminor) public function yy_pop_parser_stack() { - if (!count($this->yystack)) { + if (empty($this->yystack)) { return; } $yytos = array_pop($this->yystack); @@ -167,10 +167,18 @@ public function __destruct() public function yy_get_expected_tokens($token) { + static $res3 = array(); + static $res4 = array(); $state = $this->yystack[$this->yyidx]->stateno; $expected = self::$yyExpectedTokens[$state]; - if (in_array($token, self::$yyExpectedTokens[$state], true)) { - return $expected; + if (isset($res3[$state][$token])) { + if ($res3[$state][$token]) { + return $expected; + } + } else { + if ($res3[$state][$token] = in_array($token, self::$yyExpectedTokens[$state], true)) { + return $expected; + } } $stack = $this->yystack; $yyidx = $this->yyidx; @@ -194,12 +202,18 @@ public function yy_get_expected_tokens($token) self::$yyRuleInfo[$yyruleno]['lhs']); if (isset(self::$yyExpectedTokens[$nextstate])) { $expected = array_merge($expected, self::$yyExpectedTokens[$nextstate]); - if (in_array($token, - self::$yyExpectedTokens[$nextstate], true)) { - $this->yyidx = $yyidx; - $this->yystack = $stack; - - return array_unique($expected); + if (isset($res4[$nextstate][$token])) { + if ($res4[$nextstate][$token]) { + $this->yyidx = $yyidx; + $this->yystack = $stack; + return array_unique($expected); + } + } else { + if ($res4[$nextstate][$token] = in_array($token, self::$yyExpectedTokens[$nextstate], true)) { + $this->yyidx = $yyidx; + $this->yystack = $stack; + return array_unique($expected); + } } } if ($nextstate < self::YYNSTATE) { @@ -237,13 +251,21 @@ public function yy_get_expected_tokens($token) public function yy_is_expected_token($token) { + static $res = array(); + static $res2 = array(); if ($token === 0) { return true; // 0 is not part of this } $state = $this->yystack[$this->yyidx]->stateno; - if (in_array($token, self::$yyExpectedTokens[$state], true)) { - return true; - } + if (isset($res[$state][$token])) { + if ($res[$state][$token]) { + return true; + } + } else { + if ($res[$state][$token] = in_array($token, self::$yyExpectedTokens[$state], true)) { + return true; + } + } $stack = $this->yystack; $yyidx = $this->yyidx; do { @@ -264,12 +286,18 @@ public function yy_is_expected_token($token) $nextstate = $this->yy_find_reduce_action( $this->yystack[$this->yyidx]->stateno, self::$yyRuleInfo[$yyruleno]['lhs']); - if (isset(self::$yyExpectedTokens[$nextstate]) && - in_array($token, self::$yyExpectedTokens[$nextstate], true)) { - $this->yyidx = $yyidx; - $this->yystack = $stack; - - return true; + if (isset($res2[$nextstate][$token])) { + if ($res2[$nextstate][$token]) { + $this->yyidx = $yyidx; + $this->yystack = $stack; + return true; + } + } else { + if ($res2[$nextstate][$token] = (isset(self::$yyExpectedTokens[$nextstate]) && in_array($token, self::$yyExpectedTokens[$nextstate], true))) { + $this->yyidx = $yyidx; + $this->yystack = $stack; + return true; + } } if ($nextstate < self::YYNSTATE) { // we need to shift a non-terminal @@ -386,7 +414,7 @@ public function yy_shift($yyNewState, $yyMajor, $yypMinor) $yytos->stateno = $yyNewState; $yytos->major = $yyMajor; $yytos->minor = $yypMinor; - array_push($this->yystack, $yytos); + $this->yystack[] = $yytos; if ($this->yyTraceFILE && $this->yyidx > 0) { fprintf($this->yyTraceFILE, "%sShift %d\n", $this->yyTracePrompt, $yyNewState); @@ -412,7 +440,6 @@ public function yy_shift($yyNewState, $yyMajor, $yypMinor) public function yy_reduce($yyruleno) { - $yymsp = $this->yystack[$this->yyidx]; if ($this->yyTraceFILE && $yyruleno >= 0 && $yyruleno < count(self::$yyRuleName)) { fprintf($this->yyTraceFILE, "%sReduce (%d) [%s].\n", @@ -421,7 +448,7 @@ public function yy_reduce($yyruleno) } $this->_retvalue = $yy_lefthand_side = null; - if (array_key_exists($yyruleno, self::$yyReduceMap)) { + if (isset(self::$yyReduceMap[$yyruleno])) { // call the action $this->_retvalue = null; $this->{'yy_r' . self::$yyReduceMap[$yyruleno]}(); @@ -487,7 +514,7 @@ public function doParse($yymajor, $yytokenvalue) $x->stateno = 0; $x->major = 0; $this->yystack = array(); - array_push($this->yystack, $x); + $this->yystack[] = $x; } $yyendofinput = ($yymajor==0); diff --git a/LexerGenerator/Parser.php b/LexerGenerator/Parser.php index c2ca75c..73aa120 100644 --- a/LexerGenerator/Parser.php +++ b/LexerGenerator/Parser.php @@ -343,36 +343,29 @@ public function doFirstMatch($rules, $statename, $ruleindex) $pattern .= implode('|', $patterns); $pattern .= '/' . $this->patternFlags; fwrite($this->out, ' - $tokenMap = ' . $tokenindex . '; + if (!isset($this->yy_global_pattern' . $ruleindex . ')) { + $this->yy_global_pattern' . $ruleindex . ' = "' . $pattern . 'iS"; + } if (' . $this->counter . ' >= strlen(' . $this->input . ')) { return false; // end of input } '); - fwrite($this->out, '$yy_global_pattern = "' . - $pattern . 'iS";' . "\n"); fwrite($this->out, ' do { - if (preg_match($yy_global_pattern,' . $this->input . ', $yymatches, null, ' . + if (preg_match($this->yy_global_pattern' . $ruleindex . ',' . $this->input . ', $yymatches, null, ' . $this->counter . ')) { $yysubmatches = $yymatches; $yymatches = preg_grep("/(.|\s)+/", $yysubmatches); - if (!count($yymatches)) { + if (empty($yymatches)) { throw new Exception(\'Error: lexing failed because a rule matched\' . \' an empty string. Input "\' . substr(' . $this->input . ', ' . $this->counter . ', 5) . \'... state ' . $statename . '\'); } next($yymatches); // skip global match ' . $this->token . ' = key($yymatches); // token number - if ($tokenMap[' . $this->token . ']) { - // extract sub-patterns for passing to lex function - $yysubmatches = array_slice($yysubmatches, ' . $this->token . ' + 1, - $tokenMap[' . $this->token . ']); - } else { - $yysubmatches = array(); - } ' . $this->value . ' = current($yymatches); // token value - $r = $this->{\'yy_r' . $ruleindex . '_\' . ' . $this->token . '}($yysubmatches); + $r = $this->{\'yy_r' . $ruleindex . '_\' . ' . $this->token . '}(); if ($r === null) { ' . $this->counter . ' += strlen(' . $this->value . '); ' . $this->line . ' += substr_count(' . $this->value . ', "\n"); @@ -496,7 +489,7 @@ public function yylex' . $this -> _outRuleIndex . '() '); } foreach ($rules as $i => $rule) { - fwrite($this->out, ' function yy_r' . $this -> _outRuleIndex . '_' . $ruleMap[$i] . '($yy_subpatterns) + fwrite($this->out, ' function yy_r' . $this -> _outRuleIndex . '_' . $ruleMap[$i] . '() { ' . $rule['code'] . ' } @@ -917,7 +910,7 @@ public static function yy_destructor($yymajor, $yypminor) */ public function yy_pop_parser_stack() { - if (!count($this->yystack)) { + if (empty($this->yystack)) { return; } $yytos = array_pop($this->yystack); @@ -1868,7 +1861,6 @@ public function yy_reduce($yyruleno) //mixed $yygotominor; /* The LHS of the rule reduced */ //PHP_LexerGenerator_ParseryyStackEntry $yymsp; /* The top of the parser's stack */ //int $yysize; /* Amount to pop the stack */ - $yymsp = $this->yystack[$this->yyidx]; if ($this->yyTraceFILE && $yyruleno >= 0 && $yyruleno < count(self::$yyRuleName)) { fprintf($this->yyTraceFILE, "%sReduce (%d) [%s].\n", @@ -1877,7 +1869,7 @@ public function yy_reduce($yyruleno) } $this->_retvalue = $yy_lefthand_side = null; - if (array_key_exists($yyruleno, self::$yyReduceMap)) { + if (isset(self::$yyReduceMap[$yyruleno])) { // call the action $this->_retvalue = null; $this->{'yy_r' . self::$yyReduceMap[$yyruleno]}(); diff --git a/LexerGenerator/ParserOriginal.php b/LexerGenerator/ParserOriginal.php index 8b92ad7..635f13a 100644 --- a/LexerGenerator/ParserOriginal.php +++ b/LexerGenerator/ParserOriginal.php @@ -1742,7 +1742,6 @@ public function yy_reduce($yyruleno) //mixed $yygotominor; /* The LHS of the rule reduced */ //PHP_LexerGenerator_ParseryyStackEntry $yymsp; /* The top of the parser's stack */ //int $yysize; /* Amount to pop the stack */ - $yymsp = $this->yystack[$this->yyidx]; if (self::$yyTraceFILE && $yyruleno >= 0 && $yyruleno < count(self::$yyRuleName)) { fprintf(self::$yyTraceFILE, "%sReduce (%d) [%s].\n", @@ -1751,7 +1750,7 @@ public function yy_reduce($yyruleno) } $this->_retvalue = $yy_lefthand_side = null; - if (array_key_exists($yyruleno, self::$yyReduceMap)) { + if (isset(self::$yyReduceMap[$yyruleno])) { // call the action $this->_retvalue = null; $this->{'yy_r' . self::$yyReduceMap[$yyruleno]}(); diff --git a/LexerGenerator/Regex/Parser.php b/LexerGenerator/Regex/Parser.php index de81af1..459f1eb 100644 --- a/LexerGenerator/Regex/Parser.php +++ b/LexerGenerator/Regex/Parser.php @@ -1760,7 +1760,6 @@ public function yy_reduce($yyruleno) //mixed $yygotominor; /* The LHS of the rule reduced */ //PHP_LexerGenerator_Regex_yyStackEntry $yymsp; /* The top of the parser's stack */ //int $yysize; /* Amount to pop the stack */ - $yymsp = $this->yystack[$this->yyidx]; if (self::$yyTraceFILE && $yyruleno >= 0 && $yyruleno < count(self::$yyRuleName)) { fprintf(self::$yyTraceFILE, "%sReduce (%d) [%s].\n", @@ -1769,7 +1768,7 @@ public function yy_reduce($yyruleno) } $this->_retvalue = $yy_lefthand_side = null; - if (array_key_exists($yyruleno, self::$yyReduceMap)) { + if (isset(self::$yyReduceMap[$yyruleno])) { // call the action $this->_retvalue = null; $this->{'yy_r' . self::$yyReduceMap[$yyruleno]}(); diff --git a/ParserGenerator/Data.php b/ParserGenerator/Data.php index edb8f6b..211917f 100644 --- a/ParserGenerator/Data.php +++ b/ParserGenerator/Data.php @@ -1484,7 +1484,7 @@ public function ReportTable($mhflag) throw new Exception('rp->index != i and should be'); } // change next line - fprintf($out, " /* %3d */ \"%s ::=", $i, $rp->lhs->name); + fprintf($out, " /* %3d */ '%s ::=", $i, $rp->lhs->name); for ($j = 0; $j < $rp->nrhs; $j++) { $sp = $rp->rhs[$j]; fwrite($out,' ' . $sp->name); @@ -1494,7 +1494,7 @@ public function ReportTable($mhflag) } } } - fwrite($out, "\",\n"); + fwrite($out, "',\n"); $lineno++; } $this->tplt_xfer($this->name, $in, $out, $lineno);