public function testVisitExpressionTreeOfOneLeaf() { $expression = ExpressionSymbol::createAdapterForTerminal('a'); $eliminator = new OptionalsEliminator(new IncrementalNamingStrategy()); $eliminator->visitExpression($expression); $actual = $eliminator->getRoot(); $this->assertEquals($expression, $actual); }
public function testVisitExpressionTreeOfOneLeaf() { $expression = ExpressionSymbol::createAdapterForTerminal('a'); $eliminator = new Visitor(); $eliminator->visitExpression($expression); $actual = $eliminator->getRoot(); $this->assertEquals($expression, $actual); }
/** * S -> a S e * S -> B * B -> b B e * B -> C * C -> c C e * C -> d */ public static function productions() { $productions = []; $productions[] = new StandardProduction(StandardSymbol::nonTerminal('S'), new Concatenation(ExpressionSymbol::createAdapterForTerminal('a'), [ExpressionSymbol::createAdapterForNonTerminal('S'), ExpressionSymbol::createAdapterForTerminal('e')])); $productions[] = new StandardProduction(StandardSymbol::nonTerminal('S'), ExpressionSymbol::createAdapterForNonTerminal('B')); $productions[] = new StandardProduction(StandardSymbol::nonTerminal('B'), new Concatenation(ExpressionSymbol::createAdapterForTerminal('b'), [ExpressionSymbol::createAdapterForNonTerminal('B'), ExpressionSymbol::createAdapterForTerminal('e')])); $productions[] = new StandardProduction(StandardSymbol::nonTerminal('B'), ExpressionSymbol::createAdapterForNonTerminal('C')); $productions[] = new StandardProduction(StandardSymbol::nonTerminal('C'), new Concatenation(ExpressionSymbol::createAdapterForTerminal('c'), [ExpressionSymbol::createAdapterForNonTerminal('C'), ExpressionSymbol::createAdapterForTerminal('e')])); $productions[] = new StandardProduction(StandardSymbol::nonTerminal('C'), ExpressionSymbol::createAdapterForTerminal('d')); return $productions; }
/** * @param string $stringSymbol * @return ExpressionSymbol */ public function createTerminal($stringSymbol) { if ($stringSymbol === '') { return ExpressionSymbol::createAdapterForEpsilon(); } return ExpressionSymbol::createAdapterForTerminal($stringSymbol); }
protected function addEpsilonAlternativeForItem(Symbol $nonTerminal, Expression $optionalExpression) { $alternationItems = array(ExpressionSymbol::createAdapterForEpsilon()); if ($optionalExpression instanceof Concatenation) { $alternationItems[] = $optionalExpression; } elseif ($optionalExpression instanceof Choice) { $alternationItems[] = new Group($optionalExpression); } else { $alternationItems[] = $optionalExpression; } $alternation = new Choice(array_shift($alternationItems), $alternationItems); $production = new StandardProduction($nonTerminal, $alternation); $this->epsilonAlternatives[] = $production; return $production; }
/** * (a | (b | c | (x | y | z) | 1) | 2 | 3) => (a | b | c | x | y | z | 1 | 2 | 3) */ public function testAlternationWithMultipleNestedAlternationAsSibling() { // $this->markTestSkipped('s'); $exprTestUtils = $this->getExpressionTestUtils(); $listOfSymbols = $exprTestUtils->createListOfExpressions(array('x', 'y', 'z')); $alternation = new Choice(array_shift($listOfSymbols), $listOfSymbols); $group = new Group($alternation); $listOfSymbols = array_merge($exprTestUtils->createListOfExpressions(array('b', 'c')), array($group, ExpressionSymbol::createAdapterForTerminal('1'))); $alternation = new Choice(array_shift($listOfSymbols), $listOfSymbols); $group = new Group($alternation); $listOfSymbols = array_merge(array(ExpressionSymbol::createAdapterForTerminal('a'), $group), $exprTestUtils->createListOfExpressions(array('2', '3'))); $alternation = new Choice(array_shift($listOfSymbols), $listOfSymbols); $actualExpressionWithoutGroups = $this->getDepthFirstWalkResult($alternation); $expectedExpressionWithoutGroups = $exprTestUtils->createChoiceFromListOfStringSymbols(array('a', 'b', 'c', 'x', 'y', 'z', '1', '2', '3')); $this->assertInstanceOf(get_class($expectedExpressionWithoutGroups), $actualExpressionWithoutGroups, 'there should have been some expressions'); /** @var ExpressionIterable $actualExpressionWithoutGroups */ $actualListOfSymbols = $actualExpressionWithoutGroups->toArray(); $expectedListOfSymbols = $expectedExpressionWithoutGroups->toArray(); $assertFailMsgTpl = 'Expected the following alternation: %s'; $this->assertEquals($expectedListOfSymbols, $actualListOfSymbols, sprintf($assertFailMsgTpl, $exprTestUtils->serializeExpressionIterable($expectedExpressionWithoutGroups))); }