public class SLRNode/n{/n public string Left;/n public string Right;/n public SLRNode(string Left, string Right)/n {/n this.Left = Left;/n this.Right = Right;/n }/n}/n//项目集类/npublic class SLRitemsets/n{/n public List Container/n = new List(100);/n //记录项目在项目集合中的序号/n}/n/n//DFA结点/npublic struct DFA/n{/n public int from;/n public char symbol;/n public int to;/n public DFA(int from, char symbol, int to)/n {/n this.from = from;/n this.symbol = symbol;/n this.to = to;/n }/n}/npublic class Table/n{/n public bool error;//是否为ERROR/n public char type;//结点类型/n public int id;//数值/n public Table()/n {/n this.error = true;/n }/n public Table(char type, int id)/n {/n this.type = type;/n this.id = id;/n this.error = false;/n }/n}/npublic DFA[] dfa = new DFA[100];/n public int Pindex = 0; //dfa数组指针/n public Table[][] SLRAna;//分析表/n public Analyze Jz;/n public bool Success = false;/n public List SLRproNum = new List(50);//产生式 列表/n public List SLRobjNum = new List(50);//项目 列表/n public List proitemset = new List(100);//项目集合/n public List Gy_obj = new List(50);//归约项目序号集合/n public List Gy_itemset = new List(50);//含有归约项目的集合的序号 的集合/n public List Nchar = new List(50);//非终结符集合/n public List Echar = new List(50);//终结符集合/n/n public List[] Follow; //每个非终结符的follow集合/n/n public string RStr = /'/';/n public string RStr_obitemset = /'/';//输出返回/n public string RStr_DFA = /'/';/n public string RStr_ANA = /'/';/npublic Table[][] GET_ANA()/n {/n SLRAnaly();/n RStr_ANA += /'//r//nSLR0分析表://r//n /';/n int i;/n for (i = 0; i < Echar.Count; i++)/n {/n RStr_ANA += Echar[i].ToString() + /' /';/n }/n for (i = 0; i < Nchar.Count; i++)/n {/n RStr_ANA += Nchar[i].ToString() + /' /';/n }/n RStr_ANA += /'//r//n/';/n for (i = 0; i < proitemset.Count; i++)/n {/n RStr_ANA += i.ToString() + /' /';/n for (int j = 0; j < Echar.Count + Nchar.Count; j++)/n {/n/n if (SLRAna[i][j].error)/n {/n RStr_ANA += /' /' + /' /';/n }/n else if (i == 1 && j == Echar.Count - 1)/n {/n RStr_ANA += /'AC/' + /' /';/n }/n else if (SLRAna[i][j].type != 'N')/n {/n RStr_ANA += SLRAna[i][j].type.ToString() + SLRAna[i][j].id.ToString() + /' /';/n }/n else/n RStr_ANA += SLRAna[i][j].id.ToString() + /' /';/n }/n RStr_ANA += /'//r//n/';/n }/n/n return SLRAna;/n/n }/npublic void SLRAnaly()/n {/n Table tnode = new Table();/n/n SLRAna = new Table[proitemset.Count][];/n for (int i = 0; i < proitemset.Count; i++)/n SLRAna[i] = new Table[Echar.Count + Nchar.Count];/n/n for (int i = 0; i < proitemset.Count; i++)//初始化 赋予ERROR属性/n for (int j = 0; j < Echar.Count + Nchar.Count; j++)//为终结符加r状态 /n SLRAna[i][j] = tnode;/n/n tnode = new Table('A', 0);/n SLRAna[1][FindID(Echar, '#')] = tnode;//项目集1必定是接受项目 构建[1][#]:acc的情况 先直接赋值好 dfa里没有/n/n for (int i = 0; i < Gy_itemset.Count; i++)/n {/n SLRNode item = SLRobjNum[proitemset[Gy_itemset[i]].Container[0]];/n char left = item.Left[0];/n List follow = GetFollow(left);/n foreach (char c in follow)/n {/n int CID = FindID(Echar, c);/n SLRAna[Gy_itemset[i]][CID] = new Table('r', Find_pro(item));/n }/n }/n/n for (int i = 0; i < Pindex; i++)/n {/n if (isFinalsymbol(dfa[i].symbol))//symbol为非终结符 添加状态N/n {/n int CID = FindID(Nchar, dfa[i].symbol);/n SLRAna[dfa[i].from][CID + Echar.Count] = new Table('N', dfa[i].to);/n }/n else //不是归约项目 添加状态S/n {/n int CID = FindID(Echar, dfa[i].symbol);/n SLRAna[dfa[i].from][CID] = new Table('S', dfa[i].to);/n }/n }/n }/npublic List GetFollow(char c)/n {/n List follow = new List();/n if (c == 'E')/n follow.Add('#');/n foreach (SLRNode node in SLRproNum)/n {/n int index = node.Right.IndexOf(c);/n if (index != -1 && index < node.Right.Length - 1)/n {/n char next = node.Right[index + 1];/n if (isFinalsymbol(next))/n follow.Add(next);/n else/n {/n List first = GetFirst(next);/n if (first.Contains('#'))/n {/n first.Remove('#');/n follow.AddRange(first);/n follow.AddRange(GetFollow(node.Left[0]));/n }/n else/n {/n follow.AddRange(first);/n }/n }/n }/n else if (index != -1 && index == node.Right.Length - 1)/n {/n follow.AddRange(GetFollow(node.Left[0]));/n }/n }/n follow = follow.Distinct().ToList();/n return follow;/n }/n/n/n public List GetFirst(char c)/n {/n List first = new List();/n if (isFinalsymbol(c))/n first.Add(c);/n else/n {/n foreach (SLRNode node in SLRproNum)/n {/n if (node.Left[0] == c)/n {/n if (node.Right[0] == c)/n continue;/n else if (isFinalsymbol(node.Right[0]))/n first.Add(node.Right[0]);/n else/n {/n List subFirst = GetFirst(node.Right[0]);/n if (subFirst.Contains('#'))/n {/n subFirst.Remove('#');/n first.AddRange(subFirst);/n first.AddRange(GetFirst(node.Right[1]));/n }/n else/n {/n first.AddRange(subFirst);/n }/n }/n }/n }/n }/n first = first.Distinct().ToList();/n return first;/n }/

SLR语法分析器 - 分析表构建详解

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

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