UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA
Docente: Carlos Orlando Claros Vsquez [email protected] Pg. 1/6
LABORATORIO N 03
OBJETIVO:
1. Construir un compilador de una pasada de expresiones infija.
2. Analizar las construcciones de arboles. Paso N 01: Disear el siguiente formulario, tal como se muestra a continuacin
Paso N 02: Modificar las propiedades del objeto agregado, tal como se indica
a continuacin
Objeto Propiedad Valor Form1 Text Compilador de una pasada Facultad de Ingeniera Label1 Text Ingrese la expresin en INFIJA : Label2 Text La expresin en POSTFIJA es : Label3 Text Resultado es : TextBox1 Name TxtPreg TextBox2 Name
ReadOny TxtInter True
TextBox3 Name ReadOnly
TxtRespuesta True
GroupBox1 Text Detalle DataGridView1 Name
allowUserToAddRows DataGridView1 True
DataGridView2 Name allowUserToAddRows
DataGridView2 True
Botn de Comando 1 Name Text
BtnNuevo &Nuevo
Botn de Comando 1 Name Text
BtnCalcular &Calcular
Botn de Comando 1 Name Text
BtnSalir &Salir
UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA
Docente: Carlos Orlando Claros Vsquez [email protected] Pg. 2/6
Paso N 03: Agregar el siguiente cdigo:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace Compilador_de_una_pasada { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public int pila_top;
string [] pila_items = new string [100];
public Boolean PilaVacia() { if (pila_top == -1) return true; else return false; }
public string QuitarElePila() { if (PilaVacia()) { MessageBox.Show("Pila Vacia (UnderFlow)...!", "Atencin", MessageBoxButtons.OK, MessageBoxIcon.Information); return ""; } return pila_items[pila_top--]; }
public void AgregarElePila(string x) { if (pila_top == 100) MessageBox.Show("Pila llena (OverFlow)...!", "Atencin", MessageBoxButtons.OK, MessageBoxIcon.Information); else { pila_top++; pila_items[pila_top] = x; } }
UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA
Docente: Carlos Orlando Claros Vsquez [email protected] Pg. 3/6
public void IniciarPila() { pila_top = -1; }
public string pila_topPila() { if (PilaVacia()) { MessageBox.Show("Pila Vacia (UnderFlow)...!", "Atencin", MessageBoxButtons.OK, MessageBoxIcon.Information); return ""; } else { return pila_items[pila_top]; } }
public Boolean Pred(string op1, string op2) { if (op1 == "(") return false; if (op2 == "(" && op1 != ")")return false; if (op2 == ")" && op1 != "(")return true; if (op2 == ")" && op1 == "(") return false; switch (op2) { case "+": if (op2 != "+" && op2 != "-") return false; else return true;
case "-":
if (op2 != "+" && op2 != "-") return false; else return true;
case "*" : if (op2 != "^") return false; else return true;
case "/": if(op2!="^") return false; else return true;
case "^": if (op2 == "^")
UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA
Docente: Carlos Orlando Claros Vsquez [email protected] Pg. 4/6
return false; else return true; } return false; }
public string postfija(string Prefija) { int i=0,Indice1=0,Indice2=0; string simbolo, perdido, conca="", posfijo=""; this.IniciarPila(); while (i < Prefija.Trim().Length) { simbolo = Prefija.Substring(i, 1); i++; if (!es_operador(simbolo)) { posfijo += simbolo; this.dataGridView2.Rows.Add();
this.dataGridView2.Rows[Indice2].Cells[0].Value=simbolo; Indice2++; } else { while (!this.PilaVacia() && this.Pred(pila_topPila(), simbolo)) posfijo += QuitarElePila(); if (string.Compare(simbolo, ")") != 0) { this.AgregarElePila(simbolo); this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[Indice1].Cells[0].Value = simbolo; Indice1++; } else perdido = this.QuitarElePila(); } } while (!PilaVacia()) posfijo += this.QuitarElePila(); return posfijo; }
public Boolean es_operador(string c) { string s; int i=0; s = "+-*^/()"; while (i
UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA
Docente: Carlos Orlando Claros Vsquez [email protected] Pg. 5/6
public float eval(string posfijo) { int i = 0; string digito; float op1, op2; double y; this.IniciarPila(); while (i < posfijo.Trim().Length) { digito = posfijo.Substring(i, 1); i++; if (!es_operador(digito)) this.AgregarElePila (digito); else {
op2=float.Parse (this.QuitarElePila ()); op1=float.Parse( this.QuitarElePila()); y=this.valor(op1,op2,digito); this.AgregarElePila (Convert.ToString (y)); } } return float.Parse (this.QuitarElePila()); }
public double valor(float op1, float op2, string c) { double y=0.00; switch(c){ case "+":y=(op1+op2);break; case "-":y=(op1-op2);break; case "*":y=(op1*op2);break; case "/":y=(op1/op2);break; case "^":y=Math.Pow(op1,op2);break; default:break; } return y; }
private void BtnCalcular_Click_1(object sender, EventArgs e) { string str1; if (this.TxtPreg.Text.Trim() != " ") { str1 = this.postfija(this.TxtPreg.Text.Trim()); this.TxtInter.Text = str1.Trim(); this.TxtRespuesta.Text =
Convert.ToString(this.eval(str1)); } this.BtnCalcular.Enabled = false; this.BtnNuevo.Focus();
}
private void BtnNuevo_Click(object sender, EventArgs e) { this.TxtInter.Text = string.Empty; this.TxtPreg.Text = string.Empty;
UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA
Docente: Carlos Orlando Claros Vsquez [email protected] Pg. 6/6
this.TxtRespuesta.Text = string.Empty; this.TxtPreg.Focus(); }
private void BtnSalir_Click(object sender, EventArgs e) { if (MessageBox.Show("Desea salir?",
"Facutad de Ingeniera", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
this.Close(); }
private void TxtPreg_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { this.BtnCalcular.Enabled = true; this.BtnCalcular.Focus(); } } } } Paso N 04: Generando salida:
TAREA:
Modificar el programa, para que permita realizar operaciones bsicas con numeros enteros . por
ejemplo: 10+25.