private void button2_Click(object sender, EventArgs e) { string str = richTextBox1.Text; if (str.Length == 0) { MessageBox.Show('输入为空'); return; } string temp = ""; int i = 0; while (i < str.Length) { temp = ""; while (i < str.Length && str[i] != '\r' && str[i] != '\n') { if (str[i] != ' ') temp += str[i]; i++; } i++; if (temp.Length > 0 && temp[temp.Length - 1] == '|') { MessageBox.Show('不得包含空串!'); return; } if (temp.Length > 3) { if (temp[0] < 'A' || temp[0] > 'Z') { MessageBox.Show('含有非法左部!'); return; } if (temp[1] != '-' || temp[2] != '>') { MessageBox.Show('产生式输入不规范!'); return; } } if (temp.Length <= 3 && temp.Length > 0) { MessageBox.Show('产生式输入不规范!'); return; } } if (lr.HasShiftReduceConflict()) { MessageBox.Show('存在移进规约冲突!'); return; } if (lr.HasReduceReduceConflict()) { MessageBox.Show('存在规约规约冲突!'); return; } MessageBox.Show('正确的LR(0)文法'); button4.Enabled = true; button5.Enabled = true; button6.Enabled = true; }

private void button5_Click(object sender, EventArgs e) { listView2.Clear();

LR.Table[][] table;
table = lr.GET_ANA();
int xlen = table.GetLength(0);
int ylen = table[1].Length;

listView2.Columns.Clear();
listView2.Items.Clear();
listView2.View = View.Details;

listView2.Columns.Add(" ");

for (int i = 0; i < lr.Echar.Count; i++)
{
    string text = lr.Echar[i].ToString();
    listView2.Columns.Add(text, 58);
}
for (int i = 0; i < lr.Nchar.Count; i++)
{
    string text = lr.Nchar[i].ToString();
    listView2.Columns.Add(text, 58);
}

for (int i = 0; i < xlen; i++)
{
    ListViewItem li = new ListViewItem(i.ToString());

    for (int j = 0; j < ylen; j++)
    {
        string st = "";
        if (table[i][j].error)
            st = "-";
        else if (table[i][j].type == 'A')
            st = "AC";
        else if (table[i][j].type == 'S' && lr.HasShiftReduceConflict(i, j))
            st = "SR";
        else if (table[i][j].type == 'R' && lr.HasReduceReduceConflict(i, j))
            st = "RR";
        else
            st = table[i][j].type.ToString() + table[i][j].id.ToString();
        li.SubItems.Add(st);
    }
    listView2.Items.Add(li);
}

listView2.GridLines = true;

}

LR(0) 文法分析器:移进规约和规约规约冲突检测

原文地址: https://www.cveoy.top/t/topic/oIdy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录