Mostrando entradas con la etiqueta Analizador. Mostrar todas las entradas
Mostrando entradas con la etiqueta Analizador. Mostrar todas las entradas

Analizador lexico

Este programa es un analizador léxico que te sube las palabras analizadas de un archivo a memoria dinámica utilizando árboles
los archivos tendrán el código fuente.

Ejemplo:

inicio
escribir(Daniel)
fin

Este programa esta hecho con pase de parámetros


#include"stdio.h"  /*Las palabras que puedes utilizar en este progra ma son las iguientes*/
#include"conio.h"  /*inicio, escribe, lee, si, fin, mientras*/
#include"alloc.h"
                           /*inicio es la cabesa del programa*/

#define FALSO 0    /*fin son los pies delprograma*/
#define CIERTO 1   /*escribe,lee,si,mientras son isntrucciones dadas por*/
                           /*la gramatica ya antes establecida*/
char palabra[30],palabr[30];

struct programa{struct inicio *inicio1; struct sentencias *sen1; struct fin *fin1;};
struct inicio{struct programa *anterior1;};
struct sentencias{struct sentencias *siguiente1; struct instrucciones *ins_sig; struct sentencias *anterior2; struct programa *raiz;};
struct instrucciones{struct escribe *esc; struct lee *lee; struct mientras *min; struct si *si; struct sentencias *sent;};
struct escribe{struct instrucciones *anterior3;};
struct lee{struct instrucciones *anterior4;};
struct mientras{struct instrucciones *anterior5;};
struct si{struct instrucciones *anterior6;};
struct fin{struct programa *anterior7;};

struct programa *nodo_raiz=NULL;
struct inicio *i=NULL;
struct sentencias *sent=NULL;
struct instrucciones *inst=NULL;
struct escribe *escb=NULL;
struct lee *le=NULL;
struct mientras *mi=NULL;
struct si *s=NULL;
struct fin *f=NULL;

int ins_sen(char palabra[50])
            {if((strcmp(palabra,"escribe"))==0) return 1;
             else if((strcmp(palabra,"lee"))==0) return 2;
             else if((strcmp(palabra,"mientras"))==0) return 3;
             else if((strcmp(palabra,"si"))==0) return 4;
             else if((strcmp(palabra,"inicio"))==0) return 5;
             else if((strcmp(palabra,"fin"))==0) return 6;
            }
void mensage(){printf("NO HAY MEMORIA DISPONIBLE"); getch(); exit(1);};
void ins_lee()
            {if((le=malloc(sizeof(le)))==NULL){mensage();}
             le->anterior4=inst;
             inst->lee=le;}
void ins_mientras()
            {if((mi=malloc(sizeof(mi)))==NULL){mensage();}
             mi->anterior5=inst;
             inst->min=mi;
             }
void ins_escribe()
            { if((escb=malloc(sizeof(escb)))==NULL){mensage();}
              escb->anterior3=inst;
              inst->esc=escb;}
void ins_si()
            { struct instrucciones *tem=inst;
              if((inst=malloc(sizeof(inst)))==NULL){mensage();}
              inst->esc=NULL; inst->lee=NULL;
              inst->min=NULL; inst->si=NULL;
              inst->sent=NULL;
              if(!tem)
                        { inst->esc=NULL;
                          inst->min=NULL;
                          inst->lee=NULL;
                          inst->si=NULL;
                          inst->sent=sent;}
             else
                 { inst->esc=NULL;
                   inst->min=NULL;
                   inst->lee=NULL;
                   inst->si=NULL;
                   inst->sent=tem; }
            }
void ins_sentencias()
            { struct sentencias *tmp=sent ;
              if((sent=malloc(sizeof(sent)))==NULL){mensage();}
              if(!tmp){sent->siguiente1=NULL;
                           sent->ins_sig=NULL;
                   sent->anterior2=NULL;
                           nodo_raiz->sen1=sent;
                           sent->raiz=nodo_raiz;
                           }
             else
                 {
                   sent->ins_sig=NULL;
                   sent->anterior2=tmp;
                   sent->siguiente1=NULL;
                   sent->raiz=NULL;
                 }
            }
void ins_fin()
            { if((f=malloc(sizeof(f)))==NULL){mensage();}
              f->anterior7=nodo_raiz; nodo_raiz->fin1=f; }
void ins_inicio()
            { if((i=malloc(sizeof(i)))==NULL){mensage();}
              nodo_raiz->inicio1=i; i->anterior1=nodo_raiz; }
void ins_programa()
            { if((nodo_raiz=malloc(sizeof(nodo_raiz)))==NULL){mensage();}
              nodo_raiz->inicio1=NULL; nodo_raiz->sen1=NULL; nodo_raiz->fin1=NULL;
            }
void memoria(int que)
            { if(que==1){ins_escribe();}
              else if(que==2){ins_lee();}
              else if(que==3){ins_mientras();}
              else if(que==4){ins_si();}
              else if(que==5){ins_inicio();}
              else if(que==6){ins_fin();}
            }

void inicialisa(void) { int x; for(x=0;x<=29;x++){ palabra[x]='