コード例 #1
0
ファイル: BinaryUnion.cs プロジェクト: apodavalov/FLaG
        internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber,
                                                                 ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies)
        {
            CheckDependencies(dependencies);

            Grammar leftExpGrammar  = dependencies[0].GrammarExpression.Grammar;
            Grammar rightExpGrammar = dependencies[1].GrammarExpression.Grammar;

            NonTerminalSymbol target = new NonTerminalSymbol(new Label(new SingleLabel(Grammar._DefaultNonTerminalSymbol, index++)));

            Rule rule = new Rule(
                EnumerateHelper.Sequence(
                    new Chain(EnumerateHelper.Sequence(leftExpGrammar.Target)),
                    new Chain(EnumerateHelper.Sequence(rightExpGrammar.Target))
                    ), target);

            IEnumerable <Rule> newRules = leftExpGrammar.Rules.Concat(rightExpGrammar.Rules).Concat(rule);

            GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple(this, new Grammar(newRules, target), grammarNumber);

            if (onIterate != null)
            {
                onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies));
            }

            return(grammarExpressionTuple);
        }
コード例 #2
0
ファイル: Empty.cs プロジェクト: apodavalov/FLaG
        internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber,
                                                                 ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies)
        {
            CheckDependencies(dependencies);

            NonTerminalSymbol target = new NonTerminalSymbol(new Label(new SingleLabel(Grammar._DefaultNonTerminalSymbol, index++)));

            GrammarExpressionTuple grammarExpressionTuple =
                new GrammarExpressionTuple(
                    this,
                    new Grammar(
                        EnumerateHelper.Sequence(
                            new Rule(EnumerateHelper.Sequence(new Chain(Enumerable.Empty <Grammars.Symbol>())), target)
                            ),
                        target
                        ),
                    grammarNumber
                    );

            if (onIterate != null)
            {
                onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies));
            }

            return(grammarExpressionTuple);
        }
コード例 #3
0
        private GrammarExpressionTuple GenerateLeftGrammar(int grammarNumber,
                                                           ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies)
        {
            Grammar leftExpGrammar  = dependencies[0].GrammarExpression.Grammar;
            Grammar rightExpGrammar = dependencies[1].GrammarExpression.Grammar;

            IReadOnlySet <Rule> terminalSymbolsOnlyRules;
            IReadOnlySet <Rule> otherRules;

            rightExpGrammar.SplitRules(out terminalSymbolsOnlyRules, out otherRules);

            ISet <Rule> newRules = new HashSet <Rule>(otherRules.Concat(leftExpGrammar.Rules));

            foreach (Rule rule in terminalSymbolsOnlyRules)
            {
                ISet <Chain> newChains = new HashSet <Chain>(
                    rule.Chains.Select(
                        chain => new Chain(
                            EnumerateHelper.Sequence(leftExpGrammar.Target).Concat(chain)
                            )
                        )
                    );

                newRules.Add(new Rule(newChains, rule.Target));
            }

            GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple(this, new Grammar(newRules, rightExpGrammar.Target), grammarNumber);

            if (onIterate != null)
            {
                onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies));
            }

            return(grammarExpressionTuple);
        }
コード例 #4
0
        public GrammarExpressionWithOriginal(GrammarExpressionTuple grammarExpression, GrammarExpressionTuple originalGrammarExpression = null)
        {
            if (grammarExpression == null)
            {
                throw new ArgumentNullException(nameof(grammarExpression));
            }

            GrammarExpression         = grammarExpression;
            OriginalGrammarExpression = originalGrammarExpression;
        }
コード例 #5
0
ファイル: ConstIteration.cs プロジェクト: apodavalov/FLaG
        private GrammarExpressionTuple Mirror(GrammarExpressionTuple grammar, ref int index, ref int additionalGrammarNumber)
        {
            Grammar newGrammar = grammar.Grammar.Reorganize(index);

            index += newGrammar.NonTerminals.Count;

            return
                (new GrammarExpressionTuple(
                     grammar.Expression,
                     newGrammar,
                     additionalGrammarNumber++
                     ));
        }
コード例 #6
0
ファイル: ConstIteration.cs プロジェクト: apodavalov/FLaG
        internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber,
                                                                 ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies)
        {
            CheckDependencies(dependencies);

            if (IterationCount == 0)
            {
                return(Empty.Instance.GenerateGrammar(grammarType, grammarNumber, ref index, ref additionalGrammarNumber, onIterate));
            }

            GrammarExpressionTuple original = dependencies[0].GrammarExpression;

            if (IterationCount == 1)
            {
                GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple(this, original.Grammar, grammarNumber);

                if (onIterate != null)
                {
                    onIterate(new GrammarPostReport(grammarExpressionTuple, new GrammarExpressionWithOriginal(original).AsSequence()));
                }

                return(grammarExpressionTuple);
            }

            GrammarExpressionTuple dependency1 = original;

            for (int i = 1; i < IterationCount; i++)
            {
                GrammarExpressionTuple dependency2 = Mirror(original, ref index, ref additionalGrammarNumber);

                int number;

                if (i == IterationCount - 1)
                {
                    number = grammarNumber;
                }
                else
                {
                    number = additionalGrammarNumber++;
                }

                GrammarExpressionTuple expressionTuple =
                    new BinaryConcat(dependency1.Expression, dependency2.Expression).
                    GenerateGrammar(grammarType, number, ref index, ref additionalGrammarNumber, onIterate, new GrammarExpressionWithOriginal(dependency1), new GrammarExpressionWithOriginal(dependency2, original));

                dependency1 = expressionTuple;
            }

            return(dependency1);
        }
コード例 #7
0
        public GrammarPostReport(GrammarExpressionTuple @new, IEnumerable <GrammarExpressionWithOriginal> dependencies)
        {
            if (@new == null)
            {
                throw new ArgumentNullException(nameof(@new));
            }

            if (dependencies == null)
            {
                throw new ArgumentNullException(nameof(dependencies));
            }

            New          = @new;
            Dependencies = dependencies.ToList().AsReadOnly();

            if (Dependencies.AnyNull())
            {
                throw new ArgumentException("At least one element is null.", nameof(dependencies));
            }
        }
コード例 #8
0
ファイル: Iteration.cs プロジェクト: apodavalov/FLaG
        internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber,
                                                                 ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies)
        {
            CheckDependencies(dependencies);

            Func <Chain, NonTerminalSymbol, IEnumerable <Chain> > chainEnumerator;
            Grammar expGrammar = dependencies[0].GrammarExpression.Grammar;

            switch (grammarType)
            {
            case GrammarType.Left:
                chainEnumerator = LeftChainEnumerator;
                break;

            case GrammarType.Right:
                chainEnumerator = RightChainEnumerator;
                break;

            default:
                throw new InvalidOperationException(UnknownGrammarMessage(grammarType));
            }


            IReadOnlySet <Rule> terminalSymbolsOnlyRules;
            IReadOnlySet <Rule> otherRules;

            expGrammar.SplitRules(out terminalSymbolsOnlyRules, out otherRules);

            ISet <Rule> newRules = new HashSet <Rule>(otherRules);

            foreach (Rule rule in terminalSymbolsOnlyRules)
            {
                ISet <Chain> newChains = new HashSet <Chain>(
                    rule.Chains.SelectMany(chain => chainEnumerator(chain, expGrammar.Target))
                    );

                newRules.Add(new Rule(newChains, rule.Target));
            }

            NonTerminalSymbol symbol = new NonTerminalSymbol(new Label(new SingleLabel(Grammar._DefaultNonTerminalSymbol, index++)));

            IEnumerable <Chain> chains = EnumerateHelper.Sequence(new Chain(EnumerateHelper.Sequence(expGrammar.Target)));

            if (!IsPositive)
            {
                chains = chains.Concat(new Chain(Enumerable.Empty <Grammars.Symbol>()));
            }

            newRules.Add(new Rule(chains, symbol));

            GrammarExpressionTuple grammarExpressionTuple =
                new GrammarExpressionTuple(
                    this,
                    new Grammar(newRules, symbol),
                    grammarNumber
                    );

            if (onIterate != null)
            {
                onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies));
            }

            return(grammarExpressionTuple);
        }