Ejemplo n.º 1
0
    public LL1Table(InputGrammer inputGrammer) : base(inputGrammer)
    {
        generatedFirst = new GenerateFirst(inputGrammer);
        GenerateFollow generatedFollow = new GenerateFollow(inputGrammer, generatedFirst);

        if (!checkLeft())
        {
            throw new FailToGenerateLL1Exception("含有左递归或左公因子");
        }
        foreach (Production production in inputGrammer.userProductions)
        {
            List <string> firstSetOfRightPart = generatedFirst.getFirstFromPart(production.Values);
            foreach (string token in firstSetOfRightPart.Where(i => (!inputGrammer.nonTerminalTokens.Contains(i) && i != PublicFunc.EPSILON)))
            {
                Item item = new Item(production.Key, token, production.ToString());
                checkConflict(item);
                table.Add(item);
            }
            if (firstSetOfRightPart.Contains(PublicFunc.EPSILON))
            {
                foreach (string token in generatedFollow[production.Key].Where(i => !inputGrammer.nonTerminalTokens.Contains(i)))
                {
                    Item item = new Item(production.Key, token, production.ToString());
                    checkConflict(item);
                    table.Add(item);
                }
            }
        }
    }
Ejemplo n.º 2
0
 public DFAGraphFromLR0(InputGrammer inputGrammer) : base(inputGrammer)
 {
     DFANodes.Add(new DFANode(closure(new List <ProductionInLR0> {
         new ProductionInLR0(inputGrammer.userProductions[0])
     }), Convert.ToString(DFANodes.Count)));
     generateDFAGraph <ProductionInLR0>(DFANodes);
 }
Ejemplo n.º 3
0
    public SLR1Table(DFAGraphBase DFAGraph, InputGrammer inputGrammer) : base(DFAGraph, inputGrammer)
    {
        GenerateFirst generatedFirst = new GenerateFirst(inputGrammer);

        generatedFollow = new GenerateFollow(inputGrammer, generatedFirst);
        generateLRTable();
    }
Ejemplo n.º 4
0
 public DFAGraphBase(InputGrammer inputGrammer)
 {
     this.inputGrammer = inputGrammer;
     if (!checkGrammer())
     {
         throw new ExpandException("文法需要扩充为增广文法");
     }
 }
Ejemplo n.º 5
0
    private bool generateLLTableInGrid()
    {
        try
        {
            inputGrammer = new InputGrammer(InputArea.Text);
            ll1table     = new LL1Table(inputGrammer);
            List <Item> tableData = ll1table.TableData;

            List <string> cols = new List <string> {
                "M(N,T)"
            };
            cols.AddRange(tableData.Select(i => i.col).Distinct().ToList());
            List <string> rows = tableData.Select(i => i.row).Distinct().ToList();

            dataGridView.Columns.Clear();
            dataGridView.Rows.Clear();
            int weight = 65535 / (cols.Count + 1);
            for (int i = 0; i < cols.Count; i++)
            {
                dataGridView.Columns.Add(cols[i], null);
                // 禁用自动排序
                dataGridView.Columns[i].SortMode   = DataGridViewColumnSortMode.NotSortable;
                dataGridView.Columns[i].FillWeight = weight;
            }

            foreach (string row in rows)
            {
                dataGridView.Rows.Add(row);
            }
            foreach (Item item in tableData)
            {
                dataGridView.Rows[rows.IndexOf(item.row)].Cells[cols.IndexOf(item.col)].Value = item.data;
            }
        }
        catch (NoValidGrammerException ee)
        {
            MessageBox.Show(ee.Message, ee.Message);
            return(false);
        }
        catch (FailToGenerateLL1Exception ee)
        {
            MessageBox.Show(ee.Message, "生成LL(1)分析表失败");
            return(false);
        }
        catch (ConflictException ee)
        {
            MessageBox.Show(ee.Message, "生成LL(1)分析表失败");
            if (formDFADictionary == null)
            {
                formDFADictionary = new FormDFADictionary();
            }
            formDFADictionary.getTextData(ee.reason);
            formDFADictionary.Show();
            return(false);
        }
        return(true);
    }
Ejemplo n.º 6
0
 public DFAGraphFromLR1(InputGrammer inputGrammer) : base(inputGrammer)
 {
     generatedFirst = new GenerateFirst(inputGrammer);
     DFANodes.Add(new DFANode(closure(new List <ProductionInLR0> {
         new ProductionInLR1(inputGrammer.userProductions[0], new List <string> {
             PublicFunc.ENDSYMBOL
         })
     }), DFANodes.Count.ToString()));
     generateDFAGraph <ProductionInLR1>(DFANodes);
 }
Ejemplo n.º 7
0
    public GenerateFirst(InputGrammer inputGrammer)
    {
        nonTerminalTokens = inputGrammer.nonTerminalTokens;
        // 初始化first集合
        foreach (string token in inputGrammer.nonTerminalTokens)
        {
            firstSet.Add(new Production(token, new List <string>()));
        }
        int changeTotal;

        do
        {
            changeTotal = 0;
            foreach (Production production in inputGrammer.userProductions)
            {
                List <string> valuesInFirst = GetValuesByKey(production.Key, firstSet);
                int           i;
                for (i = 0; i < production.Values.Count; i++)
                {
                    string token = production.Values[i];
                    if (!inputGrammer.nonTerminalTokens.Contains(token))
                    {
                        // 终结符号且不是Epsilon
                        if (token != EPSILON)
                        {
                            if (!valuesInFirst.Contains(token))
                            {
                                valuesInFirst.Add(token);
                                changeTotal++;
                            }
                            break;
                        }
                    }
                    else
                    {
                        bool          hasEpsilon           = false;
                        List <string> valuesOfTokenInFirst = GetValuesByKey(token, firstSet);
                        if (ExtendElementWithoutEpsilon(valuesOfTokenInFirst, valuesInFirst, ref hasEpsilon))
                        {
                            changeTotal++;
                        }
                        if (!hasEpsilon)
                        {
                            break;
                        }
                    }
                }
                if (i == production.Values.Count && !valuesInFirst.Contains(EPSILON))
                {
                    valuesInFirst.Add(EPSILON);
                    changeTotal++;
                }
            }
        } while (changeTotal != 0);
    }
Ejemplo n.º 8
0
 public Analyze(InputGrammer inputGrammer, Table table, string text)
 {
     this.inputGrammer = inputGrammer;
     // terminalTokens去除Epsilon、按字符串长度从大到小排序
     inputTokens = flatText(text, inputGrammer.terminalTokens.Where(i => i != PublicFunc.EPSILON).OrderBy(i => - i.Length).ToList());
     if (inputTokens.Count == 0)
     {
         throw new FailToAnalyzeException("无法识别任何符号");
     }
     this.table = table;
 }
Ejemplo n.º 9
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         InputGrammer  inputGrammer   = new InputGrammer(InputArea.Text);
         GenerateFirst generatedFirst = new GenerateFirst(inputGrammer);
         if (formFirstAndFollow == null)
         {
             formFirstAndFollow = new FormFirstAndFollow();
         }
         formFirstAndFollow.getTextData(generatedFirst);
         formFirstAndFollow.Show();
     }
     catch (NoValidGrammerException ee) { MessageBox.Show(ee.Message, ee.Message); }
 }
Ejemplo n.º 10
0
    public GenerateFollow(InputGrammer inputGrammer, GenerateFirst generatedFirst)
    {
        foreach (string token in inputGrammer.nonTerminalTokens)
        {
            followSet.Add(new Production(token, new List <string>()));
        }
        // 加入$符号
        GetValuesByKey(inputGrammer.nonTerminalTokens[0], followSet).Add(ENDSYMBOL);
        int changeTotal;

        do
        {
            changeTotal = 0;
            foreach (Production production in inputGrammer.userProductions)
            {
                List <string> valuesInFollow = GetValuesByKey(production.Key, followSet);
                for (int i = 0; i < production.Values.Count; i++)
                {
                    string token = production.Values[i];
                    if (inputGrammer.nonTerminalTokens.Contains(token))
                    {
                        List <string> valuesOfTokenInFollow = GetValuesByKey(token, followSet);
                        if (i != production.Values.Count - 1)
                        {
                            bool hasEpsilon = false;
                            if (ExtendElementWithoutEpsilon(generatedFirst.getFirstFromPart(production.Values, i + 1), valuesOfTokenInFollow, ref hasEpsilon))
                            {
                                changeTotal++;
                            }
                            if (hasEpsilon && ExtendElement(valuesInFollow, valuesOfTokenInFollow))
                            {
                                changeTotal++;
                            }
                        }
                        else if (ExtendElement(valuesInFollow, valuesOfTokenInFollow))
                        {
                            changeTotal++;
                        }
                    }
                }
            }
        } while (changeTotal != 0);
    }
Ejemplo n.º 11
0
    private bool generateLRTableInGrid <T1, T2>() where T1 : DFAGraphBase where T2 : LRTable
    {
        string kindOfDFA   = typeof(T1).Name;
        string kindOfTable = typeof(T2).Name;

        kindOfTable = kindOfTable.Substring(0, kindOfTable.IndexOf("Table"));
        try
        {
            try
            {
                inputGrammer = new InputGrammer(InputArea.Text);
                dFAGraph     = Activator.CreateInstance(typeof(T1), new object[] { inputGrammer }) as T1;
                lRTable      = Activator.CreateInstance(typeof(T2), new object[] { dFAGraph, inputGrammer }) as T2;
            }
            catch (TargetInvocationException ee)
            {
                throw ee.GetBaseException();
            }
            List <Item>   tableData = lRTable.TableData;
            List <string> cols      = new List <string> {
                "状态"
            };
            cols.AddRange(tableData.Select(i => i.col).Where(i => !inputGrammer.nonTerminalTokens.Contains(i)).Distinct().ToList());
            cols.AddRange(inputGrammer.nonTerminalTokens);
            List <string> rows = tableData.Select(i => i.row).Distinct().OrderBy(i => Convert.ToInt32(i)).ToList();

            dataGridView.Columns.Clear();
            dataGridView.Rows.Clear();


            int weight = 65535 / (cols.Count + 1);
            for (int i = 0; i < cols.Count; i++)
            {
                dataGridView.Columns.Add(cols[i], null);
                // 禁用自动排序
                dataGridView.Columns[i].SortMode   = DataGridViewColumnSortMode.NotSortable;
                dataGridView.Columns[i].FillWeight = weight;
            }
            foreach (string row in rows)
            {
                dataGridView.Rows.Add(row);
            }
            foreach (Item item in tableData)
            {
                dataGridView.Rows[rows.IndexOf(item.row)].Cells[cols.IndexOf(item.col)].Value = item.data;
            }
            showTableMessage(getStrFromGrammerAndDFA(inputGrammer, dFAGraph));
            labelOfTable.Text = kindOfTable + "分析表";
        }

        catch (NoValidGrammerException ee)
        {
            MessageBox.Show(ee.Message, ee.Message);
            return(false);
        }
        catch (ExpandException ee)
        {
            MessageBox.Show(ee.Message, "生成" + kindOfDFA + "失败");
            return(false);
        }
        catch (ConflictException ee)
        {
            MessageBox.Show(ee.Message, "生成" + kindOfTable + "分析表失败");
            showTableMessage(ee.reason + getStrFromGrammerAndDFA(inputGrammer, dFAGraph));
            return(false);
        }
        return(true);
    }
Ejemplo n.º 12
0
 public LR0Table(DFAGraphBase dFAGraph, InputGrammer inputGrammer) : base(dFAGraph, inputGrammer)
 {
     generateLRTable();
 }
Ejemplo n.º 13
0
 public Table(InputGrammer inputGrammer)
 {
     this.inputGrammer = inputGrammer;
 }
Ejemplo n.º 14
0
 private string getStrFromGrammerAndDFA(InputGrammer inputGrammer, DFAGraphBase dFAGraph)
 {
     return(string.Format("\r\n产生式如下(编号从0开始)\r\n\r\n{0}\r\nDFA集族如下\r\n\r\n{1}", inputGrammer.ToString(), dFAGraph.ToString()));
 }
Ejemplo n.º 15
0
 public LRTable(DFAGraphBase dFAGraph, InputGrammer inputGrammer) : base(inputGrammer)
 {
     this.dFAGraph = dFAGraph;
 }
Ejemplo n.º 16
0
 public void getTable(Table table, InputGrammer inputGrammer)
 {
     this.table        = table;
     this.inputGrammer = inputGrammer;
 }
Ejemplo n.º 17
0
 public DFAGraphFromLALR1(InputGrammer inputGrammer) : base(inputGrammer)
 {
     mergeCore();
 }
Ejemplo n.º 18
0
    private void generateLRDFAInGrid <T>() where T : DFAGraphBase
    {
        string kind = typeof(T).Name;

        kind = kind.Substring(kind.IndexOf("From") + 4);
        try
        {
            try
            {
                inputGrammer = new InputGrammer(InputArea.Text);
                dFAGraph     = Activator.CreateInstance(typeof(T), new object[] { inputGrammer }) as T;
            }
            catch (TargetInvocationException ee)
            {
                throw ee.GetBaseException();
            }
            dataGridView.Columns.Clear();
            dataGridView.Rows.Clear();
            List <DFANode> dFANodes = dFAGraph.GetDFANodes;
            List <DFANode> srcs     = dFANodes.Where(i => i.degrees.Count != 0).ToList();
            List <string>  dsts     = dFANodes.Select(i => i.ID).ToList();


            List <string> cols = new List <string> {
                @"起点\终点"
            };
            cols.AddRange(dsts);

            // 列太多导致FillWeight过多时 ,winform DataGridView会爆掉
            int weight = 65535 / (cols.Count + 1);
            for (int i = 0; i < cols.Count; i++)
            {
                dataGridView.Columns.Add(cols[i], null);
                // 禁用排序
                dataGridView.Columns[i].SortMode   = DataGridViewColumnSortMode.NotSortable;
                dataGridView.Columns[i].FillWeight = weight;
            }

            // 行序号从小到大
            List <string> rows = srcs.Select(i => i.ID).Distinct().OrderBy(i => Convert.ToInt32(i)).ToList();
            foreach (string row in rows)
            {
                dataGridView.Rows.Add(row);
            }

            foreach (DFANode src in srcs)
            {
                int rowID = rows.IndexOf(src.ID);
                foreach (var degree in src.degrees)
                {
                    dataGridView.Rows[rowID].Cells[cols.IndexOf(degree.degreeOut)].Value = degree.translation;
                }
            }

            if (formDFADictionary == null)
            {
                formDFADictionary = new FormDFADictionary();
            }
            formDFADictionary.getTextData(dFAGraph.ToString());
            formDFADictionary.Show();


            labelOfTable.Text = kind + " DFA";
        }
        catch (NoValidGrammerException ee) { MessageBox.Show(ee.Message, ee.Message); }
        catch (ExpandException ee) { MessageBox.Show(ee.Message, "生成" + kind + " DFA失败"); }
    }
Ejemplo n.º 19
0
 public LALR1Table(DFAGraphBase dFAGraph, InputGrammer inputGrammer) : base(dFAGraph, inputGrammer)
 {
 }
Ejemplo n.º 20
0
 public LRAnalyze(InputGrammer inputGrammer, Table table, string text) : base(inputGrammer, table, text)
 {
 }