Exemplo n.º 1
0
 /**
  * Reduce the size of the action tables, if possible, by making use
  * of defaults.
  *
  * In this version, we take the most frequent REDUCE action and make
  * it the default.
  */
 function CompressTables()
 {
     for ($i = 0; $i < $this->nstate; $i++) {
         $stp = $this->sorted[$i]->data;
         $nbest = 0;
         $rbest = 0;
         for ($ap = $stp->ap; $ap; $ap = $ap->next) {
             if ($ap->type != PHP_ParserGenerator_Action::REDUCE) {
                 continue;
             }
             $rp = $ap->x;
             if ($rp === $rbest) {
                 continue;
             }
             $n = 1;
             for ($ap2 = $ap->next; $ap2; $ap2 = $ap2->next) {
                 if ($ap2->type != PHP_ParserGenerator_Action::REDUCE) {
                     continue;
                 }
                 $rp2 = $ap2->x;
                 if ($rp2 === $rbest) {
                     continue;
                 }
                 if ($rp2 === $rp) {
                     $n++;
                 }
             }
             if ($n > $nbest) {
                 $nbest = $n;
                 $rbest = $rp;
             }
         }
         /* Do not make a default if the number of rules to default
          ** is not at least 1 */
         if ($nbest < 1) {
             continue;
         }
         /* Combine matching REDUCE actions into a single default */
         for ($ap = $stp->ap; $ap; $ap = $ap->next) {
             if ($ap->type == PHP_ParserGenerator_Action::REDUCE && $ap->x === $rbest) {
                 break;
             }
         }
         if ($ap === 0) {
             throw new Exception('$ap is not an object');
         }
         $ap->sp = PHP_ParserGenerator_Symbol::Symbol_new("{default}");
         for ($ap = $ap->next; $ap; $ap = $ap->next) {
             if ($ap->type == PHP_ParserGenerator_Action::REDUCE && $ap->x === $rbest) {
                 $ap->type = PHP_ParserGenerator_Action::NOT_USED;
             }
         }
         $stp->ap = PHP_ParserGenerator_Action::Action_sort($stp->ap);
     }
 }
Exemplo n.º 2
0
 /**
  * create linked list of PHP_ParserGenerator_Actions
  *
  * @param PHP_ParserGenerator_Action|null
  * @param int one of the class constants from PHP_ParserGenerator_Action
  * @param PHP_ParserGenerator_Symbol
  * @param PHP_ParserGenerator_State|PHP_ParserGenerator_Rule
  */
 public static function Action_add(&$app, $type, PHP_ParserGenerator_Symbol $sp, $arg)
 {
     $new = new PHP_ParserGenerator_Action();
     $new->next = $app;
     $app = $new;
     $new->type = $type;
     $new->sp = $sp;
     $new->x = $arg;
     echo ' Adding ';
     $new->display();
 }