Esempio n. 1
0
 public function matchSymbol(Symbol $symbol)
 {
     if ($symbol instanceof EpsilonSymbol) {
         return true;
     }
     if (EpsilonSymbol::singletonInstance()->getType() == $symbol->getType()) {
         return EpsilonSymbol::singletonInstance()->toString() == $symbol->toString();
     }
     return false;
 }
Esempio n. 2
0
 /**
  * @return ExpressionSymbol
  */
 public static function createAdapterForEpsilon()
 {
     $epsilon = new EpsilonSymbol();
     return new self($epsilon->getType(), $epsilon->toString());
 }
Esempio n. 3
0
 /**
  * @param Symbol $nonTerminal
  *
  * @return bool
  * @throws \Exception
  */
 public function addEpsilon(Symbol $nonTerminal)
 {
     $terminalSet = $this->getTerminalSet($nonTerminal);
     if (is_null($terminalSet)) {
         throw new \Exception('no terminal set found for symbol ' . $nonTerminal->toString());
     }
     return $terminalSet->add(EpsilonSymbol::singletonInstance());
 }
 public function testComputeFromListOfNonTerminalsWhichDeriveEpsilon()
 {
     $nonTerminals = ['A_1' => StandardSymbol::nonTerminal('A_1'), 'A_2' => StandardSymbol::nonTerminal('A_2'), 'A_3' => StandardSymbol::nonTerminal('A_3'), 'A_4' => StandardSymbol::nonTerminal('A_4')];
     $terminals = ['b_1' => StandardSymbol::terminal('b_1'), 'b_2' => StandardSymbol::terminal('b_2'), 'b_4' => StandardSymbol::terminal('b_4')];
     $parseSets = ['A_1' => [EpsilonSymbol::singletonInstance(), $terminals['b_1']], 'A_2' => [EpsilonSymbol::singletonInstance(), $terminals['b_2']], 'A_3' => [EpsilonSymbol::singletonInstance()], 'A_4' => [EpsilonSymbol::singletonInstance(), $terminals['b_4']]];
     $sentence = 'A_2 A_2 A_1 A_3 A_4';
     $expectedSet = new ArraySet();
     $expectedSet->add($terminals['b_1']);
     $expectedSet->add($terminals['b_2']);
     $expectedSet->add($terminals['b_4']);
     $epsilon = EpsilonSymbol::singletonInstance();
     $expectedSet->add($epsilon);
     $actualSet = $this->buildFirstSet($sentence, $parseSets, $nonTerminals, $terminals);
     $this->assertEquals($expectedSet->count(), $actualSet->count(), 'unexpected number of actual items found');
     /** @var Symbol $symbol */
     foreach ($actualSet as $symbol) {
         $contains = $expectedSet->contains($symbol);
         $this->assertTrue($contains, sprintf('Symbol %s not found', $symbol->toString()));
     }
 }