本文整理汇总了PHP中Stack::pushTerminal方法的典型用法代码示例。如果您正苦于以下问题:PHP Stack::pushTerminal方法的具体用法?PHP Stack::pushTerminal怎么用?PHP Stack::pushTerminal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack::pushTerminal方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
public function run()
{
$this->variables = [];
$token = $this->scanner->next(TRUE);
$stack = new Stack();
$stack->push('$');
do {
$a = $stack->topTerminal();
try {
switch ($this->getFromTable($a, $token)) {
case '=':
debug::printString(debug::TYPE_PRECEDENCE, "= so push token");
$stack->push($token);
$token = $this->scanner->next(TRUE);
break;
case '<':
debug::printString(debug::TYPE_PRECEDENCE, "< so push < and token");
$stack->pushTerminal('<');
$stack->push($token);
$token = $this->scanner->next(TRUE);
break;
case '>':
// Hledáme pravidlo!
$found = FALSE;
foreach ($this->rules as $key => $rule) {
$error = FALSE;
for ($i = 0; $i < count($rule['source']); $i++) {
$tmp = $stack->top($i);
if (is_array($tmp) && !isset($tmp['nonTerminal'])) {
$tmp = $this->normalizeCodes($tmp);
}
if (is_array($tmp) && $tmp['nonTerminal'] != $rule['source'][count($rule['source']) - 1 - $i] || !is_array($tmp) && $tmp != $rule['source'][count($rule['source']) - 1 - $i]) {
$error = TRUE;
$i = count($rule['source']);
}
}
$error = $error || $stack->top(count($rule['source'])) != '<';
if (!$error) {
$result = [];
foreach ($rule['source'] as $unused) {
$result[] = $stack->pop();
}
$stack->pop();
$stack->push(['nonTerminal' => $rule['target'], 'terminals' => $result]);
$found = TRUE;
break;
}
}
if (!$found) {
$stack->debug();
throw new PrecedenceException("Error - rule not found! " . print_r($token, TRUE) . " " . print_r($a, TRUE));
} else {
debug::printString(debug::TYPE_PRECEDENCE, "rule: " . $key . " " . $rule['target'] . " -> " . implode(' ', $rule['source']));
}
break;
case '#':
if ($a == '$' && in_array($token['code'], $this->stopTokens)) {
$this->scanner->back();
$token = ['code' => T_SEMICOLON, 'value' => ';'];
} else {
throw new PrecedenceException(print_r($a, TRUE) . print_r($token, TRUE));
}
break;
default:
die("Chyba v precedenční tabulce");
}
// This solve function calls with any argument number
try {
$e1 = $stack->top();
$comma = $stack->top(1);
$e2 = $stack->top(2);
if (isset($e1['nonTerminal']) && $e1['nonTerminal'] == 'E' && isset($comma['code']) && $comma['code'] == T_COMMA && isset($e2['nonTerminal']) && $e2['nonTerminal'] == 'E') {
$stack->pop();
$stack->pop();
$stack->pop();
$stack->push(['nonTerminal' => 'E', 'terminals' => [$e1, $comma, $e2]]);
$stack->debug();
echo "\n";
}
} catch (StackEmptyException $e) {
}
} catch (PrecedenceNotInTableException $e) {
$this->scanner->back();
$token = ['code' => T_SEMICOLON, 'value' => ';'];
}
debug::printStack(debug::TYPE_PRECEDENCE, $stack);
} while ($this->normalizeCodes($token) != '$' || $stack->topTerminal() != '$');
$this->result = $stack->top();
}