Exemple #1
0
        public ConfigurableLrTable(ILrDfa dfa, RuntimeOptions flags)
        {
            this.grammar = dfa.GrammarAnalysis;

            this.data = new MutableTable <int>(dfa.States.Length, grammar.Symbols.Count);

            Configure(dfa, flags, out underlyingTable);
        }
        public CanonicalLrDfaTable(ILrDfa dfa, IMutableTable <int> actionTable)
        {
            var flag = LrTableOptimizations.EliminateLr0ReduceStates;

            this.canOptimizeReduceStates = (dfa.Optimizations & flag) == flag;

            this.grammar     = dfa.GrammarAnalysis;
            this.actionTable = actionTable ?? new MutableTable <int>(dfa.States.Length, grammar.Symbols.Count);
            FillDfaTable(dfa.States);
            BuildConflictTable();
        }
Exemple #3
0
        private void button3_Click(object sender, EventArgs e)
        {
            listView2.Items.Clear();
            button2_Click(词法编译ToolStripMenuItem, null);
            listView3.Items.Clear();
            dataGridView3.Rows.Clear();
            List <ERROR> el;

            if (Lex.getTokenList() != null)
            {
                tokenList = Lex.getTokenList();

                if (!Text.TrimEnd().Equals("") && tokenList != null)
                {
                    g = new GrammarAnalysis(Text);
                    g.getGrammarFromFile();
                    g.myParser(tokenList);
                    List <parseResult> rl = g.getResultList();
                    el = g.getErrorList();
                    List <string> threel = g.getThreeList();

                    Form f = new Form3(rl, el, tokenList, threel);
                    f.Show();
                }
            }
            foreach (ERROR em in g.getErrorList())
            {
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems.Clear();
                lvi.SubItems[0].Text = em.getErrorKind();
                lvi.SubItems.Add(em.getStrError());
                lvi.SubItems.Add(em.getRow().ToString());
                listView2.Items.Add(lvi);
                dataGridView3.Rows.Add(em.getErrorKind(), em.getStrError(), em.getRow().ToString());
            }



            List <string> threeli = g.getThreeList();

            dataGridView1.Rows.Clear();
            foreach (string term in threeli)
            {
                dataGridView1.Rows.Add(term);
            }
            Stack <SymbolTable> sm = g.getST();

            dataGridView2.Rows.Clear();
            foreach (SymbolTable sym in sm)
            {
                dataGridView2.Rows.Add(sym.getReturnType(), sym.getFun_name(), sym.getOffset());
            }
        }
Exemple #4
0
        // TODO: Performance
        private static void CollectClosureLookaheads(IDotItemSet result, GrammarAnalysis grammar)
        {
            int count = result.Count;

            if (count == 0)
            {
                return;
            }


            bool modified;

            // Debug.WriteLine("closured lookeads: item count = {0}", result.Count);

            do
            {
                modified = false;

                for (int i = 0; i != count; ++i)
                {
                    var fromItem = result[i];
                    if (!fromItem.IsReduce)
                    {
                        for (int j = 0; j != count; ++j)
                        {
                            var toItem = result[j];

                            if (fromItem.NextToken == toItem.Outcome)
                            {
                                int countBefore = 0;
                                if (!modified)
                                {
                                    countBefore = toItem.LA.Count;
                                }

                                grammar.AddFirst(fromItem.CreateNextItem(), toItem.LA);

                                if (!modified)
                                {
                                    modified = toItem.LA.Count != countBefore;
                                }
                            }
                        }
                    }
                }

                if (modified)
                {
                    // Debug.WriteLine("closured lookaheads: extra pass");
                }
            }while (modified);
        }
Exemple #5
0
        public Lalr1Dfa(GrammarAnalysis grammar, LrTableOptimizations optimizations)
        {
            this.grammar       = grammar;
            this.Optimizations = optimizations;
            this.TokenSet      = grammar.TokenSet;

            BuildLalr1States();

            if ((Optimizations & LrTableOptimizations.EliminateLr0ReduceStates) != 0)
            {
                EliminateLr0ReduceStates();
            }
        }
Exemple #6
0
        public ReductionModifiedLrDfaTable(ILrDfa dfa, IMutableTable <int> actionTable = null)
        {
            var flag = LrTableOptimizations.EliminateLr0ReduceStates;

            this.canOptimizeReduceStates = (dfa.Optimizations & flag) == flag;

            this.grammar = dfa.GrammarAnalysis;
            var states = dfa.States;

            this.actionTable = actionTable ?? new MutableTable <int>(states.Length, dfa.GrammarAnalysis.Symbols.Count);
            FillDfaTable(states);
            BuildConflictActionTable();
        }
        private bool Build(ILogging logging, out LanguageData result)
        {
            this.logging = logging;

            result = new LanguageData();

            var readerType = Type.GetType(source.ReaderTypeName);

            if (readerType == null)
            {
                logging.Write(
                    new LogEntry
                {
                    Severity = Severity.Error,
                    Message  = string.Format(
                        "Unable to find grammar reader '{0}' for language '{1}'",
                        source.ReaderTypeName,
                        source.LanguageName),
                    Origin = source.Origin
                });
                return(false);
            }

            var reader  = (IGrammarReader)Activator.CreateInstance(readerType);
            var grammar = reader.Read(source, logging);

            if (grammar == null)
            {
                return(false);
            }

            grammar.Joint.Add(source);

//            var inliner = new ProductionInliner(grammar);
//            grammar = inliner.Inline();

            if (!bootstrap && !CompileScannerTdfas(grammar))
            {
                result = null;
                return(false);
            }

            // Build parsing tables
            ILrDfa parserDfa = null;

            var grammarAnalysis = new GrammarAnalysis(grammar);

            logging.WithTimeLogging(
                source.LanguageName,
                source.Origin,
                () =>
            {
                parserDfa = new Lalr1Dfa(grammarAnalysis, LrTableOptimizations.Default);
            },
                "building LALR1 DFA");

            if (parserDfa == null)
            {
                result = null;
                return(false);
            }

            var lrTable = new ConfigurableLrTable(parserDfa, grammar.Options);

            if (!lrTable.ComplyWithConfiguration)
            {
                grammar.Reports.Add(new ConflictMessageBuilder(logging));
            }

            var localParseContexts = CollectLocalContexts(grammar, parserDfa);

            // Prepare language data for the language assembly generation
            result.IsDeterministic           = !lrTable.RequiresGlr;
            result.Grammar                   = grammar;
            result.TokenComplexity           = grammarAnalysis.GetTokenComplexity();
            result.StateToToken              = parserDfa.GetStateToSymbolTable();
            result.ParserActionTable         = lrTable.GetParserActionTable();
            result.ParserConflictActionTable = lrTable.GetConflictActionTable();

            result.LocalParseContexts = localParseContexts.ToArray();

            if (!bootstrap)
            {
                IReportData reportData = new ReportData(source, result, lrTable.Conflicts, parserDfa.States);
                foreach (var report in grammar.Reports)
                {
                    report.Build(reportData);
                }
            }

            return(true);
        }
Exemple #8
0
        private void button2_Click(object sender, EventArgs e)
        {
            string s = GrammarEditor.Text;
            int    i = 0;

            listView1.Items.Clear();
            listView2.Items.Clear();
            listBox1.DataSource = null;
            listBox3.DataSource = null;
            nl = new HashSet <NonTerminalSymbol>();

            if (s.Trim() != "")
            {
                g = new GrammarAnalysis(s);

                //MessageBox.Show("sdjhj");
                int count = 1;
                foreach (Grammar gm in g.getGrammarFromFile())
                {
                    // MessageBox.Show(gm.getRightPart().Count+"");

                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems[0].Text = count + "";
                    lvi.SubItems.Add(gm.getLeftPart());
                    lvi.SubItems.Add(gm.getmGrammar());
                    string selectset = "";

                    foreach (string sm in gm.getSelect())
                    {
                        selectset += sm + " ";
                    }
                    lvi.SubItems.Add(selectset);
                    string df = "";

                    foreach (string ss in g.mapGet(gm.getLeftPart()).getFollowList())
                    {
                        df += ss + " ";
                    }
                    lvi.SubItems.Add(df);
                    df = "";
                    foreach (string sr in g.mapGet(gm.getLeftPart()).getFirst())
                    {
                        df += sr + " ";
                    }

                    lvi.SubItems.Add(df);

                    count++;
                    listView1.Items.Add(lvi);
                }



                listBox1.DataSource = g.getNquene();
                listBox3.DataSource = g.getTquene();
                nl = g.getNlist();

                FileStream   fs2 = new FileStream("expression.txt", FileMode.Create);
                StreamWriter sw2 = new StreamWriter(fs2, Encoding.Default);
                foreach (NonTerminalSymbol n in nl)
                {
                    HashSet <Table> t = n.getTable();

                    foreach (Table tt in t)
                    {
                        string        item1 = tt.getName();
                        List <string> s2    = tt.getExpression();
                        string        item2 = "";
                        foreach (string ss3 in s2)
                        {
                            item2 += ss3 + " ";
                        }

                        sw2.WriteLine(n.getValue() + "#" + item1 + "#" + item2);
                    }
                }


                sw2.Flush();
                sw2.Close();
                sw2.Dispose();
            }

            else
            {
                MessageBox.Show("无法进行文法分析。文法为空");
            }
        }