Fallo de segmentación
Publicado por Martin (5 intervenciones) el 13/12/2007 17:50:00
//El programa lee un txt y compara si el elemento está en la tabla y si está coge su token y lo guarda en otro fichero, pero me da fallo de segmentación.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
enum {FIN=0, ID=256, NUM, FOR, IF, INCLUDE, ELSE, INT, WHILE, CHAR, DO, SWITCH, SHORT, CONTINUE, BREAK, LONG, SIZEOF, DEFAULT};
char buffer[500];
char*yytext,*p;
int yyleng;
#define MAXIMO 20
struct{
char* nombre;
int token;
} tabla[MAXIMO]= {"default", DEFAULT, "sizeof", SIZEOF, "long", LONG, "short", SHORT, "continue", CONTINUE, "break", BREAK, "long", LONG, "sizeof", SIZEOF, "switch", SWITCH, "do",DO, "char",CHAR, "for",FOR, "if",IF, "include",INCLUDE, "else",ELSE, "int",INT, "while", WHILE};
int yylex(FILE *fich, FILE *fich2);
int main() {
FILE *fich,*fich2;
char nomfich[10]="a.c"; //Archivo de donde leemos
char nomfich2[20]="tokens.tok"; //Archivo donde escribimos
int t;
fich=fopen(nomfich,"r");
fich2=fopen(nomfich2,"a+");
printf("Comienza el programa ");
while((t=yylex(fich,fich2))!=FIN)
switch(t) {
case NUM: printf("Numero %s de %d_digitos ", yytext,yyleng); break;
case ID: fputs(yytext,fich2); break;
case FOR:
case IF:
case INCLUDE:
case CHAR:
case DO:
case ELSE:
case INT:
case BREAK:
case SHORT:
case CONTINUE:
case SIZEOF:
case LONG:
case DEFAULT:
case WHILE: fputs(yytext,fich2); break;
default: printf("Es_el_caracter_%c ", t);
break;
}
return EXIT_SUCCESS;
}
int yylex(FILE *fich, FILE *fich2) {
char c;
while((c=fgetc(fich))==' ' || c==' ' || c==' ') { //Si es alguno de esos caracteres lo inserto
fputc(c,fich2);
}
if(c==EOF) return FIN; //Si llegamos al final del archivo terminamos
yytext=buffer;
p=buffer;
if(isdigit(c)) { //Compruebo si es un dÃgito
do {
*p=c;
p++;
c=fgetc(fich);
} while(isdigit(c)); //Sigo leyendo si hay más
if(c!=EOF) ungetc(c,stdin);
*p=0;
yyleng=p-yytext;
return NUM;
}
else if(isalpha(c)) { //Si es un caracter...
int i;
do {
*p=c; //Voy controlando el puntero
p++;
c=getc(fich);
}while(isalnum(c)); //Sigo leyendo si hay más caracteres
if(c!=EOF) ungetc(c,stdin);
*p=0;
yyleng=p-yytext;
for(i=0;i<MAXIMO;i++)
if(strcmp(tabla[i].nombre, yytext)==0) //Compruebo si la palabra está en la tabla
return tabla[i].token;
return ID;
}
else return c;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
enum {FIN=0, ID=256, NUM, FOR, IF, INCLUDE, ELSE, INT, WHILE, CHAR, DO, SWITCH, SHORT, CONTINUE, BREAK, LONG, SIZEOF, DEFAULT};
char buffer[500];
char*yytext,*p;
int yyleng;
#define MAXIMO 20
struct{
char* nombre;
int token;
} tabla[MAXIMO]= {"default", DEFAULT, "sizeof", SIZEOF, "long", LONG, "short", SHORT, "continue", CONTINUE, "break", BREAK, "long", LONG, "sizeof", SIZEOF, "switch", SWITCH, "do",DO, "char",CHAR, "for",FOR, "if",IF, "include",INCLUDE, "else",ELSE, "int",INT, "while", WHILE};
int yylex(FILE *fich, FILE *fich2);
int main() {
FILE *fich,*fich2;
char nomfich[10]="a.c"; //Archivo de donde leemos
char nomfich2[20]="tokens.tok"; //Archivo donde escribimos
int t;
fich=fopen(nomfich,"r");
fich2=fopen(nomfich2,"a+");
printf("Comienza el programa ");
while((t=yylex(fich,fich2))!=FIN)
switch(t) {
case NUM: printf("Numero %s de %d_digitos ", yytext,yyleng); break;
case ID: fputs(yytext,fich2); break;
case FOR:
case IF:
case INCLUDE:
case CHAR:
case DO:
case ELSE:
case INT:
case BREAK:
case SHORT:
case CONTINUE:
case SIZEOF:
case LONG:
case DEFAULT:
case WHILE: fputs(yytext,fich2); break;
default: printf("Es_el_caracter_%c ", t);
break;
}
return EXIT_SUCCESS;
}
int yylex(FILE *fich, FILE *fich2) {
char c;
while((c=fgetc(fich))==' ' || c==' ' || c==' ') { //Si es alguno de esos caracteres lo inserto
fputc(c,fich2);
}
if(c==EOF) return FIN; //Si llegamos al final del archivo terminamos
yytext=buffer;
p=buffer;
if(isdigit(c)) { //Compruebo si es un dÃgito
do {
*p=c;
p++;
c=fgetc(fich);
} while(isdigit(c)); //Sigo leyendo si hay más
if(c!=EOF) ungetc(c,stdin);
*p=0;
yyleng=p-yytext;
return NUM;
}
else if(isalpha(c)) { //Si es un caracter...
int i;
do {
*p=c; //Voy controlando el puntero
p++;
c=getc(fich);
}while(isalnum(c)); //Sigo leyendo si hay más caracteres
if(c!=EOF) ungetc(c,stdin);
*p=0;
yyleng=p-yytext;
for(i=0;i<MAXIMO;i++)
if(strcmp(tabla[i].nombre, yytext)==0) //Compruebo si la palabra está en la tabla
return tabla[i].token;
return ID;
}
else return c;
}
Valora esta pregunta


0