private void BuildLR0ItemSets 获取所有终结符和非终结符 foreach Production p in productions if !symbolsContainsnew SymbolpLeft false
private void button2_Click(object sender, EventArgs e) { // 判断是否为LR0文法 bool isLR0 = true; foreach (Production p in productions) { foreach (Symbol s in p.Right) { if (s.IsTerminal) { continue; } int count = 0; foreach (Production p2 in productions) { if (p2.Left == s.Name) { count++; } } if (count > 1) { isLR0 = false; break; } } if (!isLR0) { break; } } if (isLR0) { richTextBox1.AppendText("是LR(0)文法\n"); } else { richTextBox1.AppendText("不是LR(0)文法\n"); } }
private void button4_Click(object sender, EventArgs e) { // 生成项目族信息 LRParser parser = new LRParser(productions); parser.BuildLR0ItemSets(); listView1.Columns.Add("状态"); listView1.Columns.Add("项目族信息"); foreach (LR0ItemSet itemSet in parser.ItemSets) { ListViewItem item = new ListViewItem(itemSet.ID.ToString()); StringBuilder sb = new StringBuilder(); foreach (LR0Item item2 in itemSet) { sb.Append(item2.ToString()); sb.Append("\n"); } item.SubItems.Add(sb.ToString()); listView1.Items.Add(item); } }
private void button5_Click(object sender, EventArgs e) { // 构造LR分析表 LRParser parser = new LRParser(productions); parser.BuildLR0ItemSets(); parser.BuildGotoTable(); parser.BuildActionTable(); listView2.Columns.Add("状态"); foreach (Symbol s in parser.Symbols) { if (s.IsTerminal) { listView2.Columns.Add(s.Name); } } listView2.Columns.Add("goto"); foreach (int state in parser.ActionTable.Keys.Select(t => t.Item1).Distinct()) { ListViewItem item = new ListViewItem(state.ToString()); for (int i = 1; i < listView2.Columns.Count; i++) { string text = ""; Symbol symbol = parser.Symbols.FirstOrDefault(s => s.Name == listView2.Columns[i].Text); if (symbol != null) { if (symbol.IsTerminal) { if (parser.ActionTable.ContainsKey(new Tuple<int, Symbol>(state, symbol))) { text = parser.ActionTable[new Tuple<int, Symbol>(state, symbol)]; } } else { if (parser.GotoTable.ContainsKey(new Tuple<int, Symbol>(state, symbol))) { text = parser.GotoTable[new Tuple<int, Symbol>(state, symbol)].ToString(); } } } item.SubItems.Add(text); } listView2.Items.Add(item); } }
private void button6_Click(object sender, EventArgs e) { // 分析句子 string input = textBox1.Text; LRParser parser = new LRParser(productions); parser.BuildLR0ItemSets(); parser.BuildGotoTable(); parser.BuildActionTable(); bool result = parser.Parse(input); if (result) { listView3.Items.Add(new ListViewItem(new string[] { "accept", "", "", "", "" })); } else { listView3.Items.Add(new ListViewItem(new string[] { "error", "", "", "", "" })); } }
private void button7_Click(object sender, EventArgs e) { // 单步显示输入的待分析句子 if (inputStack.Count > 0) { int state = stateStack.Peek(); char inputChar = inputStack.Peek(); Symbol symbol = symbolStack.Count > 0 ? symbolStack.Peek() : null; string step = listView3.Items.Count.ToString(); string stateStackText = string.Join(" ", stateStack.Reverse().Select(s => s.ToString())); string symbolStackText = string.Join(" ", symbolStack.Reverse().Select(s => s.Name)); string inputStackText = string.Join(" ", inputStack.Reverse().Select(s => s.ToString())); string action = ""; if (actionTable.ContainsKey(new Tuple<int, Symbol>(state, new Symbol(inputChar.ToString(), true)))) { action = actionTable[new Tuple<int, Symbol>(state, new Symbol(inputChar.ToString(), true))]; } listView3.Items.Add(new ListViewItem(new string[] { step, stateStackText, symbolStackText, inputStackText, action })); StepByStepParse(inputStack.Peek().ToString()); } }
private void button8_Click(object sender, EventArgs e) { // 一键显示输入的待分析句子 while (inputStack.Count > 0) { int state = stateStack.Peek(); char inputChar = inputStack.Peek(); Symbol symbol = symbolStack.Count > 0 ? symbolStack.Peek() : null; string step = listView3.Items.Count.ToString(); string stateStackText = string.Join(" ", stateStack.Reverse().Select(s => s.ToString())); string symbolStackText = string.Join(" ", symbolStack.Reverse().Select(s => s.Name)); string inputStackText = string.Join(" ", inputStack.Reverse().Select(s => s.ToString())); string action = ""; if (actionTable.ContainsKey(new Tuple<int, Symbol>(state, new Symbol(inputChar.ToString(), true)))) { action = actionTable[new Tuple<int, Symbol>(state, new Symbol(inputChar.ToString(), true))]; } listView3.Items.Add(new ListViewItem(new string[] { step, stateStackText, symbolStackText, inputStackText, action })); OneKeyParse(inputStack.Peek().ToString()); }
原文地址: https://www.cveoy.top/t/topic/hfgH 著作权归作者所有。请勿转载和采集!