public Liststring terminals; 终结符集合 public Liststring nonterminal; 非终结符集合 public Dictionarystring Liststring production;继承LL1中的原始产生式 public Dictionarystring ListListstring produc
private void BuilditemFamily()
{
// 添加起始项目
List
// 循环处理每个项目,直到没有新的项目被添加
bool hasNew = true;
while (hasNew)
{
hasNew = false;
for (int i = 0; i < itemsets.Count; i++)
{
List<string> itemset = itemsets[i].Split(' ').ToList();
// 找到项目中的·
int dotIndex = itemset.IndexOf("·");
// 如果·在最后一个位置,说明该项目已经是规范项,不需要处理
if (dotIndex == itemset.Count - 1)
{
continue;
}
// 找到·后面的符号
string symbol = itemset[dotIndex + 1];
// 如果符号是终结符,也不需要处理
if (symbols.Contains(symbol) && !symbol.Equals("#"))
{
continue;
}
// 找到该符号对应的所有产生式
List<string> productionsWithSymbol = productions.Where(p => p.StartsWith(symbol)).ToList();
// 对每个产生式生成一个新的项目,并加入到项目族中
foreach (string production in productionsWithSymbol)
{
List<string> newItem = new List<string>() { symbol, "·" };
newItem.AddRange(production.Substring(1).Split(' '));
if (!itemsets.Contains(string.Join(" ", newItem)))
{
itemsets.Add(string.Join(" ", newItem));
hasNew = true;
}
}
}
}
}
private List
// 循环处理每个Item,直到没有新的Item被添加
bool hasNew = true;
while (hasNew)
{
hasNew = false;
for (int i = 0; i < items.Count; i++)
{
Item item = items[i];
// 找到·后面的符号
string symbol = item.RHS[item.DotIndex];
// 如果符号是终结符,也不需要处理
if (symbols.Contains(symbol) && !symbol.Equals("#"))
{
continue;
}
// 找到该符号对应的所有产生式
List<string> productionsWithSymbol = productions.Where(p => p.StartsWith(symbol)).ToList();
// 对每个产生式生成一个新的Item,并加入到项目集中
foreach (string production in productionsWithSymbol)
{
Item newItem = new Item(symbol, production.Split(' ').ToList(), 0);
if (!items.Contains(newItem))
{
items.Add(newItem);
hasNew = true;
}
}
}
}
// 将Item列表转化为字符串列表并返回
return items.Select(i => i.ToString()).ToList();
}
private string Goto(int index, string symbol)
{
// 将输入的项目集转化为一个Item列表
List
// 对每个Item进行移进操作,并生成新的项目集
List<Item> newItems = new List<Item>();
foreach (Item item in items)
{
if (item.DotIndex < item.RHS.Count && item.RHS[item.DotIndex].Equals(symbol))
{
newItems.Add(new Item(item.LHS, item.RHS, item.DotIndex + 1));
}
}
List<string> newSet = Closure(newItems.Select(i => i.ToString()).ToList());
// 如果新的项目集已经存在,返回其索引;否则将其添加到项目族中并返回其索引
string newSetString = string.Join(" ", newSet);
if (itemsets.Contains(newSetString))
{
return itemsets.IndexOf(newSetString).ToString();
}
else
{
itemsets.Add(newSetString);
return (itemsets.Count - 1).ToString();
}
}
private void BuildTables()
{
// 初始化GOTO表和ACTION表
foreach (string state in states)
{
gotoTable[state] = new List
// 对每个项目集生成对应的状态,并填充GOTO表和ACTION表
for (int i = 0; i < itemsets.Count; i++)
{
List<string> itemset = itemsets[i].Split(' ').ToList();
// 如果该项目集包含起始项目,则将其对应的状态设置为0
if (itemset.Contains(productions[0]) && itemset.Contains("·") && itemset.Contains("#"))
{
states.Add("0");
}
else
{
states.Add(i.ToString());
}
foreach (string symbol in symbols)
{
if (symbol.Equals("#"))
{
// 如果符号是#,则在ACTION表中填写acc
if (itemset.Contains("#") && !itemset.Contains("·"))
{
actionTable[i.ToString()].Add("acc");
}
else
{
actionTable[i.ToString()].Add("");
}
}
else if (symbols.Contains(symbol))
{
// 如果符号是终结符,则在ACTION表中填写移进或规约操作
string nextState = Goto(i, symbol);
if (!nextState.Equals(""))
{
actionTable[i.ToString()].Add("s" + nextState);
}
else
{
int productionIndex = productions.IndexOf(string.Join(" ", itemset.Where(s => !s.Equals("·"))));
if (productionIndex >= 0)
{
actionTable[i.ToString()].Add("r" + productionIndex);
}
else
{
actionTable[i.ToString()].Add("");
}
}
}
else
{
// 如果符号是非终结符,则在GOTO表中填写下一个状态的索引
string nextState = Goto(i, symbol);
if (!nextState.Equals(""))
{
gotoTable[i.ToString()].Add(nextState);
}
else
{
gotoTable[i.ToString()].Add("");
}
actionTable[i.ToString()].Add("");
}
}
}
原文地址: https://www.cveoy.top/t/topic/hdMw 著作权归作者所有。请勿转载和采集!