public LR0(isLL_1_ isLL_1_)
补充代码如下:
private void BuilditemFamily() { // 清空之前的数据 productions.Clear(); symbols.Clear(); states.Clear(); itemsets.Clear();
// 获取文法产生式和符号
string[] lines = richTextBox1.Lines;
foreach (string line in lines)
{
if (line.Trim() != "")
{
string[] parts = line.Split(new char[] { '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
string left = parts[0].Trim();
string right = parts[1].Trim();
if (!symbols.Contains(left))
{
symbols.Add(left);
}
string[] symbolsInRight = right.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string symbol in symbolsInRight)
{
productions.Add(left + "->" + symbol);
foreach (char c in symbol)
{
if (!symbols.Contains(c.ToString()))
{
symbols.Add(c.ToString());
}
}
}
}
}
// 构造初始状态
string initialState = "S`->.S";
foreach (var prod in productions)
initialState = initialState + " " + prod.ToString().Replace(">", ">.");
states.Add("0");
itemsets.Add(initialState);
// 构造其他状态
Queue<string> queue = new Queue<string>();
queue.Enqueue(initialState);
while (queue.Count > 0)
{
string itemset = queue.Dequeue();
string[] items = itemset.Split(' ');
Dictionary<string, List<string>> gotoDict = new Dictionary<string, List<string>>();
Dictionary<string, List<string>> actionDict = new Dictionary<string, List<string>>();
foreach (string symbol in symbols)
{
if (symbol == "$")
{
continue;
}
List<string> gotoItems = new List<string>();
List<string> actionItems = new List<string>();
foreach (string item in items)
{
string[] parts = item.Split(new char[] { '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
string left = parts[0].Trim();
string right = parts[1].Trim();
int dotIndex = right.IndexOf('.');
if (dotIndex == right.Length - 1)
{
continue;
}
string nextSymbol = right[dotIndex + 1].ToString();
if (nextSymbol == symbol)
{
string newItem = left + "->" + right.Substring(0, dotIndex + 1) + "." + right.Substring(dotIndex + 2);
gotoItems.Add(newItem);
}
}
if (gotoItems.Count > 0)
{
string newItemset = string.Join(" ", gotoItems);
if (!itemsets.Contains(newItemset))
{
itemsets.Add(newItemset);
states.Add(itemsets.Count.ToString());
queue.Enqueue(newItemset);
}
gotoDict[symbol] = new List<string>() { states[itemsets.IndexOf(newItemset)] };
}
else
{
gotoDict[symbol] = new List<string>();
}
foreach (string item in items)
{
string[] parts = item.Split(new char[] { '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
string left = parts[0].Trim();
string right = parts[1].Trim();
int dotIndex = right.IndexOf('.');
if (dotIndex == right.Length - 1)
{
if (left == "S`")
{
actionItems.Add("accept");
}
else
{
List<string> followSet = Follow(left);
foreach (string followSymbol in followSet)
{
actionItems.Add("reduce " + left + "->" + right.Substring(0, dotIndex));
}
}
}
else
{
string nextSymbol = right[dotIndex + 1].ToString();
if (nextSymbol == symbol)
{
bool canShift = true;
foreach (string item2 in items)
{
if (item == item2)
{
continue;
}
string[] parts2 = item2.Split(new char[] { '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
string left2 = parts2[0].Trim();
string right2 = parts2[1].Trim();
int dotIndex2 = right2.IndexOf('.');
if (dotIndex2 == right2.Length - 1)
{
continue;
}
string nextSymbol2 = right2[dotIndex2 + 1].ToString();
if (nextSymbol2 == symbol)
{
if (Precedence(right[dotIndex], right2[dotIndex2]) == "<")
{
canShift = false;
break;
}
}
}
if (canShift)
{
actionItems.Add("shift " + states[itemsets.IndexOf(itemset + " " + left + "->" + right.Substring(0, dotIndex + 1) + nextSymbol + "." + right.Substring(dotIndex + 2))]);
}
}
}
}
if (actionItems.Count > 0)
{
actionDict[symbol] = actionItems;
}
else
{
actionDict[symbol] = new List<string>();
}
}
gotoTable[itemset] = new List<string>();
actionTable[itemset] = new List<string>();
foreach (string symbol in symbols)
{
if (gotoDict.ContainsKey(symbol))
{
gotoTable[itemset].Add(symbol + " " + string.Join(",", gotoDict[symbol]));
}
else
{
gotoTable[itemset].Add(symbol + " ");
}
if (actionDict.ContainsKey(symbol))
{
actionTable[itemset].Add(symbol + " " + string.Join(",", actionDict[symbol]));
}
else
{
actionTable[itemset].Add(symbol + " ");
}
}
}
}
private string Precedence(char c1, char c2) { if (c1 == '+' || c1 == '-') { if (c2 == '' || c2 == '/') { return "<"; } else { return "="; } } else if (c1 == '' || c1 == '/') { return "="; } else { return ""; } }
private List
private List
private void button5_Click(object sender, EventArgs e)//构造LR分析表 { BuilditemFamily();
// 显示GOTO表和ACTION表
listView2.Columns.Clear();
listView2.Items.Clear();
listView2.View = View.Details;
// 添加列名
listView2.Columns.Add("状态", 80);
foreach (string symbol in symbols)
{
listView2.Columns.Add(symbol, 80);
}
// 添加数据
foreach (string itemset in itemsets)
{
ListViewItem lvi = new ListViewItem(itemsets.IndexOf(itemset).ToString());
foreach (string action in actionTable[itemset])
{
lvi.SubItems.Add(action);
}
foreach (string gotoState in gotoTable[itemset])
{
lvi.SubItems.Add(gotoState);
}
listView2.Items.Add(lvi);
}
listView2.GridLines = true;
}
原文地址: https://www.cveoy.top/t/topic/fZRJ 著作权归作者所有。请勿转载和采集!