当前位置: 首页>>代码示例>>PHP>>正文


PHP Stack::topTerminal方法代码示例

本文整理汇总了PHP中Stack::topTerminal方法的典型用法代码示例。如果您正苦于以下问题:PHP Stack::topTerminal方法的具体用法?PHP Stack::topTerminal怎么用?PHP Stack::topTerminal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Stack的用法示例。


在下文中一共展示了Stack::topTerminal方法的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();
 }
开发者ID:nechutny,项目名称:BP,代码行数:89,代码来源:Precedence.php


注:本文中的Stack::topTerminal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。