C# 代码:生成 NFA 和 DFA 的语法分析器函数
private void Project_information()
{
this.NFA.Clear();
int value = 1;
foreach (string text in this.No_end_char)
{
foreach (string str in this.Products[text])
{
this.just_product.Add(text + '->' + str, value);
this.nega_product.Add(value++, text + '->' + str);
}
}
value = 0;
this.just_project.Add(this.startchar + '->.' + this.startchar, value);
this.nega_project.Add(value++, this.startchar + '->.' + this.startchar);
this.just_project.Add(this.startchar + '->' + this.startchar + '.', value);
this.nega_project.Add(value++, this.startchar + '->' + this.startchar + '.');
foreach (string text2 in this.No_end_char)
{
foreach (string text3 in this.Products[text2])
{
bool flag = text3 == '#';
if (flag)
{
this.just_project.Add(text2 + '->.', value);
this.nega_project.Add(value++, text2 + '->.');
}
else
{
for (int i = 0; i < text3.Length + 1; i++)
{
string text4 = text3;
this.just_project.Add(text2 + '->' + text4.Insert(i, '.'), value);
this.nega_project.Add(value++, text2 + '->' + text4.Insert(i, '.'));
}
}
}
}
this.NFA.Add(new List<string>
{
"0",
this.startchar,
"1"
});
bool flag2 = this.No_end_char.Contains(this.startchar);
if (flag2)
{
foreach (string text5 in this.Products[this.startchar])
{
string text6 = text5;
bool flag3 = text5 == '#';
if (flag3)
{
this.NFA.Add(new List<string>
{
"0",
"#",
this.just_project[this.startchar + '->.'].ToString()
});
}
else
{
this.NFA.Add(new List<string>
{
"0",
"#",
this.just_project[this.startchar + '->' + text6.Insert(0, '.')].ToString()
});
}
}
}
foreach (string text7 in this.No_end_char)
{
foreach (string text8 in this.Products[text7])
{
bool flag4 = text8 == '#';
if (!flag4)
{
for (int j = 0; j < text8.Length; j++)
{
string text9 = text8;
this.NFA.Add(new List<string>
{
this.just_project[text7 + '->' + text9.Insert(j, '.')].ToString(),
text9[j].ToString(),
this.just_project[text7 + '->' + text9.Insert(j + 1, '.')].ToString()
});
bool flag5 = this.No_end_char.Contains(text9[j].ToString());
if (flag5)
{
foreach (string text10 in this.Products[text9[j].ToString()])
{
bool flag6 = text10 == '#';
if (flag6)
{
this.NFA.Add(new List<string>
{
this.just_project[text7 + '->' + text9.Insert(j, '.')].ToString(),
"#",
this.just_project[text9[j].ToString() + '->.'].ToString()
});
}
else
{
string text11 = text10;
this.NFA.Add(new List<string>
{
this.just_project[text7 + '->' + text9.Insert(j, '.')].ToString(),
"#",
this.just_project[text9[j].ToString() + '->' + text11.Insert(0, '.')].ToString()
});
}
}
}
}
}
}
}
foreach (List<string> list in this.NFA)
{
bool flag7 = !this.letter.Contains(list[1]) && list[1] != "#";
if (flag7)
{
this.letter.Add(list[1]);
}
}
this.NFA_DFA();
}
这段代码是一个语法分析器的一部分,用于生成 NFA 和 DFA。在函数中,首先清空了 NFA,然后将所有产生式和对应的编号存储到了 just_product 和 nega_product 中。接着,将初始状态的产生式和状态添加到 just_project 和 nega_project 中。然后,对于每个非终结符,将其所有产生式的状态添加到 just_project 和 nega_project 中。接着,将初始状态和开始符号连接的转移添加到 NFA 中。然后,对于每个产生式,将其所有状态和转移添加到 NFA 中。最后,生成的 NFA 将被传递给 NFA_DFA 函数进行转换。
原文地址: https://www.cveoy.top/t/topic/oVFf 著作权归作者所有。请勿转载和采集!