以下是补充的函数代码:

  1. Buildprod(string str):构造项目

public void Buildprod(string str) { string[] pro = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < pro.Length; i++) { string[] s = pro[i].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); SLRproNum.Add(new SLRNode(s[0], s[1])); for (int j = 0; j < s[1].Length; j++) { if (j == 0) { SLRobjNum.Add(new SLRNode(s[0], "." + s[1][0])); } else { SLRobjNum.Add(new SLRNode(s[0], s[1].Substring(0, j) + "." + s[1][j])); } } SLRobjNum.Add(new SLRNode(s[0], s[1] + ".")); } }

  1. Creteitemsets():项目集构建

public void Creteitemsets() { SLRitemsets item0 = new SLRitemsets(); item0.Container.Add(0); proitemset.Add(item0); for (int i = 0; i < proitemset.Count; i++) { SLRitemsets item = proitemset[i]; Dictionary<char, List> gotos = new Dictionary<char, List>(); for (int j = 0; j < item.Container.Count; j++) { int index = item.Container[j]; SLRNode node = SLRobjNum[index]; if (node.Right.IndexOf('.') < node.Right.Length - 1)//未到达末尾 { char next = node.Right[node.Right.IndexOf('.') + 1]; if (!gotos.ContainsKey(next)) { gotos.Add(next, new List()); } gotos[next].Add(index + 1); } } foreach (char key in gotos.Keys) { SLRitemsets newItem = new SLRitemsets(); List indexs = gotos[key]; for (int j = 0; j < indexs.Count; j++) { newItem.Container.Add(indexs[j]); } if (!IsInProItemset(newItem)) { proitemset.Add(newItem); } dfa[Pindex++] = new DFA(i, key, GetProItemsetIndex(newItem)); } } }

  1. GET_ANA():获取LR0分析表

public Table[][] GET_ANA() { int row = proitemset.Count; int col = Echar.Count + Nchar.Count + 1; SLRAna = new Table[row][]; for (int i = 0; i < row; i++) { SLRAna[i] = new Table[col]; for (int j = 0; j < col; j++) { SLRAna[i][j] = new Table(); } } for (int i = 0; i < row; i++) { SLRitemsets item = proitemset[i]; for (int j = 0; j < item.Container.Count; j++) { SLRNode node = SLRobjNum[item.Container[j]]; if (node.Right.IndexOf('.') < node.Right.Length - 1)//移进项目 { char next = node.Right[node.Right.IndexOf('.') + 1]; if (Echar.Contains(next)) { SLRAna[i][GetEcharIndex(next)] = new Table('s', GetDFA(i, next)); } } else//归约项目 { if (node.Left == "S'") { SLRAna[i][col - 1] = new Table('a', 0); } else { int index = GetProIndex(node.Left, node.Right.Substring(0, node.Right.Length - 1)); foreach (char c in follow[node.Left[0]]) { SLRAna[i][GetEcharIndex(c)] = new Table('r', index); } } } } } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (SLRAna[i][j].error) { RStr_ANA += "error\t"; } else { if (SLRAna[i][j].type == 's') { RStr_ANA += "s" + SLRAna[i][j].id.ToString() + "\t"; } else if (SLRAna[i][j].type == 'r') { RStr_ANA += "r" + SLRAna[i][j].id.ToString() + "\t"; } else if (SLRAna[i][j].type == 'a') { RStr_ANA += "acc\t"; } } } RStr_ANA += "\r\n"; } return SLRAna; }

  1. SLRAnaly():构造分析表

public void SLRAnaly() { Buildprod(RStr); Creteitemsets(); GET_ANA(); }

  1. sen_Analyze(string text):分析句子

public void sen_Analyze(string text) { Jz = new Analyze(); Jz.stack_state.Add("0"); string[] s = text.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < s.Length; i++) { Jz.Input_str.Add(s[i]); } int index = 0; while (!Success) { string state = Jz.stack_state[Jz.stack_state.Count - 1]; string symbol = Jz.Input_str[index]; int i = GetEcharIndex(symbol); Table t = SLRAna[int.Parse(state)][i]; if (t.type == 's')//移进 { Jz.stack_state.Add(t.id.ToString()); Jz.stack_symbol.Add(symbol); index++; } else if (t.type == 'r')//归约 { SLRNode node = SLRproNum[t.id]; int len = node.Right.Length; if (len > 1)//弹出符号栈 { for (int j = 0; j < len - 1; j++) { Jz.stack_state.RemoveAt(Jz.stack_state.Count - 1); Jz.stack_symbol.RemoveAt(Jz.stack_symbol.Count - 1); } } string newState = Jz.stack_state[Jz.stack_state.Count - 1]; Jz.stack_symbol.Add(node.Left); int i2 = GetEcharIndex(node.Left[0]); Table t2 = SLRAna[int.Parse(newState)][i2]; Jz.stack_state.Add(t2.id.ToString()); Jz.Tran_pro.Add(node.Left + "->" + node.Right); } else if (t.type == 'a')//接受 { Success = true; } else//错误 { Success = false; break; } } }

SLR(1) 分析器:C# 代码实现与解析

原文地址: https://www.cveoy.top/t/topic/f0NB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录