Skip to content

Instantly share code, notes, and snippets.

@chepecarlos
Created August 23, 2012 04:43
Show Gist options
  • Save chepecarlos/3432497 to your computer and use it in GitHub Desktop.
Save chepecarlos/3432497 to your computer and use it in GitHub Desktop.
Compiladores
#include<iostream>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string>
#define TAM_BUFFER 100
using namespace std;
class Lexico
{
char *nombreFichero;//Nombre del fichero fuente
FILE* entrada; //Fichero de entrada
int n1; //Número de línea
int traza; //Control de traza
char buffer[TAM_BUFFER]; //Buffer auxiliar de caracteres
int pBuffer; //Posición en el buffer
public:
Lexico(char *unNombreFichero, int una_traza=0);
~Lexico(void);
char siguienteToken(void);
void devuelveToken(char toke);
int lineaActual(void){return n1; };
int existeTraza(void){if(traza)return 1; else return 0;}
};
Lexico::Lexico(char *unNombreFichero, int una_traza)
{
entrada=fopen(unNombreFichero, "rt");
if((entrada==NULL))
{
cout<<"No se puede abrir el archivo"<<endl;
system("pause");
exit(-2);
}
if(una_traza) traza=1;
else traza = 0;
n1=1; //Se inicializa el contador de línea
pBuffer=0; //Se inicializa la posición del buffer
}
Lexico::~Lexico()
{
fclose(entrada);
}
char Lexico::siguienteToken(void)
{
char car;
while((car=((pBuffer>0) ? buffer[--pBuffer]:getc(entrada)))!=EOF)
{
if(car==' ') continue;
if(car=='\n'){++n1; continue;}
break;
}
if(traza) cout<<"ANALIZADOR LEXICO: Lee el token"<<car<<endl;
switch(car)
{
case'M': //palabra reservada
case'R': //palabra reservada
case'W': //palabra reservada
case'=': //asignación
case'(': //parentesis
case')':
case';': //separadores
case'}': //separadores
case'{':
case'.': //fin de programa
case'+': //operador
case'*':
case'-':
case'/':
case'%':
return(car);
}
if(islower(car))return(car); //variable
else if(isdigit(car)) return(car); //constante
else if(car == '\'') return(car);
else
{
cout<<"Error Lexico: Token Desconocido"<<endl;
system("pause");
exit(-4);
}
return(car);
}
void Lexico::devuelveToken(char token)
{
if(pBuffer>TAM_BUFFER)
{
cout<<"ERROR: Desbordamiento del buffer del analizador lexico"<<endl;
system("pause");
exit(-5);
}
else
{
buffer[pBuffer++]=token;
if(existeTraza())
cout<<"ANALIZADOR LEXICO: Recibe en buffer el token"<<token<<endl;
system("pause");
}
}
#include <cstdlib>
#include <iostream>
#include "sintacti.h"
using namespace std;
int main(int argc, char *argv[])
{
Sintactico sintactico("prueba.txt",1);
return 0;
return EXIT_SUCCESS;
}
#ifndef sintacti_H
#define sintacti_H
#include "lexico.h"
#include <stdlib.h>
#include <ctype.h>
using namespace std;
class Sintactico{
void programa (void);
void bloque (void);
void sentencia (void);
void otra_sentencia (void);
void asignacion(void);
void lectura (void);
void escritura(void);
void variable(void);
void expresion(void);
void termino(void);
void mas_terminos(void);
void factor(void);
void mas_factores(void);
void constante(void);
void errores (int codigo);
Lexico lexico; //objeto léxico miembro de la clase
public:
Sintactico(char *fuente, int traza);
~Sintactico(void);
};
Sintactico::Sintactico(char *fuente, int traza):lexico(fuente, traza)
//se inicializa el constructor de la clase léxico
{
if (lexico.existeTraza())
cout<<"INICIO DE ANALISIS SINTACTICO"<<endl;
programa();
}
/***********************************************************/
Sintactico::~Sintactico(void)
{
if (lexico.existeTraza())
{
cout<<"FIN DE ANALISIS SINTACTICO"<<endl;
cout<<"FIN DE COMPILACION"<<endl;
}
}
/***********************************************************/
void Sintactico::programa(void)
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <PROGRAMA>"<<endl;
token=lexico.siguienteToken();
if (token=='M') cout <<"M";
else errores(8);
token=lexico.siguienteToken();
if (token!='{') errores(9);
bloque();
token=lexico.siguienteToken();
if (token=='}')
{
cout<<"}";
}
else errores(2);
}
/***********************************************************/
void Sintactico::bloque(void)
{
char token=' ';
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <BLOQUE>"<<endl;
sentencia();
otra_sentencia();
}
/***********************************************************/
void Sintactico::otra_sentencia(void)
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <OTRA_SENTENCIA>"<<endl;
token=lexico.siguienteToken();
if (token==';')
{
sentencia();
otra_sentencia();
}
else lexico.devuelveToken(token); //vacio
}
/***********************************************************/
void Sintactico::sentencia(void)
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <SENTENCIA>"<<endl;
token=lexico.siguienteToken();
if ((token>='a') && (token<='z'))
{
lexico.devuelveToken(token);
asignacion();
}
else if (token=='R') lectura();
else if (token=='W') escritura();
else errores(6);
}
/***********************************************************/
void Sintactico::asignacion()
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <ASIGNACION>"<<endl;
variable();
token=lexico.siguienteToken();
if (token!='=') errores(3);
expresion();
}
/***********************************************************/
void Sintactico::variable(void)
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <VARIABLE>"<<endl;
token=lexico.siguienteToken();
if ((token>='a') && (token<='z')) cout<<token;
else errores(5);
}
/***********************************************************/
void Sintactico::expresion(void)
{
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <EXPRESION>"<<endl;
termino();
mas_terminos();
}
/***********************************************************/
void Sintactico::termino(void)
{
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <TERMINO>"<<endl;
factor();
mas_factores();
}
/***********************************************************/
void Sintactico::mas_terminos(void)
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <MAS_TERMINOS>"<<endl;
token=lexico.siguienteToken();
if (token=='+')
{
termino();
mas_terminos();
}
else if (token =='-')
{
termino();
mas_terminos();
}
else lexico.devuelveToken(token); // <vacio>
}
/***********************************************************/
void Sintactico::factor(void)
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <FACTOR>"<<endl;
token=lexico.siguienteToken();
if ((token>='0') && (token<='9'))
{
lexico.devuelveToken(token);
constante();
}
else if( token == 39){
lexico.devuelveToken(token);
constante();
}
else if (token=='(')
{
expresion();
token=lexico.siguienteToken();
if (token!=')') errores(4);
}
else
{
lexico.devuelveToken(token);
variable();
}
}
/***********************************************************/
void Sintactico::mas_factores(void)
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <MAS_FACTORES>"<<endl;
token=lexico.siguienteToken();
switch (token)
{
case '*':factor();
mas_factores();
break;
case '/':factor();
mas_factores();
break;
case '%':factor();
mas_factores();
break;
default: //<vacio>
lexico.devuelveToken(token);
}
}
/***********************************************************/
void Sintactico::lectura(void)
{
char token;
token=lexico.siguienteToken();
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <LECTURA> "<<token<<endl;
if((token<'a')||(token>'z')) errores(5);// aqui se verifica que solo lea letras minusculas
}
/***********************************************************/
void Sintactico::escritura(void)
{
char token;
token=lexico.siguienteToken();
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <ESCRITURA> "<<token<<endl;
if ((token<'a') || (token>'z')) errores(5);
}
/************************************************/
void Sintactico::constante(void)
{
char token;
if (lexico.existeTraza())
cout<<"ANALISIS SINTACTICO: <CONSTANTE> "<<endl;
token=lexico.siguienteToken();
if (((token>='0') && (token<='9')) || ((token>='a') && (token<='Z')) || (token='.')) cout<<token ;
else errores(7);
}
/***********************************************************/
void Sintactico::errores(int codigo)
{
int x;
cout<<"LINEA "<<lexico.lineaActual();
cout<<" ERROR SINTACTICO "<<codigo;
switch (codigo)
{
case 1 :cout<<" :ESPERABA UN ;"<<endl;break;
case 2 :cout<<" :ESPERABA UNA }"<<endl;break;
case 3 :cout<<" :ESPERABA UN ="<<endl;break;
case 4 :cout<<" :ESPERABA UN )"<<endl;break;
case 5 :cout<<" :ESPERABA UN IDENTIFICADOR"<<endl;break;
case 6 :cout<<" :INSTRUCCION DESCONOCIDA"<<endl;break;
case 7 :cout<<" :ESPERABA UNA "<<endl;break;
case 8 :cout<<" :ESPERABA UNA M DE MAIN"<<endl;break;
case 9 :cout<<" :ESPERABA UNA {"<<endl;break;
default:
cout<<" :NO DOCUMENTADO"<<endl;
}
cin>>x;
exit(-(codigo+100));
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment