You are on page 1of 6

UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA

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
Form1
Label1
Label2
Label3
TextBox1
TextBox2
TextBox3
GroupBox1
DataGridView1
DataGridView2
Botn de Comando 1
Botn de Comando 1
Botn de Comando 1

Propiedad
Text
Text
Text
Text
Name
Name
ReadOny
Name
ReadOnly
Text
Name
allowUserToAddRows
Name
allowUserToAddRows
Name
Text
Name
Text
Name
Text

Valor
Compilador de una pasada Facultad de Ingeniera
Ingrese la expresin en INFIJA :
La expresin en POSTFIJA es :
Resultado es :
TxtPreg
TxtInter
True
TxtRespuesta
True
Detalle
DataGridView1
True
DataGridView2
True
BtnNuevo
&Nuevo
BtnCalcular
&Calcular
BtnSalir
&Salir

Docente: Carlos Orlando Claros Vsquez cclarosvasquez@Hotmal.com

Pg. 1/6

UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA

Paso N 03: Agregar el siguiente cdigo:


using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
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;
}
}

Docente: Carlos Orlando Claros Vsquez cclarosvasquez@Hotmal.com

Pg. 2/6

UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA

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 == "^")

Docente: Carlos Orlando Claros Vsquez cclarosvasquez@Hotmal.com

Pg. 3/6

UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA

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 <= s.Length-1)
{
if(c==s.Substring (i,1))
return true;
i++;
}
return false;
}

Docente: Carlos Orlando Claros Vsquez cclarosvasquez@Hotmal.com

Pg. 4/6

UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA

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;

Docente: Carlos Orlando Claros Vsquez cclarosvasquez@Hotmal.com

Pg. 5/6

UNSACA TEORIA DE LENGUAJES DE COMPILADORES VISUAL C# - FACULTAD DE INGENIERIA

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.

Docente: Carlos Orlando Claros Vsquez cclarosvasquez@Hotmal.com

Pg. 6/6

You might also like