public Dictionary <int, List <int> > AssignFollow(Graph graph) { foreach (var item in graph.vertices) { if (item.Value.value == "*" || item.Value.value == "?") { Edge edge = graph.edges.Values.FirstOrDefault(x => x.Start == item.Key); vertice vertice = graph.vertices.FirstOrDefault(x => x.Key == edge.End).Value; foreach (var follow in vertice.Last) { if (Follows.ContainsKey(follow)) { List <int> templist = new List <int>(); templist = vertice.First.FindAll(x => Follows[follow].Contains(x) == false); templist.AddRange(Follows[follow]); Follows.Remove(follow); Follows.Add(follow, templist); } else { Follows.Add(follow, vertice.First); } } } else if (item.Value.value == ".") { Edge edge = graph.edges.Values.FirstOrDefault(x => x.Start == item.Key); Edge edge2 = graph.edges.Values.LastOrDefault(x => x.Start == item.Key); vertice vertice = graph.vertices.FirstOrDefault(x => x.Key == edge.End).Value; vertice vertice2 = graph.vertices.FirstOrDefault(x => x.Key == edge2.End).Value; foreach (var follow in vertice.Last) { if (Follows.ContainsKey(follow)) { List <int> templist = new List <int>(); templist = vertice2.First.FindAll(x => Follows[follow].Contains(x) == false); templist.AddRange(Follows[follow]); Follows.Remove(follow); Follows.Add(follow, templist); } else { Follows.Add(follow, vertice2.First); } } } } return(Follows); }
public vertice AssignFirst(Graph graph, int position) { vertice first = new vertice(); vertice vert = graph.vertices.FirstOrDefault(x => x.Key == position).Value; if (vert.First == null) { if (vert.value == "|" || vert.value == ".") { List <int> hijos = new List <int>(); vertice v1 = new vertice(); vertice v2 = new vertice(); List <int> tempvert2 = new List <int>(); hijos.Add(graph.edges.FirstOrDefault(x => x.Value.Start == position).Value.End); v1 = (AssignFirst(graph, hijos[0])); hijos.Add(graph.edges.LastOrDefault(x => x.Value.Start == position).Value.End); v2 = AssignFirst(graph, hijos[1]); if (vert.value == "|") { vert.First = v1.First.Union(v2.First).ToList(); } else if (vert.value == ".") { if (v1.nullable == true) { vert.First = v1.First.Union(v2.First).ToList(); } else { vert.First = v1.First; } } return(vert); } else { int pos = graph.edges.FirstOrDefault(x => x.Value.Start == position).Value.End; vert.First = AssignFirst(graph, pos).First; } } else if (vert.value != "*" || vert.value != "?" || vert.value != "|") { return(vert); } return(vert); }
public Graph CreateTree(List <string> expresion, int nVertices, int nEdges) { Dictionary <int, vertice> vertices = new Dictionary <int, vertice>(); Dictionary <int, Edge> edges = new Dictionary <int, Edge>(); //Graph graph = new Graph(); if (expresion.Count == 0) { Graph graph = new Graph(vertices, edges, 0); return(graph); } List <string> temp = new List <string>(); string valor = string.Empty; bool bandera = false; //if (expresion[expresion.Count-1] != "(") //{ // expresion.RemoveAt(expresion.Count - 1); // temp = expresion; // bandera = temp.TrueForAll(x => x == "("); //} if (expresion.Count == 1 || bandera == true) { nVertices++; vertice vert = new vertice(); vert.value = (bandera == true) ? valor : expresion[0]; if (expresion[0] != "?" && expresion[0] != "*" && expresion[0] != "|" && expresion[0] != ".") { vert.First = new List <int>(); vert.First.Add(NoTerminales); vert.Last = new List <int>(); vert.Last.Add(NoTerminales); NoTerminales++; vert.nullable = false; } else if (expresion[0] == "?" || expresion[0] == "*") { vert.nullable = true; } else { vert.nullable = false; } vertices.Add(nVertices, vert); Graph graph = new Graph(vertices, edges, nVertices); return(graph); } List <string> A = expresion; List <string> B = expresion; int parentesiscount = 0; bool expIsConcat = false; for (int i = 0; i < expresion.Count - 1 && !expIsConcat; i++) { if (expresion[i] == "(") { parentesiscount++; } if (expresion[i] == ")") { parentesiscount--; } if (expresion[i + 1] == "." && parentesiscount == 0) { expIsConcat = true; if (expresion[i] == "(") { A = expresion.GetRange(i, i + 1); } else { A = expresion.GetRange(0, i + 1); } B = expresion.GetRange(i + 2, expresion.Count - i - 2); } } // Si la expresion es de la forma a|b if (expIsConcat) { // Crear nodo de | nVertices++; vertice vert = new vertice(); vert.value = "."; vert.nullable = false; vertices.Add(nVertices, vert); // Crear el subtree de las expresiones Graph subTreeA = CreateTree(A, nVertices, nEdges); Graph subTreeB = CreateTree(B, nVertices + subTreeA.vertices.Count, nEdges + subTreeA.edges.Count); // Agregar los vertices del subtree subTreeA.vertices.ToList().ForEach(x => vertices.Add(x.Key, x.Value)); subTreeB.vertices.ToList().ForEach(x => vertices.Add(x.Key, x.Value)); // Agregar los edges del subtree subTreeA.edges.ToList().ForEach(x => edges.Add(edges.Count + 1, x.Value)); subTreeB.edges.ToList().ForEach(x => edges.Add(edges.Count + 1, x.Value)); // Agregar el edge que une al nodo y al subtree nEdges = edges.Count + 1; Edge newEdgeA = new Edge(nVertices, subTreeA.head); edges.Add(nEdges, newEdgeA); nEdges = edges.Count + 1; Edge newEdgeB = new Edge(nVertices, subTreeB.head); edges.Add(nEdges, newEdgeB); // Retornar el grafo Graph graph = new Graph(vertices, edges, nVertices); return(graph); } // Revisar que la expresion sea de la forma a|b List <string> a = expresion; List <string> b = expresion; int parentesisCount = 0; bool expIsOr = false; for (int i = 0; i < expresion.Count - 1 && !expIsOr; i++) { if (expresion[i] == "(") { parentesisCount++; } if (expresion[i] == ")") { parentesisCount--; } if (parentesisCount == 0 && expresion[i + 1] == "|") { expIsOr = true; a = expresion.GetRange(0, i + 1); b = expresion.GetRange(i + 2, expresion.Count - i - 2); } } // Si la expresion es de la forma a|b if (expIsOr) { // Crear nodo de | nVertices++; vertice vert = new vertice(); vert.value = "|"; vert.nullable = false; vertices.Add(nVertices, vert); // Crear el subtree de las expresiones Graph subTreeA = CreateTree(a, nVertices, nEdges); Graph subTreeB = CreateTree(b, nVertices + subTreeA.vertices.Count, nEdges + subTreeA.edges.Count); // Agregar los vertices del subtree subTreeA.vertices.ToList().ForEach(x => vertices.Add(x.Key, x.Value)); subTreeB.vertices.ToList().ForEach(x => vertices.Add(x.Key, x.Value)); // Agregar los edges del subtree subTreeA.edges.ToList().ForEach(x => edges.Add(edges.Count + 1, x.Value)); subTreeB.edges.ToList().ForEach(x => edges.Add(edges.Count + 1, x.Value)); // Agregar el edge que une al nodo y al subtree nEdges = edges.Count + 1; Edge newEdgeA = new Edge(nVertices, subTreeA.head); edges.Add(nEdges, newEdgeA); nEdges = edges.Count + 1; Edge newEdgeB = new Edge(nVertices, subTreeB.head); edges.Add(nEdges, newEdgeB); // Retornar el grafo Graph graph = new Graph(vertices, edges, nVertices); return(graph); } // Si la expresion es del tipo (s) else if (expresion.Count >= 2 && expresion[0] == "(" && expresion[expresion.Count - 1] == ")") { // Devuelve el arbol de s return(CreateTree(expresion.GetRange(1, expresion.Count - 2), nVertices, nEdges)); } // Si la expresion es de la forma s* else if ((expresion.Count == 2 && expresion[1] == "*") || (expresion.Count > 2 && expresion[0] == "(" && expresion[expresion.Count - 2] == ")" && expresion[expresion.Count - 1] == "*")) { // Crear el nodo del * nVertices++; vertice vert = new vertice(); vert.value = "*"; vert.nullable = true; vertices.Add(nVertices, vert); // Crear el subtree de la expresion Graph subTree = CreateTree(expresion.GetRange(0, expresion.Count - 1), nVertices, nEdges); // Agregar los vertices del subtree subTree.vertices.ToList().ForEach(x => vertices.Add(x.Key, x.Value)); // Agregar los edges del subtree subTree.edges.ToList().ForEach(x => edges.Add(x.Key, x.Value)); // Agregar el edge que une al nodo y al subtree nEdges = edges.Count + 1; Edge newEdge = new Edge(nVertices, subTree.head); edges.Add(nEdges, newEdge); // Retornar el grafo Graph graph = new Graph(vertices, edges, nVertices); return(graph); } else if ((expresion.Count == 2 && expresion[1] == "?") || (expresion.Count > 2 && expresion[0] == "(" && expresion[expresion.Count - 2] == ")" && expresion[expresion.Count - 1] == "?")) { // Crear el nodo del * nVertices++; vertice vert = new vertice(); vert.value = "?"; vert.nullable = true; vertices.Add(nVertices, vert); // Crear el subtree de la expresion Graph subTree = CreateTree(expresion.GetRange(0, expresion.Count - 1), nVertices, nEdges); // Agregar los vertices del subtree subTree.vertices.ToList().ForEach(x => vertices.Add(x.Key, x.Value)); // Agregar los edges del subtree subTree.edges.ToList().ForEach(x => edges.Add(x.Key, x.Value)); // Agregar el edge que une al nodo y al subtree nEdges = edges.Count + 1; Edge newEdge = new Edge(nVertices, subTree.head); edges.Add(nEdges, newEdge); // Retornar el grafo Graph graph = new Graph(vertices, edges, nVertices); return(graph); } Graph graphtemp = new Graph(vertices, edges, 0); return(graphtemp); }
public List <string> ValidarActions() { GetActions(); bool concatenacion = true; int count = 0; bool parentesis = false; bool parentesis2 = false; List <string> listadeNodostemp = new List <string>(); List <List <string> > listaexpresionestemp = new List <List <string> >(); List <string> listadeNodos = new List <string>(); foreach (var item in TokenDic.Values) { listadeNodos.Clear(); listadeNodostemp.Clear(); foreach (var dato in item) { if (dato != "(" && dato != "*" && dato != "?" && dato != ")" && dato != "|") { if (parentesis == true) { listadeNodos.Add(dato); } else if (concatenacion == true) { listadeNodostemp.Clear(); listadeNodostemp.AddRange(listadeNodos); listadeNodos.Clear(); if (listadeNodostemp.Count > 0) { listadeNodos.Add("("); listadeNodos = listadeNodos.Concat(listadeNodostemp).ToList(); listadeNodos.Add("."); listadeNodos.Add(dato); listadeNodos.Add(")"); } else { listadeNodos.Add(dato); } concatenacion = true; } else { listadeNodostemp.Clear(); listadeNodostemp.AddRange(listadeNodos); listadeNodos.Clear(); listadeNodos.Add("("); listadeNodos = listadeNodos.Concat(listadeNodostemp).ToList(); listadeNodos.Add(dato); listadeNodos.Add(")"); concatenacion = true; } } else if (dato == "(") { if (listadeNodos.Last() != "|") { listadeNodos.Add("."); } listadeNodos.Add("("); parentesis = true; } else if (dato == ")") { listadeNodos.Add(")"); parentesis = false; } else if (dato == "|") { listadeNodos.Add(dato); concatenacion = false; } else { if (dato == "*" || dato == "?") { listadeNodostemp.Clear(); string last = listadeNodos.Last(); if (last != ")") { listadeNodos.RemoveAt(listadeNodos.Count - 1); listadeNodostemp.AddRange(listadeNodos); listadeNodos.Clear(); listadeNodos = listadeNodos.Concat(listadeNodostemp).ToList(); listadeNodos.Add("("); listadeNodos.Add("("); listadeNodos.Add(last); listadeNodos.Add(dato); listadeNodos.Add(")"); } else if (concatenacion) { int position = listadeNodos.Count - 2; List <string> anteriores = new List <string>(); anteriores = listadeNodos.GetRange(0, position); listadeNodostemp = listadeNodos.GetRange(position, 1); listadeNodos.Clear(); listadeNodos = listadeNodos.Concat(anteriores).ToList(); listadeNodos.Add("("); listadeNodos = listadeNodos.Concat(listadeNodostemp).ToList(); listadeNodos.Add(dato); listadeNodos.Add(")"); listadeNodos.Add(")"); } else { int position = listadeNodos.LastIndexOf("("); List <string> anteriores = new List <string>(); anteriores = listadeNodos.GetRange(0, position); listadeNodostemp = listadeNodos.GetRange(position, listadeNodos.Count - position); listadeNodos.Clear(); listadeNodos = listadeNodos.Concat(anteriores).ToList(); listadeNodos.Add("("); listadeNodos = listadeNodos.Concat(listadeNodostemp).ToList(); listadeNodos.Add(dato); listadeNodos.Add(")"); } } } } count++; if (count <= TokenDic.Values.Count) { listadeNodostemp.Clear(); listadeNodostemp.AddRange(listadeNodos); listadeNodos.Clear(); if (listadeNodostemp.ElementAt(listadeNodostemp.Count - 1) != ")") { listadeNodos.Add("("); listadeNodos.AddRange(listadeNodostemp); listadeNodos.Add(")"); } else { listadeNodos.AddRange(listadeNodostemp); } List <string> temp = new List <string>(); temp.AddRange(listadeNodos); listaexpresionestemp.Add(temp); concatenacion = true; } } listadeNodos.Clear(); foreach (var expresion in listaexpresionestemp) { listadeNodostemp.Clear(); listadeNodostemp.AddRange(listadeNodos); listadeNodos.Clear(); if (listadeNodostemp.Count > 0) { listadeNodos.Add("("); listadeNodos = listadeNodos.Concat(listadeNodostemp).ToList(); listadeNodos.Add("|"); if (expresion.First() != "(") { listadeNodos.Add("("); listadeNodos.AddRange(expresion); listadeNodos.Add(")"); } else { listadeNodos.AddRange(expresion); } listadeNodos.Add(")"); } else { listadeNodos.AddRange(expresion); } } listadeNodostemp.Clear(); listadeNodostemp.AddRange(listadeNodos); listadeNodos.Clear(); listadeNodos.Add("("); listadeNodos = listadeNodos.Concat(listadeNodostemp).ToList(); listadeNodos.Add("."); listadeNodos.Add("#"); listadeNodos.Add(")"); string expresiioon = String.Join(" ", listadeNodos); graph = arbol.CreateTree(listadeNodos, 0, 0); //Asigno first vertice graph1 = arbol.AssignFirst(graph, graph.head); //Asigno Last vertice graph2 = arbol.AssignLast(graph, graph.head); int nt = arbol.NoTerminales; Follows = arbol.AssignFollow(graph); if (Follows.Count < nt) { List <int> tem = new List <int>(); Follows.Add(Follows.Count + 1, tem); } foreach (var vertu in graph.vertices) { List <List <string> > TempData = new List <List <string> >(); if (vertu.Value.value != "*" && vertu.Value.value != "#" && vertu.Value.value != "+" && vertu.Value.value != "?" && vertu.Value.value != "|" && vertu.Value.value != "." && transitions.Keys.Contains(vertu.Value.value) == false) { transitions.Add(vertu.Value.value, TempData); } } P.Add(1, graph.vertices.ToList().FirstOrDefault(x => x.Key == 1).Value.First); try { P = arbol.CreateTransitions(graph, transitions, P, 1); } catch (Exception ex) { string p = string.Empty; } error.Add("fin"); return(actions); }