Exemplo n.º 1
0
 public void RemoveProduction(ProductionModel productionModel)
 {
     if (!_matrix.ContainsKey(productionModel.LeftHandSide.NonTerminal))
         _matrix.Remove(productionModel.LeftHandSide.NonTerminal);
     if (!_lookup.ContainsKey(productionModel.LeftHandSide.NonTerminal))
         _lookup.Remove(productionModel.LeftHandSide.NonTerminal);
 }
Exemplo n.º 2
0
        public void AddProduction(ProductionModel production)
        {
            if (!_matrix.ContainsKey(production.LeftHandSide.NonTerminal))
            {
                _matrix[production.LeftHandSide.NonTerminal] = new UniqueList <NonTerminalModel>();
            }

            if (!_lookup.ContainsKey(production.LeftHandSide.NonTerminal))
            {
                _lookup[production.LeftHandSide.NonTerminal] = production;
            }

            foreach (var alteration in production.Alterations)
            {
                for (var s = 0; s < alteration.Symbols.Count; s++)
                {
                    var symbol = alteration.Symbols[s];
                    if (symbol.ModelType != SymbolModelType.Production ||
                        symbol.ModelType != SymbolModelType.Reference)
                    {
                        continue;
                    }
                    AddProductionToNewOrExistingSymbolSet(production, symbol);
                }
            }
        }
Exemplo n.º 3
0
 public void RemoveProduction(ProductionModel productionModel)
 {
     if (!_matrix.ContainsKey(productionModel.LeftHandSide.NonTerminal))
     {
         _matrix.Remove(productionModel.LeftHandSide.NonTerminal);
     }
     if (!_lookup.ContainsKey(productionModel.LeftHandSide.NonTerminal))
     {
         _lookup.Remove(productionModel.LeftHandSide.NonTerminal);
     }
 }
Exemplo n.º 4
0
        public void GrammarModelConstructorGivenOnlyStartProductionShouldTraverseRecursiveStructureOnlyOnce()
        {
            var S = new ProductionModel("S");
            var A = new ProductionModel("A");
            S.AddWithAnd(S);
            S.AddWithOr(A);
            A.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("a")));

            var grammarModel = new GrammarModel(S);
            var grammar = grammarModel.ToGrammar();

            Assert.AreEqual(3, grammar.Productions.Count);
        }
Exemplo n.º 5
0
        IEnumerable<ProductionModel> Expression(EbnfExpression expression, ProductionModel currentProduction)
        {
            foreach (var production in Term(expression.Term, currentProduction))
                yield return production;

            if (expression.NodeType != EbnfNodeType.EbnfExpressionAlteration)
                yield break;

            var expressionAlteration = expression as EbnfExpressionAlteration;
            currentProduction.Lambda();

            foreach (var production in Expression(expressionAlteration.Expression, currentProduction))
                yield return production;
        }
Exemplo n.º 6
0
        public IGrammar ToGrammar()
        {
            if (StartSymbolExists())
            {
                if (ProductionsAreEmpty())
                    PopulateMissingProductionsFromStart(Start);
                AssertStartProductionExistsForStartSymbol(_reachibilityMatrix);
            }
            else
                Start = _reachibilityMatrix.GetStartProduction();

            var productions = GetProductionsFromProductionsModel();
            var ignoreRules = GetIgnoreRulesFromIgnoreRulesModel();

            return new Grammar(Start.LeftHandSide.NonTerminal, productions, ignoreRules);
        }
Exemplo n.º 7
0
        public void GrammarModelConstructorGivenOnlyStartProductionShouldDiscoverLinkedProductions()
        {
            var S = new ProductionModel("S");
            var A = new ProductionModel("A");
            var B = new ProductionModel("B");
            var C = new ProductionModel("C");
            S.AddWithAnd(A);
            A.AddWithAnd(B);
            A.AddWithOr(C);
            B.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("b")));
            C.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("c")));

            var grammarModel = new GrammarModel(S);
            var grammar = grammarModel.ToGrammar();

            Assert.AreEqual(5, grammar.Productions.Count);
        }
Exemplo n.º 8
0
 private void PopulateMissingProductionsRecursively(ProductionModel production, ISet <INonTerminal> visited)
 {
     if (visited.Add(production.LeftHandSide.NonTerminal))
     {
         Productions.Add(production);
         foreach (var alteration in production.Alterations)
         {
             for (var s = 0; s < alteration.Symbols.Count; s++)
             {
                 var symbol = alteration.Symbols[s];
                 if (symbol.ModelType == SymbolModelType.Production)
                 {
                     PopulateMissingProductionsRecursively(symbol as ProductionModel, visited);
                 }
             }
         }
     }
 }
Exemplo n.º 9
0
        public void AddProduction(ProductionModel production)
        {
            if (!_matrix.ContainsKey(production.LeftHandSide.NonTerminal))
                _matrix[production.LeftHandSide.NonTerminal] = new UniqueList<NonTerminalModel>();

            if (!_lookup.ContainsKey(production.LeftHandSide.NonTerminal))
                _lookup[production.LeftHandSide.NonTerminal] = production;

            foreach (var alteration in production.Alterations)
            {
                for(var s = 0; s< alteration.Symbols.Count; s++)
                {
                    var symbol = alteration.Symbols[s];
                    if (symbol.ModelType != SymbolModelType.Production
                        || symbol.ModelType != SymbolModelType.Reference)
                        continue;
                    AddProductionToNewOrExistingSymbolSet(production, symbol);
                }
            }
        }
Exemplo n.º 10
0
        public IGrammar ToGrammar()
        {
            if (StartSymbolExists())
            {
                if (ProductionsAreEmpty())
                {
                    PopulateMissingProductionsFromStart(Start);
                }
                AssertStartProductionExistsForStartSymbol(_reachibilityMatrix);
            }
            else
            {
                Start = _reachibilityMatrix.GetStartProduction();
            }

            var productions = GetProductionsFromProductionsModel();
            var ignoreRules = GetIgnoreRulesFromIgnoreRulesModel();

            return(new Grammar(Start.LeftHandSide.NonTerminal, productions, ignoreRules));
        }
Exemplo n.º 11
0
        public void GrammarModelGivenNullStartShouldResolveStartFromProductions()
        {
            var S = new ProductionModel("S");
            var A = new ProductionModel("A");
            var B = new ProductionModel("B");

            S.AddWithAnd(A);
            S.AddWithAnd(B);
            A.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("a")));
            A.AddWithAnd(B);
            B.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("b")));

            var grammarModel = new GrammarModel();
            grammarModel.Productions.Add(S);
            grammarModel.Productions.Add(A);
            grammarModel.Productions.Add(B);

            var grammar = grammarModel.ToGrammar();
            Assert.AreEqual(3, grammar.Productions.Count);
            Assert.IsNotNull(grammar.Start);
        }
Exemplo n.º 12
0
        public void AddProduction(ProductionModel production)
        {
            if (!this.matrix.ContainsKey(production.LeftHandSide.NonTerminal))
            {
                this.matrix[production.LeftHandSide.NonTerminal] = new UniqueList <NonTerminalModel>();
            }

            if (!this.lookup.ContainsKey(production.LeftHandSide.NonTerminal))
            {
                this.lookup[production.LeftHandSide.NonTerminal] = production;
            }

            foreach (var alteration in production.Alterations)
            {
                foreach (var symbol in alteration.Symbols)
                {
                    if (symbol is NonTerminalModel nonTerminalModel)
                    {
                        AddProductionToNewOrExistingSymbolSet(production, nonTerminalModel);
                    }
                }
            }
        }
Exemplo n.º 13
0
 public ProductionExpression(INonTerminal leftHandSide)
 {
     ProductionModel = new ProductionModel(leftHandSide);
 }
Exemplo n.º 14
0
 public void OnResetProductions()
 {
     Start = null;
     _reachibilityMatrix.ClearProductions();
 }
Exemplo n.º 15
0
 private void PopulateMissingProductionsRecursively(ProductionModel production, ISet<INonTerminal> visited)
 {
     if (visited.Add(production.LeftHandSide.NonTerminal))
     {
         Productions.Add(production);
         foreach (var alteration in production.Alterations)
             for (var s =0; s< alteration.Symbols.Count; s++)
             {
                 var symbol = alteration.Symbols[s];
                 if (symbol.ModelType == SymbolModelType.Production)
                     PopulateMissingProductionsRecursively(symbol as ProductionModel, visited);
             }
     }
 }
Exemplo n.º 16
0
 private void OnRemoveProduction(ProductionModel productionModel)
 {
     _reachibilityMatrix.RemoveProduction(productionModel);
 }
Exemplo n.º 17
0
 private void OnAddProduction(ProductionModel productionModel)
 {
     _reachibilityMatrix.AddProduction(productionModel);
 }
Exemplo n.º 18
0
 public GrammarModel(ProductionModel start)
     : this()
 {
     Start = start;
 }
Exemplo n.º 19
0
 IEnumerable<ProductionModel> Rule(EbnfRule rule)
 {
     var nonTerminal = GetNonTerminalFromQualifiedIdentifier(rule.QualifiedIdentifier);
     var productionModel = new ProductionModel(nonTerminal);
     foreach(var production in Expression(rule.Expression, productionModel))
         yield return production;
     yield return productionModel;
 }
Exemplo n.º 20
0
        IEnumerable<ProductionModel> Repetition(EbnfFactorRepetition repetition, ProductionModel currentProduction)
        {
            var name = repetition.ToString();
            var nonTerminal = new NonTerminal(name);
            var repetitionProduction = new ProductionModel(nonTerminal);

            currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));

            var expression = repetition.Expression;
            foreach (var production in Expression(expression, repetitionProduction))
                yield return production;

            repetitionProduction.AddWithAnd(new NonTerminalModel(nonTerminal));
            repetitionProduction.Lambda();

            yield return repetitionProduction;
        }
Exemplo n.º 21
0
        IEnumerable<ProductionModel> Optional(EbnfFactorOptional optional, ProductionModel currentProduction)
        {
            var name = optional.ToString();
            var nonTerminal = new NonTerminal(name);
            var optionalProduction = new ProductionModel(nonTerminal);

            currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));

            var expression = optional.Expression;
            foreach (var production in Expression(expression, optionalProduction))
                yield return production;

            optionalProduction.Lambda();
            yield return optionalProduction;
        }
Exemplo n.º 22
0
        IEnumerable<ProductionModel> Grouping(EbnfFactorGrouping grouping, ProductionModel currentProduction)
        {
            var name = grouping.ToString();
            var nonTerminal = new NonTerminal(name);
            var groupingProduction = new ProductionModel(nonTerminal);

            currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));

            var expression = grouping.Expression;
            foreach (var production in Expression(expression, groupingProduction))
                yield return production;

            yield return groupingProduction;
        }
Exemplo n.º 23
0
        public void GrammarModelToGrammarShouldCreateGrammar()
        {
            var grammarModel = new GrammarModel();

            var S = new ProductionModel("S");
            var A = new ProductionModel("A");
            var B = new ProductionModel("B");

            var a = new StringLiteralLexerRule("a");
            var b = new StringLiteralLexerRule("b");
            var space = new StringLiteralLexerRule(" ");

            S.AddWithAnd(A.LeftHandSide);
            S.AddWithAnd(B.LeftHandSide);
            S.AddWithOr(B.LeftHandSide);
            A.AddWithAnd(new LexerRuleModel(a));
            B.AddWithAnd(new LexerRuleModel(b));

            grammarModel.Productions.Add(S);
            grammarModel.Productions.Add(A);
            grammarModel.Productions.Add(B);

            grammarModel.IgnoreRules.Add(new LexerRuleModel(space));

            grammarModel.Start = S;

            var grammar = grammarModel.ToGrammar();
            Assert.AreEqual(4, grammar.Productions.Count);
            Assert.AreEqual(1, grammar.Ignores.Count);
        }
Exemplo n.º 24
0
 public void GrammarModelToGrammarShouldAddProductionWhenEmptyDefinition()
 {
     var S = new ProductionModel("S");
     var grammarModel = new GrammarModel(S);
     var grammar = grammarModel.ToGrammar();
     Assert.AreEqual(1, grammar.Productions.Count);
 }
Exemplo n.º 25
0
 public void AddProduction(ProductionModel productionModel)
 {
     this.productionModels.Add(productionModel);
     this.reachabilityMatrix.AddProduction(productionModel);
 }
Exemplo n.º 26
0
        IEnumerable<ProductionModel> Term(EbnfTerm term, ProductionModel currentProduction)
        {
            foreach (var production in Factor(term.Factor, currentProduction))
                yield return production;

            if (term.NodeType != EbnfNodeType.EbnfTermConcatenation)
                yield break;

            var concatenation = term as EbnfTermConcatenation;
            foreach(var production in Term(concatenation.Term, currentProduction))
                yield return production;
        }
Exemplo n.º 27
0
 private void OnRemoveProduction(ProductionModel productionModel)
 {
     _reachibilityMatrix.RemoveProduction(productionModel);
 }
Exemplo n.º 28
0
 public StartProductionSettingModel(ProductionModel productionModel)
     : base(SettingKey, productionModel.LeftHandSide.NonTerminal.Value)
 {
 }
Exemplo n.º 29
0
 private void OnResetProductions()
 {
     Start = null;
     _reachibilityMatrix.ClearProductions();
 }
Exemplo n.º 30
0
        IEnumerable<ProductionModel> Factor(EbnfFactor factor, ProductionModel currentProduction)
        {
            switch (factor.NodeType)
            {
                case EbnfNodeType.EbnfFactorGrouping:
                    var grouping = factor as EbnfFactorGrouping;
                    foreach (var production in Grouping(grouping, currentProduction))
                        yield return production;
                    break;

                case EbnfNodeType.EbnfFactorOptional:
                    var optional = factor as EbnfFactorOptional;
                    foreach (var production in Optional(optional, currentProduction))
                        yield return production;
                    break;

                case EbnfNodeType.EbnfFactorRepetition:
                    var repetition = factor as EbnfFactorRepetition;
                    foreach (var production in Repetition(repetition, currentProduction))
                        yield return production;
                    break;

                case EbnfNodeType.EbnfFactorIdentifier:
                    var identifier = factor as EbnfFactorIdentifier;
                    var nonTerminal = GetNonTerminalFromQualifiedIdentifier(identifier.QualifiedIdentifier);
                    currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));
                    break;

                case EbnfNodeType.EbnfFactorLiteral:
                    var literal = factor as EbnfFactorLiteral;
                    var stringLiteralRule = new StringLiteralLexerRule(literal.Value);
                    currentProduction.AddWithAnd( new LexerRuleModel(stringLiteralRule));
                    break;

                case EbnfNodeType.EbnfFactorRegex:
                    var regex = factor as EbnfFactorRegex;
                    var nfa = _thompsonConstructionAlgorithm.Transform(regex.Regex);
                    var dfa = _subsetConstructionAlgorithm.Transform(nfa);
                    var dfaLexerRule = new DfaLexerRule(dfa, regex.Regex.ToString());
                    currentProduction.AddWithAnd(new LexerRuleModel(dfaLexerRule));
                    break;
            }
        }
Exemplo n.º 31
0
 private void OnAddProduction(ProductionModel productionModel)
 {
     _reachibilityMatrix.AddProduction(productionModel);
 }
Exemplo n.º 32
0
        private void AddProductionToNewOrExistingSymbolSet(ProductionModel production, NonTerminalModel symbol)
        {
            var set = this.matrix.AddOrGetExisting(symbol.NonTerminal);

            set.AddUnique(production.LeftHandSide);
        }
Exemplo n.º 33
0
 private void PopulateMissingProductionsFromStart(ProductionModel start)
 {
     var visited = new HashSet<INonTerminal>();
     PopulateMissingProductionsRecursively(start, visited);
 }
Exemplo n.º 34
0
        private void AddProductionToNewOrExistingSymbolSet(ProductionModel production, SymbolModel symbol)
        {
            var set = _matrix.AddOrGetExisting(symbol.Symbol);

            set.Add(production.LeftHandSide);
        }
Exemplo n.º 35
0
 public GrammarModel(ProductionModel start)
     : this()
 {
     Start = start;
 }
Exemplo n.º 36
0
 public StartProductionSettingModel(ProductionModel productionModel)
     : this(productionModel.LeftHandSide.NonTerminal.QualifiedName)
 {
 }
Exemplo n.º 37
0
        private void AddProductionToNewOrExistingSymbolSet(ProductionModel production, SymbolModel symbol)
        {
            UniqueList<NonTerminalModel> set = null;
            if (!_matrix.TryGetValue(symbol.Symbol, out set))
            {
                set = new UniqueList<NonTerminalModel>();
                _matrix[symbol.Symbol] = set;
            }

            set.Add(production.LeftHandSide);
        }
Exemplo n.º 38
0
        private void PopulateMissingProductionsFromStart(ProductionModel start)
        {
            var visited = new HashSet <INonTerminal>();

            PopulateMissingProductionsRecursively(start, visited);
        }
Exemplo n.º 39
0
 public ProductionExpression(FullyQualifiedName fullyQualifiedName)
 {
     ProductionModel = new ProductionModel(fullyQualifiedName);
 }
Exemplo n.º 40
0
        public void GrammarModelToGrammarShouldResolverProductionReferencesFromOtherGrammars()
        {
            var S = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns1", "S")) };
            var A = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns1", "A")) };
            S.Alterations.Add(
                new AlterationModel(
                    new[] { A }));
            A.Alterations.Add(
                new AlterationModel(
                    new[] { new LexerRuleModel(
                        new StringLiteralLexerRule("a"))})
            );
            var ns1GrammarModel = new GrammarModel
            {
                Start = S
            };
            ns1GrammarModel.Productions.Add(S);
            ns1GrammarModel.Productions.Add(A);

            var ns1ProductionReferece = new ProductionReferenceModel(ns1GrammarModel.ToGrammar());

            var Z = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns2", "Z")) };
            var X = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns2", "X")) };
            X.Alterations.Add(
                new AlterationModel(
                    new SymbolModel[]
                    {
                        Z, ns1ProductionReferece
                    }));

            var ns2GrammarModel = new GrammarModel
            {
                Start = Z
            };
            ns2GrammarModel.Productions.Add(Z);
            ns2GrammarModel.Productions.Add(X);

            var ns2Grammar = ns2GrammarModel.ToGrammar();

            Assert.AreEqual(4, ns2Grammar.Productions.Count);
        }