Skip to content

Instantly share code, notes, and snippets.

@chepecarlos
Created June 27, 2012 08:02
Show Gist options
  • Save chepecarlos/3002325 to your computer and use it in GitHub Desktop.
Save chepecarlos/3002325 to your computer and use it in GitHub Desktop.
Programa para una tarea de compiladores
#include<iostream>
#include<stack>
using namespace std;
stack <char> pila;
int mover(int estado , char simbolo)
{
if( estado == 1 && simbolo == '(' )
{
pila.push('X');
return 1;
}
if( estado == 1 && simbolo == 'a' )
return 2;
if( estado == 2 && simbolo == ')' )
{
pila.pop();
return 2;
}
if( estado == 2 && simbolo == '+' )
return 1;
if( estado == 2 && simbolo == '-' )
return 1;
return 0;
}
int scanner(char palabra[10] )
{
int i = 0;
int estado = 1; //estado inicial
while(palabra[i])
{
estado = mover(estado,palabra[i]);
i++;
}
if ( estado == 2 && pila.empty()) //estado final
return 1;
return 0;
}
int main()
{
system("color f0");
char palabra[10];
cout<<"\n\n\t\tIngrese Cadena : ";
gets(palabra);
if( scanner(palabra))
cout<<"\n\n\t\t\t Corecto !!! ";
else
cout<<" \n\n\t\t\t Error !!!!";
cout<<endl<<endl;
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment