Пример #1
0
        //private static IEnumerable<Production> Productions() {
        //	var retval = Letter();
        //	retval = retval.Concat(DecimalDigit());
        //	retval = retval.Concat(BasicSymbols());
        //	retval = retval.Concat(SecondPart());
        //	retval = retval.Concat(ThirdPart());
        //	retval = retval.Concat(FinalPart());

        //	return retval;
        //}
        private static IEnumerable <Production> Annotations()
        {
            return(new List <Production> {
                new Production("SyntacticTerm", new Sentence {
                    Nonterminal.Of("SyntacticFactor"),
                    Terminal.Of("("),
                    Nonterminal.Of("Annotations"),
                    Terminal.Of(")"),
                }),
                new Production("Annotations", new Sentence {
                }),
                new Production("Annotations", new Sentence {
                    Nonterminal.Of("Annotation"),
                    Nonterminal.Of("AnnotationWithSepList0"),
                }),
                new Production("AnnotationWithSep", new Sentence {
                    Terminal.Of(","),
                    Nonterminal.Of("Annotation"),
                }),
                new Production("Annotation", new Sentence {
                    Nonterminal.Of("MetaIdentifier"),
                    Terminal.Of("("),
                    Nonterminal.Of("AnnotationArgs"),
                    Terminal.Of(")")
                }),
                new Production("AnnotationArgs", new Sentence {
                }),
                new Production("AnnotationArgs", new Sentence {
                    Nonterminal.Of("AnnotationArg"),
                    Nonterminal.Of("AnnotationArgWithSepList0"),
                }),
                new Production("AnnotationArgWithSep", new Sentence {
                    Terminal.Of(","),
                    Nonterminal.Of("AnnotationArg"),
                }),
                new Production("AnnotationArg", new Sentence {
                    Nonterminal.Of("TerminalString"),
                }),
                new Production("AnnotationArg", new Sentence {
                    Nonterminal.Of("Integer"),
                }),
            }.Concat(
                       MakeList <object>("AnnotationWithSep", 0)
                       ).Concat(
                       MakeList <object>("AnnotationArgWithSep", 0)
                       ));
        }
Пример #2
0
        private static IEnumerable <Production> BasicSymbols()
        {
            return(new List <Production> {
                new Production("Integer", Nonterminal.Of("DecimalDigitList1")),

                new Production("ConcatenateSymbol", Terminal.Of(",")),
                new Production("DefiningSymbol", Terminal.Of("=")),
                new Production("DefinitionSeparatorSymbol", Terminal.Of("|")),
                new Production("DefinitionSeparatorSymbol", Terminal.Of("/")),
                new Production("DefinitionSeparatorSymbol", Terminal.Of("!")),
                new Production("EndCommentSymbol", Terminal.Of("*)")),
                new Production("EndGroupSymbol", Terminal.Of(")")),
                new Production("EndOptionSymbol", Terminal.Of("]")),
                new Production("EndOptionSymbol", Terminal.Of("/)")),
                new Production("EndRepeatSymbol", Terminal.Of("}")),
                new Production("EndRepeatSymbol", Terminal.Of(":)")),
                new Production("ExceptSymbol", Terminal.Of("-")),
                new Production("FirstQuoteSymbol", Terminal.Of("'")),
                new Production("RepetitionSymbol", Terminal.Of("*")),
                new Production("SecondQuoteSymbol", Terminal.Of("\"")),
                new Production("SpecialSequenceSymbol", Terminal.Of("?")),
                new Production("StartCommentSymbol", Terminal.Of("(*")),
                new Production("StartGroupSymbol", Terminal.Of("(")),
                new Production("StartOptionSymbol", Terminal.Of("[")),
                new Production("StartOptionSymbol", Terminal.Of("(/")),
                new Production("StartRepeatSymbol", Terminal.Of("{")),
                new Production("StartRepeatSymbol", Terminal.Of("(:")),
                new Production("TerminatorSymbol", Terminal.Of(";")),
                new Production("TerminatorSymbol", Terminal.Of(".")),

                new Production("SpaceCharacter", Terminal.Of(" ")),
                new Production("HorizontalTabulationCharacter", Terminal.Of("\t")),
                new Production("NewLine", Terminal.Of("\n")),
                new Production("CarriageReturn", Terminal.Of("\r")),
                //new Production("NewLine", Terminal.Of("\r\n")),
                new Production("VerticalTabulationCharacter", Terminal.Of("\v")),
                new Production("FormFeed", Terminal.Of("\f")),
            }.Concat(
                       ":+_%@&#$<>\\^`~".Select((x) => new Production("OtherCharacter", Terminal.Of(x.ToString())))
                       ).Concat(
                       MakeList <object>("DecimalDigit", 1)
                       ));
            // !, ., and / are defined elsewhere
            // including space seems like a bug
        }
Пример #3
0
        /*
         * Based on https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form#Further_examples
         * This work was released under CC-BY-SA (https://creativecommons.org/licenses/by-sa/3.0/)
         *
         * <syntax>         ::= <rule> | <rule> <syntax>
         * <rule>           ::= <opt-whitespace> "<" <rule-name> ">" <opt-whitespace> "::=" <opt-whitespace> <expression> <line-end>
         * <opt-whitespace> ::= " " <opt-whitespace> | ""
         * <expression>     ::= <list> | <list> <opt-whitespace> "|" <opt-whitespace> <expression>
         * <line-end>       ::= <opt-whitespace> <EOL> | <line-end> <line-end>
         * <list>           ::= <term> | <term> <opt-whitespace> <list>
         * <term>           ::= <literal> | "<" <rule-name> ">"
         * <literal>        ::= '"' <text1> '"' | "'" <text2> "'"
         * <text1>          ::= "" | <character1> <text1>
         * <text2>          ::= "" | <character2> <text2>
         * <character>      ::= <letter> | <digit> | <symbol>
         * <letter>         ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
         * <digit>          ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
         * <symbol>         ::=  "|" | " " | "-" | "!" | "#" | "$" | "%" | "&" | "(" | ")" | "*" | "+" | "," | "-" | "." | "/" | ":" | ";" | ">" | "=" | "<" | "?" | "@" | "[" | "\" | "]" | "^" | "_" | "`" | "{" | "}" | "~"
         * <character1>     ::= <character> | "'"
         * <character2>     ::= <character> | '"'
         * <rule-name>      ::= <letter> | <rule-name> <rule-char>
         * <rule-char>      ::= <letter> | <digit> | "-"
         */
        public static Grammar Grammar()
        {
            var productions = new List <Production> {
                new Production(
                    Nonterminal.Of("syntax"),
                    new Sentence {
                    Nonterminal.Of("rule"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => ((IEnumerable <Production>)args[0].Payload).ToList())
                })
                    ),
                new Production(
                    Nonterminal.Of("syntax"),
                    new Sentence {
                    Nonterminal.Of("rule"),
                    Nonterminal.Of("syntax"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => {
                        var list = (List <Production>)args[1].Payload;
                        list.AddRange((IEnumerable <Production>)args[0].Payload);
                        return(list);
                    })
                })
                    ),

                new Production(Nonterminal.Of("opt_comments"), new Sentence {
                }),
                new Production(Nonterminal.Of("opt_comments"), new Sentence {
                    Nonterminal.Of("comment"),
                    Nonterminal.Of("opt_comments"),
                }),

                new Production(
                    Nonterminal.Of("rule"),
                    new Sentence {
                    Nonterminal.Of("opt_comments"),
                    Nonterminal.Of("opt_whitespace"),
                    Terminal.Of("<"),
                    Nonterminal.Of("rule_name"),
                    Terminal.Of(">"),
                    Nonterminal.Of("opt_whitespace"),
                    Terminal.Of(":"),
                    Terminal.Of(":"),
                    Terminal.Of("="),
                    Nonterminal.Of("opt_whitespace"),
                    Nonterminal.Of("expression"),
                    Nonterminal.Of("line_end"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation((args) => {
                        var lhs  = Nonterminal.Of((string)args[3].Payload);
                        var rhss = (List <Sentence>)args[10].Payload;

                        return(rhss.Select(rhs => new Production(lhs, rhs)));
                    })
                })
                    ),

                new Production(Nonterminal.Of("comment"), new Sentence {
                    Nonterminal.Of("opt_whitespace"),
                    Terminal.Of("/"),
                    Terminal.Of("/"),
                    Nonterminal.Of("comment_text"),
                    Nonterminal.Of("line_end"),
                }),                 // ambiguous with space at end

                new Production(Nonterminal.Of("opt_whitespace"), new Sentence {
                    Terminal.Of(" "),
                    Nonterminal.Of("opt_whitespace"),
                }),
                new Production(Nonterminal.Of("opt_whitespace"), new Sentence {
                }),

                new Production(
                    Nonterminal.Of("expression"),
                    new Sentence {
                    Nonterminal.Of("list"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => new List <Sentence> {
                        (Sentence)args[0].Payload
                    })
                })
                    ),
                new Production(
                    Nonterminal.Of("expression"),
                    new Sentence {
                    Nonterminal.Of("list"),
                    Nonterminal.Of("opt_whitespace"),
                    Terminal.Of("|"),
                    Nonterminal.Of("opt_whitespace"),
                    Nonterminal.Of("expression"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => {
                        var list = (List <Sentence>)args[4].Payload;
                        list.Add((Sentence)args[0].Payload);
                        return(list);
                    })
                })
                    ),

                new Production(Nonterminal.Of("line_end"), new Sentence {
                    Nonterminal.Of("opt_whitespace"),
                    Nonterminal.Of("EOL"),
                }),
                new Production(Nonterminal.Of("line_end"), new Sentence {
                    Nonterminal.Of("line_end"),
                    Nonterminal.Of("line_end"),
                }),

                new Production(
                    Nonterminal.Of("list"),
                    new Sentence {
                    Nonterminal.Of("term"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => new Sentence((Word)args[0].Payload))
                })
                    ),
                new Production(
                    Nonterminal.Of("list"),
                    new Sentence {
                    Nonterminal.Of("term"),
                    Nonterminal.Of("opt_whitespace"),
                    Nonterminal.Of("list"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => {
                        var sentence = (Sentence)args[2].Payload;
                        sentence.Add((Word)args[0].Payload);
                        return(sentence);
                    })
                })
                    ),

                new Production(Nonterminal.Of("term"), new Sentence {
                    Nonterminal.Of("literal"),
                }),
                new Production(
                    Nonterminal.Of("term"),
                    new Sentence {
                    Terminal.Of("<"),
                    Nonterminal.Of("rule_name"),
                    Terminal.Of(">"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => Nonterminal.Of((string)args[1].Payload))
                })
                    ),

                new Production(
                    Nonterminal.Of("literal"),
                    new Sentence {
                    Terminal.Of("\""),
                    Nonterminal.Of("text1"),
                    Terminal.Of("\""),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => Terminal.Of((string)args[1].Payload))
                })
                    ),
                new Production(
                    Nonterminal.Of("literal"),
                    new Sentence {
                    Terminal.Of("'"),
                    Nonterminal.Of("text2"),
                    Terminal.Of("'"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => Terminal.Of((string)args[1].Payload))
                })
                    ),

                new Production(Nonterminal.Of("comment_text"), new Sentence {
                }),
                new Production(Nonterminal.Of("comment_text"), new Sentence {
                    Nonterminal.Of("comment_char"),
                    Nonterminal.Of("comment_text"),
                }),

                new Production(Nonterminal.Of("text1"), new Sentence {
                }),
                new Production(Nonterminal.Of("text1"), new Sentence {
                    Nonterminal.Of("character1"),
                    Nonterminal.Of("text1"),
                }),

                new Production(Nonterminal.Of("text2"), new Sentence {
                }),
                new Production(Nonterminal.Of("text2"), new Sentence {
                    Nonterminal.Of("character2"),
                    Nonterminal.Of("text2"),
                }),

                new Production(Nonterminal.Of("character"), new Sentence {
                    Nonterminal.Of("letter"),
                }),
                new Production(Nonterminal.Of("character"), new Sentence {
                    Nonterminal.Of("digit"),
                }),
                new Production(Nonterminal.Of("character"), new Sentence {
                    Nonterminal.Of("symbol"),
                }),

                new Production(Nonterminal.Of("comment_char"), new Sentence {
                    Nonterminal.Of("character"),
                }),
                new Production(Nonterminal.Of("comment_char"), new Sentence {
                    Terminal.Of("\""),
                }),
                new Production(Nonterminal.Of("comment_char"), new Sentence {
                    Terminal.Of("'"),
                }),

                // letters
                // digits
                // symbols

                new Production(Nonterminal.Of("character1"), new Sentence {
                    Nonterminal.Of("character"),
                }),
                new Production(Nonterminal.Of("character1"), new Sentence {
                    Terminal.Of("'"),
                }),

                new Production(Nonterminal.Of("character2"), new Sentence {
                    Nonterminal.Of("character"),
                }),
                new Production(Nonterminal.Of("character2"), new Sentence {
                    Terminal.Of("\""),
                }),

                new Production(Nonterminal.Of("rule_name"), new Sentence {
                    Nonterminal.Of("letter"),
                }),
                new Production(Nonterminal.Of("rule_name"), new Sentence {
                    Nonterminal.Of("rule_name"),
                    Nonterminal.Of("rule_char"),
                }),

                new Production(Nonterminal.Of("rule_char"), new Sentence {
                    Nonterminal.Of("letter"),
                }),
                new Production(Nonterminal.Of("rule_char"), new Sentence {
                    Nonterminal.Of("digit"),
                }),
                new Production(Nonterminal.Of("rule_char"), new Sentence {
                    Terminal.Of("_"),
                }),
                new Production(Nonterminal.Of("rule_char"), new Sentence {
                    Terminal.Of("-"),
                }),

                new Production(Nonterminal.Of("EOL"), new Sentence {
                    Terminal.Of("\n"),
                }),
                new Production(Nonterminal.Of("EOL"), new Sentence {
                    Terminal.Of("\r"),
                }),
            };

            foreach (var letter in new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" })
            {
                productions.Add(
                    new Production(Nonterminal.Of("letter"), new Sentence {
                    Terminal.Of(letter),
                })
                    );
            }
            foreach (var digit in new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" })
            {
                productions.Add(
                    new Production(Nonterminal.Of("digit"), new Sentence {
                    Terminal.Of(digit),
                })
                    );
            }
            foreach (var symbol in new string[] { "|", " ", "-", "!", "#", "$", "%", "&", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", ">", "=", "<", "?", "@", "[", "\\", "]", "^", "_", "`", "{", "}", "~" })
            {
                productions.Add(
                    new Production(Nonterminal.Of("symbol"), new Sentence {
                    Terminal.Of(symbol),
                })
                    );
            }

            var start = Nonterminal.Of("syntax");
            var g     = new Grammar(productions, start);

            return(g);
        }
Пример #4
0
        private static IEnumerable <Production> SecondPart()
        {
            return(new List <Production> {
                new Production("TerminalCharacter", Nonterminal.Of("TerminalCharacterBasic")),
                new Production("TerminalCharacter", Nonterminal.Of("FirstQuoteSymbol")),
                new Production("TerminalCharacter", Nonterminal.Of("SecondQuoteSymbol")),
                new Production("TerminalCharacter", Nonterminal.Of("SpaceCharacter")),

                new Production("GapFreeSymbolTerminal", Nonterminal.Of("TerminalCharacterBasic")),
                // new Production("GapFreeSymbolTerminal", Nonterminal.Of("SpecialSequenceSymbol")),

                new Production(
                    Nonterminal.Of("GapFreeSymbol"),
                    new Sentence {
                    Nonterminal.Of("GapFreeSymbolTerminal")
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => new List <Terminal> {
                        Terminal.Of((string)args[0].Payload)
                    })
                })
                    ),
                new Production(
                    Nonterminal.Of("GapFreeSymbol"),
                    new Sentence {
                    Nonterminal.Of("TerminalString")
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => {
                        var tup = (Tuple <string, List <Terminal> >)args[0].Payload;
                        // TODO shouldn't mutate
                        var list = tup.Item2;
                        list.Insert(0, Terminal.Of(tup.Item1));
                        list.Add(Terminal.Of(tup.Item1));
                        return list;
                    })
                })
                    ),

                new Production("GapSeparator", Nonterminal.Of("SpaceCharacter")),
                new Production("GapSeparator", Nonterminal.Of("HorizontalTabulationCharacter")),
                new Production("GapSeparator", Nonterminal.Of("NewLine")),
                new Production("GapSeparator", Nonterminal.Of("CarriageReturn")),
                new Production("GapSeparator", Nonterminal.Of("VerticalTabulationCharacter")),
                new Production("GapSeparator", Nonterminal.Of("FormFeed")),

                // TODO
                new Production(
                    Nonterminal.Of("SyntaxLayout"),
                    new Sentence {
                    Nonterminal.Of("GapSeparatorList0"),
                    Nonterminal.Of("GapFreeSymbolWithOptionalGapSeparatorList1"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => ((List <List <Terminal> >)args[1].Payload).SelectMany(term => term).ToList())
                })
                    ),

                new Production(
                    Nonterminal.Of("GapFreeSymbolWithOptionalGapSeparator"),
                    new Sentence {
                    Nonterminal.Of("GapFreeSymbol"),
                    Nonterminal.Of("GapSeparatorList0"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => (List <Terminal>)args[0].Payload)
                })
                    ),
            }.Concat(
                       MakeList <object>("GapSeparator", 0)
                       ).Concat(
                       MakeList <List <Terminal> >("GapFreeSymbolWithOptionalGapSeparator", 1)
                       ));
        }
Пример #5
0
        private static IEnumerable <Production> Terminals()
        {
            return(new List <Production> {
                new Production(
                    Nonterminal.Of("TerminalString"),
                    new Sentence {
                    Nonterminal.Of("FirstQuoteSymbol"),
                    Nonterminal.Of("FirstTerminalCharacterList1"),
                    Nonterminal.Of("FirstQuoteSymbol"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => Tuple.Create("'", ((List <string>)args[1].Payload).Select(x => Terminal.Of(x)).ToList()))
                })
                    ),
                new Production(
                    Nonterminal.Of("TerminalString"),
                    new Sentence {
                    Nonterminal.Of("SecondQuoteSymbol"),
                    Nonterminal.Of("SecondTerminalCharacterList1"),
                    Nonterminal.Of("SecondQuoteSymbol"),
                },
                    1,
                    new Annotations(new List <IAnnotation> {
                    new ActionAnnotation(args => Tuple.Create("\"", ((List <string>)args[1].Payload).Select(x => Terminal.Of(x)).ToList()))
                })
                    ),

                new Production("TerminalCharacterBasic", Nonterminal.Of("Letter")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("DecimalDigit")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("CommentlessTerminal")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("ConcatenateSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("DefiningSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("DefinitionSeparatorSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("EndCommentSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("EndGroupSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("EndOptionSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("EndRepeatSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("ExceptSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("RepetitionSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("SpecialSequenceSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("StartCommentSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("StartGroupSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("StartOptionSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("StartRepeatSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("TerminatorSymbol")),
                new Production("TerminalCharacterBasic", Nonterminal.Of("OtherCharacter")),

                new Production("FirstTerminalCharacter", Nonterminal.Of("TerminalCharacterBasic")),
                new Production("FirstTerminalCharacter", Nonterminal.Of("SpaceCharacter")),
                new Production("FirstTerminalCharacter", Nonterminal.Of("SecondQuoteSymbol")),

                new Production("SecondTerminalCharacter", Nonterminal.Of("TerminalCharacterBasic")),
                new Production("SecondTerminalCharacter", Nonterminal.Of("SpaceCharacter")),
                new Production("SecondTerminalCharacter", Nonterminal.Of("FirstQuoteSymbol")),
            }.Concat(
                       MakeList <string>("FirstTerminalCharacter", 1)
                       ).Concat(
                       MakeList <string>("SecondTerminalCharacter", 1)
                       ));
        }