/** * @param Production $production * @return array|Production[] */ public function transform(Production $production) { $expression = $production->getExpression(); $visitor = new Visitor(); $hierarchicVisitDispatcher = new CompleteVisitDispatcher($visitor); $walker = new DepthFirstStackBasedWalker(); $walker->walk($expression, $hierarchicVisitDispatcher); $rootExpression = $visitor->getRoot(); $production = new StandardProduction($production->getNonTerminal(), $rootExpression); return array($production); }
/** * @param Production $production * @return array|Production[] */ public function transform(Production $production) { $expression = $production->getExpression(); if ($expression instanceof Choice) { $productions = array(); $expressions = iterator_to_array($expression->getIterator()); foreach ($expressions as $expression) { $productions[] = new StandardProduction($production->getNonTerminal(), $expression); } return $productions; } return array($production); }
/** * @param Production $initialProduction * @return array|Production[] */ public function transform(Production $initialProduction) { $expression = $initialProduction->getExpression(); $visitor = new OptionalsEliminator($this->nonTerminalNamingStrategy); $hierarchicVisitDispatcher = new CompleteVisitDispatcher($visitor); $walker = new DepthFirstStackBasedWalker(); $walker->walk($expression, $hierarchicVisitDispatcher); $visitRoot = $visitor->getRoot(); $cleanedProduction = new StandardProduction($initialProduction->getNonTerminal(), $visitRoot); $cleanedProductionsList = array($cleanedProduction); $cleanedProductionsList = array_merge($cleanedProductionsList, $visitor->getEpsilonAlternatives()); return $cleanedProductionsList; }
/** * @param \Helstern\Nomsky\Grammar\Production\Production $p * * @return \Helstern\Nomsky\GrammarAnalysis\Production\NormalizedProduction * @throws \Exception */ public function normalizeProduction(Production $p) { $expression = $p->getExpression(); if ($expression instanceof ExpressionSymbol) { $lhs = $p->getNonTerminal(); $normalized = $this->processExpressionSymbolProduction($lhs, $expression); return $normalized; } if ($expression instanceof Concatenation) { $lhs = $p->getNonTerminal(); $normalized = $this->processConcatenationProduction($lhs, $expression); return $normalized; } throw new \Exception('the production has not been simplified enough'); }