Пример #1
0
 /**
  * @param array $nonTerminals
  *
  * @return LLParseTableBuilder
  */
 public function addNonTerminals(array $nonTerminals)
 {
     $set = new ArraySet();
     foreach ($nonTerminals as $symbol) {
         $set->add($symbol);
     }
     $this->nonTerminals = $set;
     return $this;
 }
Пример #2
0
 /**
  * @param Symbol $nonTerminal
  * @param SymbolPredicate $acceptPredicate
  * @return ArraySet
  */
 public function filterTerminalSet(Symbol $nonTerminal, SymbolPredicate $acceptPredicate)
 {
     $terminalSetIterator = $this->getTerminalSetIterator($nonTerminal);
     $filtered = new ArraySet();
     while ($terminalSetIterator->valid()) {
         $terminal = $terminalSetIterator->current();
         $terminalSetIterator->next();
         if ($acceptPredicate->matchSymbol($terminal)) {
             $filtered->add($terminal);
         }
     }
     return $filtered;
 }
Пример #3
0
 /**
  * @return LookAheadSets
  */
 public static function predictSets()
 {
     $productions = self::normalizedProductions();
     $sets = new LookAheadSets(new SimpleHashKeyFactory());
     $set = new ArraySet();
     $set->add(StandardSymbol::terminal('a'));
     $sets->add($productions[0], $set);
     $set = new ArraySet();
     $set->add(StandardSymbol::terminal('b'));
     $set->add(StandardSymbol::terminal('c'));
     $set->add(StandardSymbol::terminal('d'));
     $sets->add($productions[1], $set);
     $set = new ArraySet();
     $set->add(StandardSymbol::terminal('b'));
     $sets->add($productions[2], $set);
     $set = new ArraySet();
     $set->add(StandardSymbol::terminal('c'));
     $set->add(StandardSymbol::terminal('d'));
     $sets->add($productions[3], $set);
     $set = new ArraySet();
     $set->add(StandardSymbol::terminal('c'));
     $sets->add($productions[4], $set);
     $set = new ArraySet();
     $set->add(StandardSymbol::terminal('d'));
     $sets->add($productions[5], $set);
     return $sets;
 }
 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()));
     }
 }