//Lista sentencias public void listaSentencias(nodo arbol) { arbol.hijos.ForEach(nodo => { switch (nodo.nombre) { case ":=": //Asignación nodo.hijos[0].valor = fnCalcularValor(nodo.hijos[1]); fnUpdateHashTable(nodo.hijos[0], nodo.hijos[1].tipoNodo); break; case "while": case "if": //Condición if fnCheckConditions(nodo.hijos[0]); for (int i = 1; i < nodo.hijos.Count; i++) { listaSentencias(nodo.hijos[i]); } break; case "do": listaSentencias(nodo.hijos[0]); fnCheckConditions(nodo.hijos[1]); break; case "cin": case "cout": fnCalcularValor(nodo.hijos[0]); break; } }); }
public nodo condicion() { nodo temp = new nodo("if", TipoNodo.sent, Tipo.sentencia, TipoNodo.sent); match("if"); //temp.nodos[0] = exp(); match("("); temp.hijos.Add(exp()); match(")"); match("then"); //temp.nodos[1] = lista_sentencia(); temp.hijos.Add(lista_sentencia()); Console.WriteLine(MainWindow.getToken().getLexema()); if (MainWindow.getToken().getLexema() == "else") { match("else"); //temp.nodos[2] = lista_sentencia(); temp.hijos.Add(lista_sentencia()); } match("end"); match(";"); return(temp); }
public nodo createVariableType(string variable, TipoNodo tipo, Tipo tipo1, TipoNodo tipoStatic) { nodo temp = new nodo(variable, tipo, tipo1, tipoStatic); match(variable); return(temp); }
public void initializeHashTable(nodo arbol, TipoNodo tipo) { for (int i = 0; i < arbol.hijos.Count; i++) { arbol.hijos[i].tipoNodo = tipo; pushIntoHashTable(arbol.hijos[i]); } }
public void fnUpdateHashTable(nodo arbol, TipoNodo tipo) { if (tablaHash.ContainsKey(arbol.nombre)) { arbol.tipoNodo = tablaHash[arbol.nombre].tipo; tablaHash[arbol.nombre].lista.Add(arbol.linea); if (arbol.tipoNodo == tipo || (arbol.tipoNodo == TipoNodo.float_number && tipo == TipoNodo.integer)) { Console.WriteLine("Variable: " + arbol.nombre + " Valor: " + arbol.valor); float valor = arbol.valor; if (arbol.tipoNodo == TipoNodo.integer) { valor = (float)Math.Truncate(valor); } tablaHash[arbol.nombre].valor = valor; return; } else { string desde, hasta; if (tipo == TipoNodo.integer) { hasta = "Int"; } else if (tipo == TipoNodo.float_number) { hasta = "Float"; } else { hasta = "Bool"; } if (arbol.tipoNodo == TipoNodo.integer) { desde = "Int"; } else if (arbol.tipoNodo == TipoNodo.float_number) { desde = "Float"; } else { desde = "Bool"; } erroresSemanticos += "Linea: " + arbol.linea + ". No se puede asignar un valor del tipo " + hasta + " a uno del tipo " + desde + "\n"; arbol.tipoNodo = TipoNodo.error; arbol.valor = 0; } } else { arbol.tipoNodo = TipoNodo.undefined; erroresSemanticos += "Linea: " + arbol.linea + " .La variable '" + arbol.nombre + "' no ha sido declarada\n"; } }
public nodo bloque() { nodo temp = null; match("{"); temp = lista_sentencia(); match("}"); return(temp); }
public nodo cout() { nodo temp = new nodo("cout", TipoNodo.sent, Tipo.sentencia, TipoNodo.sent); match("cout"); temp.hijos.Add(exp()); match(";"); //temp.nodos[0] = exp(); return(temp); }
public void codeIntermediate_process(nodo arbol) { emitRM("LD", mp, 0, ac, "Load max address"); emitRM("ST", ac, 0, ac, "Clear location 0"); foreach (var hijo in arbol.hijos) { cGen(hijo); } emitRO("HALT", 0, 0, 0, ""); }
public nodo iteracion() { nodo temp = new nodo("while", TipoNodo.sent, Tipo.sentencia, TipoNodo.sent); match("while"); match("("); temp.hijos.Add(exp()); match(")"); temp.hijos.Add(bloque()); return(temp); }
public nodo finID_RESERVED() { nodo temp = null; if (MainWindow.getToken().getTipo() == TipoToken.ID_RESERVED) { temp = new nodo(MainWindow.getToken().getLexema(), TipoNodo.exp, Tipo.id, TipoNodo.exp); MainWindow.iterator++; } return(temp); }
public nodo finID() { nodo temp = null; if (MainWindow.getToken().getTipo() == TipoToken.ID) { temp = new nodo(MainWindow.getToken().getLexema(), TipoNodo.id, Tipo.id, TipoNodo.exp); temp.linea = MainWindow.getToken().getFila(); MainWindow.iterator++; } return(temp); }
public nodo declaracion() { nodo temp = tipoVariable(); if (temp == null) { return(temp); } nodo q; q = finID(); if (q != null) { temp.hijos.Add(q); } else { return(temp); } if (MainWindow.getToken().getLexema() == ",") { while (MainWindow.getToken().getLexema() == ",") { match(","); q = finID(); if (q != null) { temp.hijos.Add(q); } else { MainWindow.erroresSintactico += "Se esperaba identificador Linea: " + MainWindow.getTokenAnterior().getFila() + " Columna: " + MainWindow.getTokenAnterior().getColumna() + "\n"; return(temp); } } } if (MainWindow.getToken().getLexema() == ";") { match(";"); } else { MainWindow.erroresSintactico += "Se esperaba ; Linea: " + MainWindow.getTokenAnterior().getFila() + " Columna: " + MainWindow.getTokenAnterior().getColumna() + "\n"; } return(temp); }
public nodo repeticion() { nodo temp = new nodo("do", TipoNodo.sent, Tipo.sentencia, TipoNodo.sent); match("do"); //temp.nodos[0] = lista_sentencia(); temp.hijos.Add(lista_sentencia()); match("until"); match("("); //temp.nodos[1] = exp(); temp.hijos.Add(exp()); match(")"); match(";"); return(temp); }
public TipoNodo fnCheckNode(nodo raiz) { if (raiz.hijos[0].tipoNodo != raiz.hijos[1].tipoNodo) { if (raiz.hijos[0].tipoNodo == TipoNodo.boolean || raiz.hijos[1].tipoNodo == TipoNodo.boolean) { erroresSemanticos += "Linea: " + raiz.linea + " .No se puede ejecutar la operacion " + raiz.nombre + " con esos tipos de datos.\n"; } return(TipoNodo.float_number); } else { return(raiz.hijos[0].tipoNodo); } }
public void cGen(nodo arbol) { if (arbol != null) { switch (arbol.tipoNodoStatic) { case TipoNodo.exp: genExp(arbol); break; case TipoNodo.sent: genStmt(arbol); break; } } }
public nodo asignar() { nodo temp = null; nodo izq = finID(); if (MainWindow.getToken().getLexema() == ":=") { temp = new nodo(MainWindow.getToken().getLexema(), TipoNodo.exp, Tipo.sentencia, TipoNodo.sent); match(":="); temp.hijos.Add(izq); temp.hijos.Add(exp()); match(";"); //temp.nodos[0] = izq; //temp.nodos[1] = exp(); } else if (MainWindow.getToken().getLexema() == "++" || MainWindow.getToken().getLexema() == "--") { temp = new nodo(":=", TipoNodo.exp, Tipo.sentencia, TipoNodo.sent); string s; if (MainWindow.getToken().getLexema() == "++") { s = "+"; } else { s = "-"; } nodo mas_menos = new nodo(s, TipoNodo.exp, Tipo.expresion, TipoNodo.exp); mas_menos.hijos.Add(new nodo(izq.nombre, TipoNodo.id, Tipo.id, TipoNodo.exp)); mas_menos.hijos.Add(new nodo("1", TipoNodo.integer, Tipo.constante, TipoNodo.exp, 1)); temp.hijos.Add(izq); temp.hijos.Add(mas_menos); match(MainWindow.getToken().getLexema()); match(";"); } else { error_ant("asignacion, incremento o decremento"); } return(temp); }
public nodo exp() { nodo temp, nuevo; temp = exp_simple(); while (MainWindow.getToken().getLexema() == "<=" || MainWindow.getToken().getLexema() == "<" || MainWindow.getToken().getLexema() == ">=" || MainWindow.getToken().getLexema() == ">" || MainWindow.getToken().getLexema() == "==" || MainWindow.getToken().getLexema() == "!=") { nuevo = new nodo(MainWindow.getToken().getLexema(), TipoNodo.exp, Tipo.operador, TipoNodo.exp); match(MainWindow.getToken().getLexema()); //nuevo.nodos[0] = temp; //nuevo.nodos[1] = exp_simple(); nuevo.hijos.Add(temp); nuevo.hijos.Add(exp_simple()); temp = nuevo; } return(temp); }
public nodo lista_variables() { nodo temp = finID(); nodo p = temp; while (MainWindow.getToken().getLexema() != ";") { nodo q; match(","); q = finID(); if (q == null) { break; } } return(temp); }
public nodo cin() { nodo temp = new nodo("cin", TipoNodo.sent, Tipo.sentencia, TipoNodo.sent); match("cin"); if (MainWindow.getToken().getTipo() == TipoToken.ID) { temp.hijos.Add(fin()); //temp.nodos[0] = fin(); } else { error_ant("identificador"); return(temp); } match(";"); return(temp); }
public nodo term() { nodo temp, nuevo; temp = fac(); while (MainWindow.getToken().getLexema() == "/" || MainWindow.getToken().getLexema() == "*" || MainWindow.getToken().getLexema() == "%") { switch (MainWindow.getToken().getLexema()) { case "*": nuevo = new nodo("*", TipoNodo.exp, Tipo.operador, TipoNodo.exp); match("*"); //nuevo.nodos[0] = temp; //nuevo.nodos[1] = fac(); nuevo.hijos.Add(temp); nuevo.hijos.Add(fac()); temp = nuevo; break; case "/": nuevo = new nodo("/", TipoNodo.exp, Tipo.operador, TipoNodo.exp); match("/"); //nuevo.nodos[0] = temp; //nuevo.nodos[1] = fac(); nuevo.hijos.Add(temp); nuevo.hijos.Add(fac()); temp = nuevo; break; case "%": nuevo = new nodo("%", TipoNodo.exp, Tipo.operador, TipoNodo.exp); match("%"); //nuevo.nodos[0] = temp; //nuevo.nodos[1] = fac(); nuevo.hijos.Add(temp); nuevo.hijos.Add(fac()); temp = nuevo; break; } } return(temp); }
public void pushIntoHashTable(nodo nodo) { if (tablaHash.ContainsKey(nodo.nombre)) { erroresSemanticos += "Linea: " + nodo.linea + ". Variable: " + nodo.nombre + " duplicada.:" + "\n"; return; } HashElement hashElement = new HashElement(); hashElement.nombre = nodo.nombre; hashElement.valor = 0; hashElement.tipo = nodo.tipoNodo; hashElement.location = location.ToString(); hashElement.lista = new List <int>(); hashElement.lista.Add(nodo.linea); tablaHash.Add(nodo.nombre, hashElement); Console.WriteLine(tablaHash[nodo.nombre].nombre); location++; }
public nodo fac() { nodo nuevo, temp; temp = fin(); while (MainWindow.getToken().getLexema() == "^") { if (MainWindow.getToken().getLexema() == "^") { nuevo = new nodo("^", TipoNodo.exp, Tipo.operador, TipoNodo.exp); match("^"); nuevo.hijos.Add(temp); nuevo.hijos.Add(fin()); //nuevo.nodos[0] = temp; //nuevo.nodos[1] = fin(); temp = nuevo; } } return(temp); }
//match(","); //while (true) //{ // q = finID(); // if (q == null && MainWindow.getToken().getLexema() == ";") { // break; // } // if (q != null) // { // temp.hijos.Add(q); // } // if(q!=null && (MainWindow.getToken().getTipo() != TipoToken.ID && MainWindow.getToken().getLexema() != "," && MainWindow.getToken().getLexema() != ";")) // { // MainWindow.erroresSintactico += "Se esperaba ; Linea: " + MainWindow.getTokenAnterior().getFila() + " Columna: " + MainWindow.getTokenAnterior().getColumna() + "\n"; // break; // } // if(MainWindow.getToken().getLexema() == ",") // { // match(","); // } //} public nodo tipoVariable() { nodo temp = null; switch (MainWindow.getToken().getLexema()) { case "int": temp = createVariableType("int", TipoNodo.integer, Tipo.constante, TipoNodo.exp); break; case "float": temp = createVariableType("float", TipoNodo.float_number, Tipo.constante, TipoNodo.exp); break; case "boolean": temp = createVariableType("boolean", TipoNodo.boolean, Tipo.constante, TipoNodo.exp); break; } return(temp); }
public nodo lista_sentencia() { nodo temp = new nodo("sentencias", TipoNodo.sent, Tipo.sentencia, TipoNodo.sent); nodo s = new nodo(); temp.hijos.Add(sentencia()); while (MainWindow.getToken().getLexema() != "end" && MainWindow.getToken().getLexema() != "else" && MainWindow.getToken().getLexema() != "until" && MainWindow.getToken().getLexema() != "}" && MainWindow.iterator < MainWindow.listaTokens.Count) { s = sentencia(); if (s != null) { temp.hijos.Add(s); } else { MainWindow.iterator++; } } return(temp); }
public nodo programa() { nodo temp = null; if (MainWindow.getToken().getLexema() == "main") { match("main"); temp = new nodo("main", TipoNodo.exp, Tipo.expresion, TipoNodo.exp); match("{"); //temp.nodos[0] = lista_declaracion(); temp.hijos.Add(lista_declaracion()); //temp.nodos[1] = lista_sentencia(); temp.hijos.Add(lista_sentencia()); match("}"); } else { Console.WriteLine("error"); } return(temp); }
//Lista declaraciones public void listaDeclaraciones(nodo arbol) { Console.WriteLine(arbol.hijos.Count); for (int i = 0; i < arbol.hijos.Count; i++) { switch (arbol.hijos[i].tipoNodo) { case TipoNodo.integer: initializeHashTable(arbol.hijos[i], TipoNodo.integer); break; case TipoNodo.float_number: initializeHashTable(arbol.hijos[i], TipoNodo.float_number); break; case TipoNodo.boolean: initializeHashTable(arbol.hijos[i], TipoNodo.boolean); break; } } }
public nodo fin() { float real; int integer; nodo temp = null; if (MainWindow.getToken().getLexema() == "(") { match("("); temp = exp(); match(")"); } else if (int.TryParse(MainWindow.getToken().getLexema(), out integer)) { Console.WriteLine("Numero entero -> " + integer); temp = new nodo(MainWindow.getToken().getLexema(), TipoNodo.integer, Tipo.constante, TipoNodo.exp); temp.valor = integer; MainWindow.iterator++; } else if (float.TryParse(MainWindow.getToken().getLexema(), out real)) { Console.WriteLine("Numero real -> " + real); temp = new nodo(MainWindow.getToken().getLexema(), TipoNodo.float_number, Tipo.constante, TipoNodo.exp); temp.valor = real; MainWindow.iterator++; } else if (MainWindow.getToken().getTipo() == TipoToken.ID) { temp = new nodo(MainWindow.getToken().getLexema(), TipoNodo.id, Tipo.id, TipoNodo.exp); temp.linea = MainWindow.getToken().getFila(); MainWindow.iterator++; } else { Console.WriteLine("error ->" + MainWindow.getToken().getLexema()); } return(temp); }
public nodo sentencia() { nodo temp = null; Token token = MainWindow.getToken(); switch (token.getLexema()) { case "if": temp = condicion(); return(temp); case "cin": temp = cin(); return(temp); case "cout": temp = cout(); return(temp); case "do": temp = repeticion(); return(temp); case "while": temp = iteracion(); return(temp); } if (token.getTipo() == TipoToken.ID) { temp = asignar(); return(temp); } else { MainWindow.erroresSintactico += "Valor inesperado -> " + MainWindow.getToken().getLexema() + " Linea: " + MainWindow.getTokenAnterior().getFila() + " Columna: " + MainWindow.getTokenAnterior().getColumna() + "\n"; return(temp); } }
public nodo lista_declaracion() { nodo temp = new nodo("declaracion", TipoNodo.exp, Tipo.expresion, TipoNodo.exp); nodo q = declaracion(); if (q != null) { temp.hijos.Add(q); } while (q != null) { q = declaracion(); if (q != null) { temp.hijos.Add(q); } else { break; } } return(temp); }
public nodo exp_simple() { nodo temp, nuevo; temp = term(); Console.WriteLine(MainWindow.getToken().getLexema()); while (MainWindow.getToken().getLexema() == "+" || MainWindow.getToken().getLexema() == "-" || MainWindow.getToken().getLexema() == "++" || MainWindow.getToken().getLexema() == "--") { Console.WriteLine(MainWindow.getToken().getLexema()); switch (MainWindow.getToken().getLexema()) { case "+": nuevo = new nodo(); nuevo.nombre = MainWindow.getToken().getLexema(); nuevo.tipoNodoStatic = TipoNodo.exp; nuevo.tipo = Tipo.operador; match("+"); //nuevo.nodos[0] = temp; //nuevo.nodos[1] = term(); nuevo.hijos.Add(temp); nuevo.hijos.Add(term()); temp = nuevo; break; case "-": nuevo = new nodo(); nuevo.nombre = MainWindow.getToken().getLexema(); nuevo.tipoNodoStatic = TipoNodo.exp; nuevo.tipo = Tipo.operador; match("-"); //nuevo.nodos[0] = temp; //nuevo.nodos[1] = term(); nuevo.hijos.Add(temp); nuevo.hijos.Add(term()); temp = nuevo; break; case "++": nuevo = new nodo(":=", TipoNodo.exp, Tipo.sentencia, TipoNodo.sent); nodo mas = new nodo("+", TipoNodo.exp, Tipo.operador, TipoNodo.exp); mas.hijos.Add(temp); mas.hijos.Add(new nodo("1", TipoNodo.integer, Tipo.constante, TipoNodo.exp, 1)); nuevo.hijos.Add(temp); nuevo.hijos.Add(mas); match("++"); temp = nuevo; break; case "--": nuevo = new nodo(":=", TipoNodo.exp, Tipo.sentencia, TipoNodo.sent); nodo menos = new nodo("-", TipoNodo.exp, Tipo.operador, TipoNodo.exp); menos.hijos.Add(temp); menos.hijos.Add(new nodo("1", TipoNodo.integer, Tipo.constante, TipoNodo.exp, 1)); nuevo.hijos.Add(temp); nuevo.hijos.Add(menos); match("++"); temp = nuevo; break; } } return(temp); }