Skip to content

Commit

Permalink
optimization for higher lexer/parser speed
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed May 16, 2015
1 parent 7c6caba commit 65b3429
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 47 deletions.
3 changes: 1 addition & 2 deletions Lempar Original.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]}();
Expand Down
71 changes: 49 additions & 22 deletions Lempar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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",
Expand All @@ -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]}();
Expand Down Expand Up @@ -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);

Expand Down
26 changes: 9 additions & 17 deletions LexerGenerator/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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'] .
' }
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand All @@ -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]}();
Expand Down
3 changes: 1 addition & 2 deletions LexerGenerator/ParserOriginal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]}();
Expand Down
3 changes: 1 addition & 2 deletions LexerGenerator/Regex/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]}();
Expand Down
4 changes: 2 additions & 2 deletions ParserGenerator/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -1494,7 +1494,7 @@ public function ReportTable($mhflag)
}
}
}
fwrite($out, "\",\n");
fwrite($out, "',\n");
$lineno++;
}
$this->tplt_xfer($this->name, $in, $out, $lineno);
Expand Down

0 comments on commit 65b3429

Please sign in to comment.