You are on page 1of 12

Teora De Compilado res METODO DE

Profesor: CHOLESKY
Vsquez Claros Carlos

Integrantes:
Carrillo La Rosa Jess Chavez Gamarra Ingrid Chumpitazi Huapaya Jorge

Facultad de Ingeniera E.A.P Ingeniera Informtica

ANLISIS LXICO
Un analizador lxico o analizador lexicogrfico (en ingls scanner) es un programa que recibe como entrada el cdigo fuente de otro programa (secuencia de caracteres) y produce una salida compuesta de tokens (componentes lxicos) o smbolos. Estos tokens sirven para una posterior etapa del proceso de traduccin, siendo la entrada para el analizador sintctico (en ingls parser). Esto se realiza mediante una serie de rutinas que identifican cada instruccin, se debe realizar este proceso para que el anlisis posterior, el sintctico, pueda procesar la sentencia ingresada. METODOLOGA DE DESARROLLO Hemos hecho esta etapa utilizando los conocimientos adquiridos en clase y aplicando los criterios personales del equipo de trabajo con caractersticas del Lenguaje de Programacin C#. Los siguientes cdigos lo encontramos en el formulario FrmCholesky.cs

//-----------------------ANALISIS LEXICO-------------------------------list_Palabras.Items.Clear (); list_LineasCapturadas.Items.Clear (); for (int i = 0; i < lineas.Length; i++) { String temp = lineas[i].Replace("\r\n", "").Replace ("\n\r", "").Replace("\r", "").Replace("\n", "").Trim().Replace(" ", ""); list_LineasCapturadas.Items.Add (temp); }

Separando Los Tokens con las funciones


public String getFunc(String cadena) { string pattern = @"^[a-zA-Z]+"; Regex regex = new Regex(pattern); string aaaa = regex.Replace(cadena, ""); return cadena.Replace(aaaa, ""); } public String getCoor(string cadena) { try { string f = getFunc(cadena); cadena = cadena.Replace(f, "").Replace("('", "").Replace("')", "").Replace(";", ""); cadena = cadena.Replace("X1", "").Replace("x1", "").Replace("X2", "").Replace("x2", "");

Manual Tcnico | Mtodo de Cholesky 2

Facultad de Ingeniera E.A.P Ingeniera Informtica


cadena = cadena.Replace("X3", "").Replace("x3", "").Replace("(", "").Replace(")", ""); return cadena; } catch (Exception asx) { return "Error : " + asx; } }

ANLISIS SINTCTICO
Un analizador sintctico (en ingls parser) es una de las partes de un compilador que transforma su entrada en un rbol de derivacin. El anlisis sintctico convierte el texto de entrada en otras estructuras (comnmente rboles), que son ms tiles para el posterior anlisis y capturan la jerarqua implcita de la entrada. Un analizador lxico crea tokens de una secuencia de caracteres de entrada y son estos tokens los que son procesados por el analizador sintctico para construir la estructura de datos, por ejemplo un rbol de anlisis o rboles de sintaxis abstracta. METODOLOGA DE DESARROLLO Para determinar si la sintaxis de la funcin es correcta, definimos un patrn y lo utilizamos para realizar la validacin mediante Expresiones Regulares, todo esto se encuentra dentro del mtodo revisarSintaxis() el cual nos retorna un objeto de tipo bool con los valores true en caso de que la cadena cumpla con el patrn establecido, o false si no cumple.
private bool revisarSintaxis(string p, int k) { string patron = ""; if (k == 0) { patron = @"[a-zA-Z]+[(][0-9][)];$"; } else { if (k > 0 && k < 4) { patron = @"[a-zA-Z]+[(]['][+]?[0-9]+([.][0-9]+)?(X1|x1)[+][09]+([.][0-9]+)?(X2|x2)[+][0-9]+([.][0-9]+)?(X3|x3)[=][+]?[09]+([.][0-9]+)?['][)];$"; } else { if (k == 4) { patron = @"[a-zA-Z]+[(][)];$"; } } } Regex regex = new Regex(patron); return regex.IsMatch(p); }

Manual Tcnico | Mtodo de Cholesky 3

Facultad de Ingeniera E.A.P Ingeniera Informtica

ANLISIS SEMNTICO
El anlisis semntico utiliza como entrada el rbol sintctico detectado por el anlisis sintctico para comprobar restricciones de tipo y otras limitaciones semnticas y preparar la generacin de cdigo.
METODOLOGA DE DESARROLLO
//VALIDAR PARAMETROS DE ENTRADA for (k = 0; k < list_LineasCapturadas.Items.Count; k++) { string Errores = ""; string lineaActual = list_LineasCapturadas.Items[k] + ""; if (revisarSintaxis(lineaActual, k))//Si se ha escrito bien la funcion completa { //Validando las Funciones y los parametros string ptrn_funct = "[a-zA-Z]+"; string ptrn_param1 = "^[0-9]$"; string ptrn_param2 = "^[-]?[0-9]+([.][0-9]+)?[+][0-9]+([.][09]+)?[+][0-9]+([.][0-9]+)?[=][+]?[0-9]+([.][0-9]+)?$"; string ptrn_param3 = ""; string temp_funct = getFunc(lineaActual);//Obtiene la funcion string temp_param = getCoor(lineaActual);//Obtiene valores de la funcion

Regex regx_funct = new Regex(ptrn_funct); Regex regx_param = new Regex(""); ; if (k == 0) { regx_param = new Regex(ptrn_param1); } else { if (k > 0 && k < 4) { regx_param = new Regex(ptrn_param2); } else { if (k == 4) { regx_param = new Regex(ptrn_param3); } } }

bool temp_f = regx_funct.IsMatch(temp_funct); bool temp_p = regx_param.IsMatch(temp_param); /////////////////////////////////////////////////////////

Manual Tcnico | Mtodo de Cholesky 4

Facultad de Ingeniera E.A.P Ingeniera Informtica

if (k == 0) { f = 0; } else { if (k > 0 && k < 4) { f = 1; } else { if (k == 4) { f = 2; } } } if (temp_p) { if (string.Compare(funciones[f], temp_funct) == 0) { //OK FUNCION CORRECTA list_Comandos_Interceptar.Items.Add("" + lineaActual); list_Palabras.Items.Add(temp_funct); list_Palabras.Items.Add("("); string[] coor_ = temp_param.Split(','); for (int h = 0; h < coor_.Length; h++) { if (h + 1 == coor_.Length) { list_Palabras.Items.Add("" + coor_[h]); } else { list_Palabras.Items.Add("" + coor_[h]); list_Palabras.Items.Add(","); } } list_Palabras.Items.Add(")"); list_Palabras.Items.Add(";"); } else { Errores += " Funcion Invalida cerca de : \"" + temp_funct + "\" - La Funcion no se encuentra dentro de las Palabras Reservadas"; } } else { if (!temp_f) Errores += " Funcion Incorrecta cerca de : \"" + temp_funct + "\""; if (!temp_p) Errores += " Parametro Incorrecto cerca de : \"" + temp_param + "\""; } } else {

Manual Tcnico | Mtodo de Cholesky 5

Facultad de Ingeniera E.A.P Ingeniera Informtica


if (k == 0) { Errores += "Error de Sintaxis, la sintaxis correcta debe ser: tamaoMatriz(a); "; } else { if (k > 0 && k < 4) { Errores += "Error de Sintaxis, la sintaxis correcta debe ser: Fila('aX1+bX2+cX3=d'); "; } else { if (k == 4) { Errores += "Error de Sintaxis, la sintaxis correcta debe ser: terminar(); "; } } } } if (Errores.Length > 1) { list_Bugs.Items.Add("Linea : " + (k + 1) + " -> " + Errores); } }

if (list_Bugs.Items.Count < 1 && list_Comandos_Interceptar.Items.Count ==5) { lbInfo.Text = "COMPILACION EXITOSA"; RTxtConsola.ReadOnly = true; this.btncompilar.Enabled = false; BtnNuevo.Enabled = false; int contador=0; for (int s = 1; s < 4; s++) { llenarGrillas(list_LineasCapturadas.Items[s].ToString()); contador++; } this.BtnCalcular.Enabled = true; this.BtnNuevo.Enabled = true; this.RTxtConsola.ReadOnly = true; this.LLenar_Variables_X(OrdenMatriz); this.Matriz_X(); this.LLenar_Diagonal_MatrizL(OrdenMatriz); this.Mostrar_Datos(OrdenMatriz, 3); this.LLenar_MatrizU(OrdenMatriz); this.Mostrar_Datos(OrdenMatriz, 2); } else

Manual Tcnico | Mtodo de Cholesky 6

Facultad de Ingeniera E.A.P Ingeniera Informtica


{ lbInfo.Text = "PRECAUCION.. CORREGIR COMANDOS"; list_Bugs.Items.Add("Error... Debe escribirse los Comandos Correspondientes"); } }

public void llenarGrillas(string line) { string algo; int i = 0; string[] valor = new string[10]; string cad = ""; string param = ""; int aux = -1; algo = getCoor(line); for (int z = 0; z < algo.Length; z++) { cad = algo.Substring(z, 1); if (aux != -1) { if (cad == "+" || cad == "=" || cad == "-") { if (!(cad == "=" && algo.Substring(z + 1, 1) == "-")) { valor[i] = param; param = ""; i++; } } else { param = param + cad; } } else { param = param + cad; aux = 0; } if (cad == "-") { param = cad; } if (z == (algo.Length - 1)) { valor[i] = param; } } DgvA.Rows.Add(); for (int x = 0; x < 3; x++)

Manual Tcnico | Mtodo de Cholesky 7

Facultad de Ingeniera E.A.P Ingeniera Informtica


{ DgvA.Rows[contador].Cells[x].Value = valor[x]; } DgvB.Rows.Add(); DgvB.Rows[contador].Cells[0].Value = valor[3]; contador++; }

CARGAR MATRIZ A
public void Matriz_X() { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { A[i, j] = Convert.ToInt32(DgvA.Rows[i].Cells[j].Value); } } }

LLENAR VECTOR SOLUCION


public void LLenar_Vector_Solucion(int orden) { int i; for (i = 0; i < orden; i++) { this.DgvBY.Rows.Add(); this.DgvBY.Rows[i].Cells[0].Value = this.DgvB.Rows[i].Cells[0].Value; } }

VACIAR LISTA
public void vaciarlista() { list_Bugs.Items.Clear(); list_LineasCapturadas.Items.Clear(); list_Palabras.Items.Clear(); list_Comandos_Interceptar.Items.Clear(); DgvA.Rows.Clear(); DgvB.Rows.Clear(); DgvX.Rows.Clear(); DgvSolucionX.Rows.Clear(); DgvSolucionY.Rows.Clear(); lbInfo.Text = ""; this.RTxtConsola.Clear(); DgvXY.Rows.Clear(); DgvU.Rows.Clear(); DgvL.Rows.Clear(); this.RTxtConsola.Focus(); this.RTxtConsola.ReadOnly= false; this.BtnCalcular.Enabled = false;

Manual Tcnico | Mtodo de Cholesky 8

Facultad de Ingeniera E.A.P Ingeniera Informtica


}

LLENAR VARIABLES X
public void LLenar_Variables_X(int Orden) { int i; string Variable = "X"; for (i = 0; i < Orden; i++) { this.DgvX.Rows.Add(); this.DgvX.Rows[i].Cells[0].Value = Variable + i.ToString().Trim(); this.DgvXY.Rows.Add(); this.DgvXY.Rows[i].Cells[0].Value = Variable + i.ToString().Trim(); } }

LLENAR VARIABLES Y
public void LLenar_Variables_Y(int Orden) { int i; string Variable = "Y"; for (i = 0; i < Orden; i++) { this.DgvY.Rows.Add(); this.DgvY.Rows[i].Cells[0].Value = Variable + i.ToString().Trim(); } }

LLENAR MATRIZ U
public void LLenar_MatrizU(int Orden) { int i,j; for (i = 0; i < Orden; i++) { for (j = 0; j < Orden; j++) { U[j, i] = L[i, j];

} } }

LLENAR GRILLAS
public void llenarGrillas(string line) { string algo; int i = 0; string[] valor = new string[10]; string cad = ""; string param = ""; int aux = -1; algo = getCoor(line);

Manual Tcnico | Mtodo de Cholesky 9

Facultad de Ingeniera E.A.P Ingeniera Informtica


for (int z = 0; z < algo.Length; z++) { cad = algo.Substring(z, 1); if (aux != -1) { if (cad == "+" || cad == "=" || cad == "-") { if (!(cad == "=" && algo.Substring(z + 1, 1) == "-")) { valor[i] = param; param = ""; i++; } } else { param = param + cad; } } else { param = param + cad; aux = 0; } if (cad == "-") { param = cad; } if (z == (algo.Length - 1)) { valor[i] = param; } } DgvA.Rows.Add(); for (int x = 0; x < 3; x++) { DgvA.Rows[contador].Cells[x].Value = valor[x]; } DgvB.Rows.Add(); DgvB.Rows[contador].Cells[0].Value = valor[3]; contador++; }

LLENAR MATRIZ TRIANGULAR INFERIOR


public void LLenar_Matriz_Triangular_Inferior(int Orden) { int i, j, k; double Suma = 0; double cantidad = 0; for (k = 0; k < Orden; k++) { for (i = 0; i < Orden; i++)

Manual Tcnico | Mtodo de Cholesky 10

Facultad de Ingeniera E.A.P Ingeniera Informtica


{ if (k != i && k > i) { for (j = 0; j <= i - 1; j++) { Suma += L[i, j] * L[k, j]; } L[k, i] = (1 / L[i, i]) * (A[k, i] - Suma); cantidad = L[k, i]; cadena = Convert.ToString(cantidad); if (cadena == "Infinito") infinito = cadena; Suma = 0; } } } }

LLENAR MATRIZ DIAGONAL L


public void { int j, double double LLenar_Diagonal_MatrizL(int Orden) k; Suma = 0; diagonal;

for (j = 0; j < Orden; j++) { this.LLenar_Matriz_Triangular_Inferior(Orden); for (k = 0; k < j ; k++) { Suma += L[j,k] * L[j,k]; } L[j, j] =Math.Sqrt(A[j, j] - Suma); diagonal = L[j, j]; cadenadiagonal = Convert.ToString(diagonal); if (cadenadiagonal == "NeuN" || cadenadiagonal == "Infinito") { infinitodiagonal = cadenadiagonal; } Suma = 0; } }

PINTAR FUNCIONES

Manual Tcnico | Mtodo de Cholesky 11

Facultad de Ingeniera E.A.P Ingeniera Informtica

int i = 0, j = 0, k = 0; string cad; int hasta = RTxtConsola.SelectionStart; String word = ""; for (i = hasta - 1; i >= 0; i--) { k = k + 1; cad = RTxtConsola.Text.Substring(i, 1); word = cad + word; if (cad == " " || cad == "\t" || cad == "\n") { j = i; i = -1; } } cad = word.Replace("\n", ""); for (int n = 0; n < 3; n++) { if (string.Compare(funciones[n], cad.Replace("\n", "")) == 0) { this.RTxtConsola.SelectionStart = j; this.RTxtConsola.SelectionLength = k; this.RTxtConsola.SelectionFont = new Font("Courier", 14, FontStyle.Bold); this.RTxtConsola.SelectionColor = System.Drawing.Color.Red; this.RTxtConsola.SelectedText = this.RTxtConsola.SelectedText; this.RTxtConsola.SelectionColor = System.Drawing.Color.White; this.RTxtConsola.SelectionFont = new Font("Courier", 14, FontStyle.Regular); } else { this.RTxtConsola.SelectionColor = System.Drawing.Color.White; this.RTxtConsola.SelectionFont = new Font("Courier", 12, FontStyle.Regular); } } } catch (Exception asx) { } }

Manual Tcnico | Mtodo de Cholesky 12

You might also like