Пример #1
0
        static EmptyGrammar()
        {
            BaseGrammar = Grammar.EmptyGrammar;
            RestrictedGrammar = new RestrictedStartSymbolGrammar(BaseGrammar);
            ExtendedGrammar = new ExtendedGrammar(RestrictedGrammar);

            EpsilonGrammaticals = new HashSet<GrammaticalSymbol>();
            EpsilonGrammaticals.Add(BaseGrammar.StartSymbol);

            FirstSets = new Dictionary<GrammaticalSymbol, ISet<TerminalSymbol>>();
            // S: #,Eps
            FirstSets[BaseGrammar.StartSymbol] = new HashSet<TerminalSymbol>(
                new TerminalSymbol[]
                {
                    ExtendedGrammar.EndOfSourceSymbol,
                    EpsilonSymbol.Instance
                });

            FollowSets = new Dictionary<GrammaticalSymbol, ISet<TerminalSymbol>>();
            // S: -
            FollowSets[BaseGrammar.StartSymbol] = new HashSet<TerminalSymbol>();

            LR0CanonicalSets = new Syntan.SyntacticAnalysis.LR.LR0CanonicalSets();
            LR0CanonicalSets.Add(new HashSet<Syntan.SyntacticAnalysis.LR.LR0Item>(
                new Syntan.SyntacticAnalysis.LR.LR0Item[]
                {
                    new Syntan.SyntacticAnalysis.LR.LR0Item(BaseGrammar.Rules[0], 0)
                }));

            // SLR1ParserTable
            SLR1ParserTable = new Syntan.SyntacticAnalysis.LR.ParserTable(1, 1, 1);
            SLR1ParserTable.SetAction(0, 0, Syntan.SyntacticAnalysis.LR.ParserTable.Action.Accept());
        }
        public void CreateWithNoStartRules()
        {
            var terminals = new TerminalSymbol[]
            {
                new TerminalSymbol("a"),
                new TerminalSymbol("b"),
            };

            var grammaticals = new GrammaticalSymbol[]
            {
                new GrammaticalSymbol("S"),
                new GrammaticalSymbol("A"),
            };

            var rules = new Rule[]
            {
                new Rule(grammaticals[1], new Symbol[]{ terminals[0] }),
                new Rule(grammaticals[1], new Symbol[]{ terminals[1] }),
            };

            var gr = new RestrictedStartSymbolGrammar(new Grammar(terminals, grammaticals, rules, 0));

            Assert.Equal(2, gr.Grammaticals.Count);
            Assert.Equal(3, gr.Rules.Count);
            Assert.Equal(gr.StartSymbol, gr.StartRule.LeftHandSide);
            Assert.True(gr.StartRule.IsEpsilonRule);
        }
        public void CreateWithMultipleStartRules()
        {
            var terminals = new TerminalSymbol[]
            {
                new TerminalSymbol("a"),
                new TerminalSymbol("b"),
            };

            var grammaticals = new GrammaticalSymbol[]
            {
                new GrammaticalSymbol("S"),
            };

            var rules = new Rule[]
            {
                new Rule(grammaticals[0], new Symbol[]{ terminals[0] }),
                new Rule(grammaticals[0], new Symbol[]{ terminals[1] }),
            };

            var gr = new RestrictedStartSymbolGrammar(new Grammar(terminals, grammaticals, rules, 0));

            Assert.Equal(2, gr.Grammaticals.Count);
            Assert.Equal(3, gr.Rules.Count);
            Assert.Equal(1, gr.Rules.Count(r => r.LeftHandSide == gr.StartSymbol));
            Assert.True(gr.Rules.FirstOrDefault(r => r.RightHandSide.IndexOf(gr.StartSymbol) != -1) == null);
        }
Пример #4
0
        /// <summary>
        /// Creates a new instance with the given <paramref name="grammar"/> as the base grammar.
        /// </summary>
        /// <param name="grammar">The grammar to extend.</param>
        public ExtendedGrammar( RestrictedStartSymbolGrammar grammar )
        {
            if( grammar == null )
                throw new ArgumentNullException("grammar");

            this.base_gr = grammar;
            this.Initialize();
        }
        public void CreateWithEmptyBaseGrammar()
        {
            var gr = new RestrictedStartSymbolGrammar(Grammar.EmptyGrammar);

            Assert.NotNull(gr.StartSymbol);
            Assert.NotNull(gr.StartRule);
            Assert.Equal(1, gr.Grammaticals.Count);
            Assert.Equal(1, gr.Rules.Count);
            Assert.True(gr.StartRule.IsEpsilonRule);
        }
Пример #6
0
        public void CreateWithValidGrammar( RestrictedStartSymbolGrammar restricted_grammar )
        {
            var eg = new ExtendedGrammar(restricted_grammar);

            Assert.NotNull(eg);
            Assert.Equal(restricted_grammar, eg.RestrictedGrammar);
            Assert.Equal(restricted_grammar.Terminals.Count + 1, eg.Terminals.Count);
            Assert.NotNull(eg.EndOfSourceSymbol);
            Assert.Equal(eg.EndOfSourceSymbol, eg.Terminals[eg.Terminals.Count - 1]);
            Assert.Equal(restricted_grammar.StartRule.RightHandSide.Count + 1, eg.StartRule.RightHandSide.Count);
            Assert.Equal(eg.EndOfSourceSymbol, eg.StartRule.RightHandSide[eg.StartRule.RightHandSide.Count - 1]);
        }
Пример #7
0
        static Grammar1()
        {
            var terminals = new TerminalSymbol[]
                {
                    new TerminalSymbol("a"),
                };

            var grammaticals = new GrammaticalSymbol[]
                {
                    new GrammaticalSymbol("S"),
                };

            var rules = new Rule[]
                {
                    // S->aS
                    new Rule(
                        grammaticals[0],
                        new Symbol[]
                        {
                            terminals[0],
                            grammaticals[0],
                        }),

                    // S->
                    new Rule(
                        grammaticals[0],
                        new Symbol[0]),
                };

            BaseGrammar = new Grammar(terminals, grammaticals, rules, 0);
            RestrictedGrammar = new RestrictedStartSymbolGrammar(BaseGrammar);
            ExtendedGrammar = new ExtendedGrammar(RestrictedGrammar);

            EpsilonGrammaticals = new HashSet<GrammaticalSymbol>(RestrictedGrammar.Grammaticals);

            FirstSets = new Dictionary<GrammaticalSymbol, ISet<TerminalSymbol>>();
            // S': a,Eps
            FirstSets[ExtendedGrammar.StartSymbol] = new HashSet<TerminalSymbol>(BaseGrammar.Terminals);
            FirstSets[ExtendedGrammar.StartSymbol].Add(EpsilonSymbol.Instance);
            // S: a,Eps
            FirstSets[BaseGrammar.StartSymbol] = new HashSet<TerminalSymbol>(BaseGrammar.Terminals);
            FirstSets[BaseGrammar.StartSymbol].Add(EpsilonSymbol.Instance);

            FollowSets = new Dictionary<GrammaticalSymbol, ISet<TerminalSymbol>>();
            // S':
            FollowSets[ExtendedGrammar.Grammaticals[0]] = new HashSet<TerminalSymbol>();
            // S: #
            FollowSets[ExtendedGrammar.Grammaticals[1]] = new HashSet<TerminalSymbol>();
            FollowSets[ExtendedGrammar.Grammaticals[1]].Add(ExtendedGrammar.EndOfSourceSymbol);
        }
Пример #8
0
        static Grammar3()
        {
            var terminals = new TerminalSymbol[]
                {
                    new TerminalSymbol("a"),
                    new TerminalSymbol("b"),
                };

            var grammaticals = new GrammaticalSymbol[]
                {
                    new GrammaticalSymbol("S"),
                    new GrammaticalSymbol("A"),
                    new GrammaticalSymbol("B"),
                };

            var rules = new Rule[]
                {
                    // S->AB
                    new Rule(
                        grammaticals[0],
                        new Symbol[]
                        {
                            grammaticals[1],
                            grammaticals[2],
                        }),

                    // S->aSbS
                    new Rule(
                        grammaticals[1],
                        new Symbol[]
                        {
                            terminals[0],
                            grammaticals[0],
                            terminals[1],
                            grammaticals[0],
                        }),

                    // A->
                    new Rule(
                        grammaticals[1],
                        new Symbol[0]),

                    // B->
                    new Rule(
                        grammaticals[2],
                        new Symbol[0]),
                };

            BaseGrammar = new Grammar(terminals, grammaticals, rules, 0);
            RestrictedGrammar = new RestrictedStartSymbolGrammar(BaseGrammar);
            ExtendedGrammar = new ExtendedGrammar(RestrictedGrammar);

            EpsilonGrammaticals = new HashSet<GrammaticalSymbol>(ExtendedGrammar.Grammaticals);

            FollowSets = new Dictionary<GrammaticalSymbol, ISet<TerminalSymbol>>();
            // S': -
            FollowSets[ExtendedGrammar.StartSymbol] = new HashSet<TerminalSymbol>();
            // S: b,#
            FollowSets[ExtendedGrammar.Grammaticals[1]] = new HashSet<TerminalSymbol>(
                new TerminalSymbol[]{
                    ExtendedGrammar.Terminals[1],
                    ExtendedGrammar.EndOfSourceSymbol,
                });
            // A: b,#
            FollowSets[ExtendedGrammar.Grammaticals[2]] = FollowSets[ExtendedGrammar.Grammaticals[1]];
            // B: b,#
            FollowSets[ExtendedGrammar.Grammaticals[3]] = FollowSets[ExtendedGrammar.Grammaticals[1]];
        }
Пример #9
0
 public void ReadWithValidData( ISet<LR0Item> set, Symbol symbol, RestrictedStartSymbolGrammar grammar, ISet<LR0Item> expected )
 {
     var actual = LR0CanonicalSets.Read(set, symbol, grammar);
     Assert.NotNull(actual);
     Assert.True(LR0CanonicalSets.AreEqual(actual, expected));
 }
Пример #10
0
 public void BuildWithValidGrammar( RestrictedStartSymbolGrammar grammar, LR0CanonicalSets expected )
 {
     var actual = LR0CanonicalSets.Build(grammar);
     Assert.NotNull(actual);
     Assert.True(actual.Equals(expected));
 }
Пример #11
0
 public void BuildWithValidGrammer( RestrictedStartSymbolGrammar grammar, ISet<GrammaticalSymbol> expected_result )
 {
     var actual_result = EpsilonGrammaticals.Build(grammar);
     Assert.True(actual_result.SetEquals(expected_result));
 }
Пример #12
0
        private void Grammar_MenuItem_Click( object sender, EventArgs e )
        {
            if( this.parser.IsParsing )
                return; //TODO: ask user?

            using( var dlg = new GrammarForm() )
            {
                dlg.Text = "Nyelvtan szerkesztő";
                dlg.Grammar = this.ext_grammar != null ? this.ext_grammar.RestrictedGrammar.BaseGrammar : null;

                if( dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK )
                {
                    try
                    {
                        var grammar = dlg.Grammar;
                        var rst_grammar = new RestrictedStartSymbolGrammar(grammar);
                        this.ext_grammar = new ExtendedGrammar(rst_grammar);
                        this.canonical_sets = SyntacticAnalysis.LR.LR0CanonicalSets.Build(rst_grammar);
                        this.epsilon_grammaticals = EpsilonGrammaticals.Build(grammar);
                        this.follow_sets = FollowSets.Build(this.ext_grammar, this.epsilon_grammaticals);
                        this.parser_table = SyntacticAnalysis.LR.SLR1ParserTableBuilder.Build(
                            this.ext_grammar, this.canonical_sets, this.follow_sets);
                    }
                    catch( Exception ex )
                    {
                        MessageBox.Show(this,
                            ex.Message, "A Nyelvtant nem tudom használni",
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        this.ext_grammar = null;
                        this.canonical_sets = null;
                        this.epsilon_grammaticals = null;
                        this.follow_sets = null;
                        this.parser_table = null;
                    }

                    // Update controls
                    if( this.ext_grammar != null )
                    {
                        this.ParserStart_ToolButton.Enabled = true;

                        this.ParserView.ClearInputHistory();

                        this.ParserTable.SetTable(this.ext_grammar, this.parser_table);

                        this.DfsmView.SetCanonicalSets(this.ext_grammar.RestrictedGrammar.BaseGrammar, this.canonical_sets);
                        this.DfsmView.Invalidate();

                        this.SyntacticTreeView.ClearNodes();
                    }
                    else
                    {
                        this.ParserStart_ToolButton.Enabled = false;
                    }
                }
            }
        }
Пример #13
0
        static Grammar2()
        {
            var terminals = new TerminalSymbol[]
                {
                    new TerminalSymbol("+"),
                    new TerminalSymbol("*"),
                    new TerminalSymbol("("),
                    new TerminalSymbol(")"),
                    new TerminalSymbol("i"),
                };

            var grammaticals = new GrammaticalSymbol[]
                {
                    new GrammaticalSymbol("S"),
                    new GrammaticalSymbol("E"),
                    new GrammaticalSymbol("E'"),
                    new GrammaticalSymbol("T"),
                    new GrammaticalSymbol("T'"),
                    new GrammaticalSymbol("F"),
                };

            var rules = new Rule[]
                {
                    // S->E
                    new Rule(
                        grammaticals[0],
                        new Symbol[]
                        {
                            grammaticals[1],
                        }),

                    // E->TE'
                    new Rule(
                        grammaticals[1],
                        new Symbol[]
                        {
                            grammaticals[3],
                            grammaticals[2],
                        }),

                    // E'->+TE'
                    new Rule(
                        grammaticals[2],
                        new Symbol[]
                        {
                            terminals[0],
                            grammaticals[3],
                            grammaticals[2],
                        }),

                    // E'->
                    new Rule(
                        grammaticals[2],
                        new Symbol[] { }),

                    // T->FT'
                    new Rule(
                        grammaticals[3],
                        new Symbol[]
                        {
                            grammaticals[5],
                            grammaticals[4],
                        }),

                    // T'->*FT'
                    new Rule(
                        grammaticals[4],
                        new Symbol[]
                        {
                            terminals[1],
                            grammaticals[5],
                            grammaticals[4],
                        }),

                    // T'->
                    new Rule(
                        grammaticals[4],
                        new Symbol[] { }),

                    // F->(E)
                    new Rule(
                        grammaticals[5],
                        new Symbol[]
                        {
                            terminals[2],
                            grammaticals[1],
                            terminals[3],
                        }),

                    // F->i
                    new Rule(
                        grammaticals[5],
                        new Symbol[]
                        {
                            terminals[4],
                        }),
                };

            BaseGrammar = new Grammar(terminals, grammaticals, rules, 0);
            RestrictedGrammar = new RestrictedStartSymbolGrammar(BaseGrammar);
            ExtendedGrammar = new ExtendedGrammar(RestrictedGrammar);

            EpsilonGrammaticals = new HashSet<GrammaticalSymbol>(
                new GrammaticalSymbol[] { grammaticals[2], grammaticals[4] });

            FirstSets = new Dictionary<GrammaticalSymbol, ISet<TerminalSymbol>>();
            // S: (,i
            FirstSets[BaseGrammar.Grammaticals[0]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    BaseGrammar.Terminals[2],
                    BaseGrammar.Terminals[4],
                });

            // E: (,i
            FirstSets[BaseGrammar.Grammaticals[1]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    BaseGrammar.Terminals[2],
                    BaseGrammar.Terminals[4],
                });

            // E': +,Eps
            FirstSets[BaseGrammar.Grammaticals[2]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    BaseGrammar.Terminals[0],
                    EpsilonSymbol.Instance,
                });

            // T: (,i
            FirstSets[BaseGrammar.Grammaticals[3]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    BaseGrammar.Terminals[2],
                    BaseGrammar.Terminals[4],
                });

            // T': *,Eps
            FirstSets[BaseGrammar.Grammaticals[4]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    BaseGrammar.Terminals[1],
                    EpsilonSymbol.Instance,
                });

            // F: (,i
            FirstSets[BaseGrammar.Grammaticals[5]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    BaseGrammar.Terminals[2],
                    BaseGrammar.Terminals[4],
                });

            FollowSets = new Dictionary<GrammaticalSymbol, ISet<TerminalSymbol>>();

            // S:
            FollowSets[ExtendedGrammar.Grammaticals[0]] = new HashSet<TerminalSymbol>();

            // E: ),EoS
            FollowSets[ExtendedGrammar.Grammaticals[1]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    ExtendedGrammar.Terminals[3],
                    ExtendedGrammar.Terminals[5],
                });

            // E': ),EoS
            FollowSets[ExtendedGrammar.Grammaticals[2]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    ExtendedGrammar.Terminals[3],
                    ExtendedGrammar.Terminals[5],
                });

            // T: +,),EoS
            FollowSets[ExtendedGrammar.Grammaticals[3]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    ExtendedGrammar.Terminals[0],
                    ExtendedGrammar.Terminals[3],
                    ExtendedGrammar.Terminals[5],
                });

            // T': +,),EoS
            FollowSets[ExtendedGrammar.Grammaticals[4]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    ExtendedGrammar.Terminals[0],
                    ExtendedGrammar.Terminals[3],
                    ExtendedGrammar.Terminals[5],
                });

            // F: +,*,),EoS
            FollowSets[ExtendedGrammar.Grammaticals[5]] = new HashSet<TerminalSymbol>(new TerminalSymbol[]
                {
                    ExtendedGrammar.Terminals[0],
                    ExtendedGrammar.Terminals[1],
                    ExtendedGrammar.Terminals[3],
                    ExtendedGrammar.Terminals[5],
                });

            //TODO: Use real fixture!!
            LR0CanonicalSets = LR0CanonicalSets.Build(RestrictedGrammar);

            //TODO: Use real fixtures!!
            SLR1ParserTable = SLR1ParserTableBuilder.Build(ExtendedGrammar, LR0CanonicalSets, FollowSets);
        }
Пример #14
0
 ExtendedGrammar( SerializationInfo info, StreamingContext context )
 {
     this.base_gr = (RestrictedStartSymbolGrammar)info.GetValue("BaseGrammar", typeof(RestrictedStartSymbolGrammar));
     this.Initialize();
 }
        public void CreateWithStartRuleNotTheFirst()
        {
            var terminals = new TerminalSymbol[]
            {
                new TerminalSymbol("a"),
                new TerminalSymbol("b"),
            };

            var grammaticals = new GrammaticalSymbol[]
            {
                new GrammaticalSymbol("S"),
                new GrammaticalSymbol("A"),
            };

            var rules = new Rule[]
            {
                new Rule(grammaticals[1], new Symbol[]{ terminals[0] }),
                new Rule(grammaticals[0], new Symbol[]{ terminals[1] }),
            };

            var gr = new RestrictedStartSymbolGrammar(new Grammar(terminals, grammaticals, rules, 0));

            Assert.Equal(2, gr.Grammaticals.Count);
            Assert.Equal(2, gr.Rules.Count);
            Assert.Equal(rules[1], gr.StartRule);
        }