private void BuilditemFamily 清空之前的数据 productionsClear; symbolsClear; statesClear; itemsetsClear; 获取文法产生式和符号 strin
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);
// 构造其他状态
int i = 0;
while (i < itemsets.Count)
{
string[] items = itemsets[i].Split(' ');
Dictionary<string, List<string>> gotos = new Dictionary<string, List<string>>();
foreach (string item in items)
{
int dotIndex = item.IndexOf('.');
if (dotIndex < item.Length - 1)
{
string symbol = item.Substring(dotIndex + 1, 1);
if (!gotos.ContainsKey(symbol))
{
gotos[symbol] = new List<string>();
}
string newItem = item.Substring(0, dotIndex) + symbol + "." + item.Substring(dotIndex + 2);
gotos[symbol].Add(newItem);
}
}
foreach (string symbol in gotos.Keys)
{
List<string> newItemset = gotos[symbol];
string newItemsetName = string.Join(" ", newItemset.ToArray());
if (!itemsets.Contains(newItemsetName))
{
itemsets.Add(newItemsetName);
states.Add(itemsets.Count - 1 + "");
}
}
i++;
}
// 构造转移表
for (i = 0; i < itemsets.Count; i++)
{
string[] items = itemsets[i].Split(' ');
Dictionary<string, int> actions = new Dictionary<string, int>();
foreach (string item in items)
{
int dotIndex = item.IndexOf('.');
if (dotIndex < item.Length - 1)
{
string symbol = item.Substring(dotIndex + 1, 1);
if (symbols.Contains(symbol))
{
string newItem = item.Substring(0, dotIndex) + symbol + "." + item.Substring(dotIndex + 2);
int j = itemsets.IndexOf(newItem);
if (j >= 0)
{
if (symbols.IndexOf(symbol) == symbols.Count - 1)
{
actions[symbol] = j;
}
else
{
actions[symbol] = j + states.Count;
}
}
}
}
else if (dotIndex == item.Length - 1)
{
foreach (string prod in productions)
{
string left = prod.Substring(0, prod.IndexOf('-')).Trim();
if (items.Contains(left + "->" + prod.Substring(prod.IndexOf('>') + 1).Trim()))
{
int j = productions.IndexOf(prod);
actions["$"] = j + 2 * states.Count;
}
}
}
}
foreach (string symbol in symbols)
{
if (!actions.ContainsKey(symbol))
{
actions[symbol] = -1;
}
}
foreach (string symbol in actions.Keys)
{
int action = actions[symbol];
if (action >= 0)
{
if (symbol == "$")
{
action = action - 2 * states.Count;
}
else if (symbols.IndexOf(symbol) < symbols.Count - 1)
{
action = action - states.Count;
}
dataGridView1.Rows[i].Cells[symbols.IndexOf(symbol) + 1].Value = action.ToString();
}
}
}
原文地址: https://www.cveoy.top/t/topic/heXi 著作权归作者所有。请勿转载和采集!