Ejemplo n.º 1
0
 /**
  * @param NormalizedProduction $production
  *
  * @return bool
  */
 private function directlyGeneratesEpsilon(NormalizedProduction $production)
 {
     //directly generates epsilon
     $answer = $production->count() == 1;
     $firstSymbol = $production->getFirstSymbol();
     $answer &= SymbolIsEpsilon::singletonInstance()->matchSymbol($firstSymbol);
     return $answer;
 }
Ejemplo n.º 2
0
 /**
  * Calculates the first set of $list and adds it's elements to $set
  *
  * @param \Helstern\Nomsky\Grammar\Symbol\SymbolSet $newFirstSet
  * @param array|NormalizedProduction[] $list
  * @param \Helstern\Nomsky\GrammarAnalysis\ParseSets\ParseSets $firstSets
  *
  * @return bool if the epsilon symbol was added to $set
  */
 public function processSymbolList(SymbolSet $newFirstSet, array $list, ParseSets $firstSets)
 {
     if (0 == count($list)) {
         $newFirstSet->add(new EpsilonSymbol());
         return true;
     }
     if (1 == count($list)) {
         /** @var Symbol $symbol */
         $symbol = $list[0];
         return $this->processSymbol($newFirstSet, $symbol, $firstSets);
     }
     $symbolIsEpsilon = SymbolIsEpsilon::singletonInstance();
     $symbolsIsNonTerminal = SymbolTypeEquals::newInstanceMatchingNonTerminals();
     /** @var SymbolSet $lastSet */
     $lastSet = null;
     $epsilonCounter = new MatchCountingInterceptor($symbolIsEpsilon);
     $acceptor = Inverter::newInstance($epsilonCounter);
     //we assume there is no epsilon symbol in the $list list
     $previousSymbol = reset($list);
     //add the first non-terminal and continue past this one later on
     if ($symbolsIsNonTerminal->matchSymbol($previousSymbol)) {
         $lastSet = $firstSets->filterTerminalSet($previousSymbol, $acceptor);
         $newFirstSet->addAll($lastSet);
     } else {
         $newFirstSet->add($previousSymbol);
     }
     //as long as the previous symbol was a non-terminal, process the next symbol
     for (next($list); !is_null(key($list)) && $symbolsIsNonTerminal->matchSymbol($previousSymbol) && $epsilonCounter->getMatchCount() > 0; next($list)) {
         $previousSymbol = current($list);
         $epsilonCounter = new MatchCountingInterceptor($symbolIsEpsilon);
         $acceptor = Inverter::newInstance($epsilonCounter);
         if ($symbolsIsNonTerminal->matchSymbol($previousSymbol)) {
             $lastSet = $firstSets->filterTerminalSet($previousSymbol, $acceptor);
             $newFirstSet->addAll($lastSet);
         } else {
             $newFirstSet->add($previousSymbol);
         }
     }
     if (is_null(key($list)) && $symbolsIsNonTerminal->matchSymbol($previousSymbol) && $epsilonCounter->getMatchCount() > 0) {
         $newFirstSet->add(new EpsilonSymbol());
         return true;
     }
     return false;
 }