示例#1
0
        static BnfGrammar()
        {
            /*
             *  <grammar>        ::= <rule> | <rule> <grammar>
             *  <rule>           ::= "<" <rule-name> ">" "::=" <expression>
             *  <expression>     ::= <list> | <list> "|" <expression>
             *  <line-end>       ::= <EOL> | <line-end> <line-end>
             *  <list>           ::= <term> | <term> <list>
             *  <term>           ::= <literal> | "<" <rule-name> ">"
             *  <literal>        ::= '"' <text> '"' | "'" <text> "'"
             */
            var whitespace = CreateWhitespaceLexerRule();
            var ruleName = CreateRuleNameLexerRule();
            var implements = CreateImplementsLexerRule();
            var eol = CreateEndOfLineLexerRule();
            var notDoubleQuote = CreateNotDoubleQuoteLexerRule();
            var notSingleQuuote = CreateNotSingleQuoteLexerRule();

            var grammar = new NonTerminal("grammar");
            var rule = new NonTerminal("rule");
            var identifier = new NonTerminal("identifier");
            var expression = new NonTerminal("expression");
            var lineEnd = new NonTerminal("line-end");
            var list = new NonTerminal("list");
            var term = new NonTerminal("term");
            var literal = new NonTerminal("literal");
            var doubleQuoteText = new NonTerminal("doubleQuoteText");
            var singleQuoteText = new NonTerminal("singleQuoteText");

            var lessThan = new TerminalLexerRule('<');
            var greaterThan = new TerminalLexerRule('>');
            var doubleQuote = new TerminalLexerRule('"');
            var slash = new TerminalLexerRule('\'');
            var pipe = new TerminalLexerRule('|');

            var productions = new[]
            {
                new Production(grammar, rule),
                new Production(grammar, rule, grammar),
                new Production(rule, identifier, implements, expression),
                new Production(expression, list),
                new Production(expression, list, pipe, expression),
                new Production(lineEnd, eol),
                new Production(lineEnd, lineEnd, lineEnd),
                new Production(list, term),
                new Production(list, term, list),
                new Production(term, literal),
                new Production(term, identifier),
                new Production(identifier, lessThan, ruleName, greaterThan),
                new Production(literal, doubleQuote, notDoubleQuote, doubleQuote),
                new Production(literal, slash, notSingleQuuote, slash)
            };

            var ignore = new[]
            {
                whitespace
            };

            _bnfGrammar = new Grammar(grammar, productions, ignore);
        }
示例#2
0
        static BnfGrammar()
        {
            /*
             *  Grammar
             *  -------
             *  <syntax>         ::= <rule> | <rule> <syntax>
             *  <rule>           ::= "<" <rule-name> ">" "::=" <expression>
             *  <expression>     ::= <list> | <list> "|" <expression>
             *  <line-end>       ::= <EOL> | <line-end> <line-end>
             *  <list>           ::= <term> | <term> <list>
             *  <term>           ::= <literal> | "<" <rule-name> ">"
             *  <literal>        ::= '"' <text> '"' | "'" <text> "'"
             */
            var whitespace = CreateWhitespaceLexerRule();
            var ruleName = CreateRuleNameLexerRule();
            var implements = CreateImplementsLexerRule();
            var eol = CreateEndOfLineLexerRule();
            var notDoubleQuote = CreateNotDoubleQuoteLexerRule();
            var notSingleQuuote = CreateNotSingleQuoteLexerRule();

            var syntax = new NonTerminal("syntax");
            var rule = new NonTerminal("rule");
            var identifier = new NonTerminal("identifier");
            var expression = new NonTerminal("expression");
            var lineEnd = new NonTerminal("line-end");
            var list = new NonTerminal("list");
            var term = new NonTerminal("term");
            var literal = new NonTerminal("literal");
            var doubleQuoteText = new NonTerminal("doubleQuoteText");
            var singleQuoteText = new NonTerminal("singleQuoteText");

            var productions = new[]
            {
                new Production(syntax, rule),
                new Production(syntax, rule, syntax),
                new Production(rule, identifier, implements, expression),
                new Production(expression, list),
                new Production(expression, list, new TerminalLexerRule('|'), expression),
                new Production(lineEnd, eol),
                new Production(lineEnd, lineEnd, lineEnd),
                new Production(list, term),
                new Production(list, term, list),
                new Production(term, literal),
                new Production(term, identifier),
                new Production(identifier, new TerminalLexerRule('<'), ruleName, new TerminalLexerRule('>')),
                new Production(literal, new TerminalLexerRule('"'), notDoubleQuote, new TerminalLexerRule('"')),
                new Production(literal, new TerminalLexerRule('\''), notSingleQuuote, new TerminalLexerRule('\''))
            };

            var ignore = new[]
            {
                whitespace
            };

            _bnfGrammar = new Grammar(syntax, productions, new ILexerRule[] { }, ignore);
        }
示例#3
0
 public void Test_Grammar_That_RulesFor_Returns_Rules_When_Production_Matches()
 {
     var B = new NonTerminal("B");
     var A = new NonTerminal("A");
     var S = new NonTerminal("S");
     var grammarBuilder = new GrammarBuilder("S")
         .Production("S", r => r
             .Rule("A")
             .Rule("B"))
         .Production("A", r => r
             .Rule('a'))
         .Production("B", r => r
             .Rule('b'));
     var grammar = grammarBuilder.ToGrammar();
     var rules = grammar.RulesFor(A).ToList();
     Assert.AreEqual(1, rules.Count);
     Assert.AreEqual("A", rules[0].LeftHandSide.Value);
 }
示例#4
0
 public IRuleBuilder Rule(params object[] symbols)
 {
     var symbolList = new List<ISymbol>();
     if (symbols != null)
     {
         foreach (var symbol in symbols)
         {
             if (symbol is char)
             {
                 var terminal = new Terminal((char)symbol);
                 var lexerRule = new TerminalLexerRule(
                     terminal,
                     new TokenType(terminal.ToString()));
                 symbolList.Add(lexerRule);
             }
             else if (symbol is ITerminal)
             {
                 var terminal = symbol as ITerminal;
                 var lexerRule = new TerminalLexerRule(
                     terminal,
                     new TokenType(terminal.ToString()));
                 symbolList.Add(lexerRule);
             }
             else if (symbol is ILexerRule)
             {
                 symbolList.Add(symbol as ILexerRule);
             }
             else if (symbol is string)
             {
                 var nonTerminal = new NonTerminal(symbol as string);
                 symbolList.Add(nonTerminal);
             }
             else if (symbol == null)
             { }
             else { throw new ArgumentException("unrecognized terminal or nonterminal"); }
         }
     }
     _rules.Add(symbolList);
     return this;
 }
示例#5
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;
        }
示例#6
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;
        }
示例#7
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;
        }
示例#8
0
        static RegexGrammar()
        {
            var notMeta = CreateNotMetaLexerRule();
            var notCloseBracket = CreateNotCloseBracketLexerRule();
            var escape = CreateEscapeCharacterLexerRule();

            var regex = new NonTerminal(Regex);
            var expression = new NonTerminal(Expression);
            var term = new NonTerminal(Term);
            var factor = new NonTerminal(Factor);
            var atom = new NonTerminal(Atom);
            var iterator = new NonTerminal(Iterator);
            var set = new NonTerminal(Set);
            var positiveSet = new NonTerminal(PositiveSet);
            var negativeSet = new NonTerminal(NegativeSet);
            var characterClass = new NonTerminal(CharacterClass);
            var characterRange = new NonTerminal(CharacterRange);
            var character = new NonTerminal(Character);
            var characterClassCharacter = new NonTerminal(CharacterClassCharacter);

            var caret = new TerminalLexerRule('^');
            var dollar = new TerminalLexerRule('$');
            var pipe = new TerminalLexerRule('|');
            var dot = new TerminalLexerRule('.');
            var openParen = new TerminalLexerRule('(');
            var closeParen = new TerminalLexerRule(')');
            var star = new TerminalLexerRule('*');
            var plus = new TerminalLexerRule('+');
            var question = new TerminalLexerRule('?');
            var openBracket = new TerminalLexerRule('[');
            var closeBracket = new TerminalLexerRule(']');
            var minus = new TerminalLexerRule('-');

            var productions = new[]
            {
                new Production(regex, expression),
                new Production(regex, caret, expression),
                new Production(regex, expression, dollar),
                new Production(regex, caret, expression, dollar),
                new Production(expression, term),
                new Production(expression, term, pipe, expression),
                new Production(term, factor),
                new Production(term, factor, term),
                new Production(factor, atom),
                new Production(factor, atom, iterator),
                new Production(atom, dot),
                new Production(atom, character),
                new Production(atom, openParen, expression, closeParen),
                new Production(atom, set),
                new Production(iterator, star),
                new Production(iterator, plus),
                new Production(iterator, question),
                new Production(set, positiveSet),
                new Production(set, negativeSet),
                new Production(positiveSet, openBracket, characterClass, closeBracket),
                new Production(negativeSet, openBracket, caret, characterClass, closeBracket),
                new Production(characterClass, characterRange),
                new Production(characterClass, characterRange, characterClass),
                new Production(characterRange, characterClassCharacter),
                new Production(characterRange, characterClassCharacter, minus, characterClassCharacter),
                new Production(character, notMeta),
                new Production(character, escape),
                new Production(characterClassCharacter, notCloseBracket),
                new Production(characterClassCharacter, escape)
            };

            _regexGrammar = new Grammar(regex, productions, null);
        }
示例#9
0
        public IRuleBuilder Rule1(params object[] symbols)
        {
            var symbolList = new List<ISymbol>();
            var terminalNeighborList = new List<ITerminal>();

            if (symbols.IsNullOrEmpty())
                return this;

            foreach (var symbol in symbols)
            {
                var flushTerminals = false;
                if (symbol is INonTerminal)
                {
                    flushTerminals = true;
                    symbolList.Add(symbol as INonTerminal);
                }
                else if (symbol is string)
                {
                    flushTerminals = true;
                    var nonTerminal = new NonTerminal(symbol as string);
                    symbolList.Add(nonTerminal);
                }
                else if (symbol is char)
                {
                    var terminal = new Terminal((char)symbol);
                    terminalNeighborList.Add(terminal);
                }
                else if (symbol is ITerminal)
                {
                    terminalNeighborList.Add(symbol as ITerminal);
                }
                else if (symbol is ILexerRule)
                {
                    flushTerminals = true;
                    symbolList.Add(symbol as ILexerRule);
                }
                else if (symbol == null)
                { }
                else { throw new ArgumentException("unrecognized terminal or nonterminal"); }

                if (flushTerminals)
                {
                    var grammarLexerRule = CreateGrammarLexerRule(terminalNeighborList);
                    symbolList.Add(grammarLexerRule);
                    terminalNeighborList.Clear();
                }
            }

            if (terminalNeighborList.Count > 0)
            {
                var grammarLexerRule = CreateGrammarLexerRule(terminalNeighborList);
                symbolList.Add(grammarLexerRule);
                terminalNeighborList.Clear();
            }

            _rules.Add(symbolList);
            return this;
        }
示例#10
0
 private IGrammarLexerRule CreateGrammarLexerRule(IList<ITerminal> terminalNeighborList)
 {
     var startNonTerminal = new NonTerminal("S");
     var production = new Production(
         startNonTerminal,
         terminalNeighborList.ToArray());
     var grammar = new Grammar(startNonTerminal, new[] { production }, null, null);
     return new GrammarLexerRule(Guid.NewGuid().ToString(), grammar);
 }