/* Name: scanner.c Author: Sardelli Giovanni Francesco Matricola:439990 Date: 12/01/04 18.21 Description: analizzatore lessicale di un frammento di assembler */ # include # include # include # include # define MAXSIZE 31 /*lunghezza massima identificatore*/ # define MAXSIZEFILE 12 /******************************************************************************* *********************Dichiarazione dei prototipi di funzione******************** *******************************************************************************/ void openFile(FILE *, char [MAXSIZEFILE]); void presentazione(void); void fine (void); /**************Dichiarazione dei tipi di dato definiti dall'utente*************/ typedef union{ char stringa[80]; int intValue; char name[MAXSIZE+1]; }tokenType; typedef struct{ int col; int riga; int cat; tokenType tipo; } miotoken; enum states{ ASSIOMA_S, NUM_S, ID_S, STR_S, STR_END_S, COMM_S, COMM_END_S, FINE_S, PT_INTERR_S, AT_S, UNDERLINE_S, TONDA1_S, TONDA2_S, QUADRA1_S, QUADRA2_S, ANG1_1, ANG2_S, PT_VIRG_S, VIRG_S, PT_S, APICE_S, DP_APICE_S, E_CMR_S, DOLLARO_S, PIU_S, MENO_S, PER_S, DIVISO_S, ADC, ADD, AND, ASSUME, CALL, CMP, DATE, DB, DUP, DW, DEC, DIV, END, ENDM, ENDP, ENDS, FAR, IDIV, IMUL, INC, INT, JMP, LABEL, LEA, LOOP, MACRO, MOV, MUL, NOT, OR, PARA, POP, PROC, PUSH, REP, RET, SEGMENT, STACK, SUB, XCHG, XOR, }; char *categoria[]={"ASSIOMA_S", " NUM_S", "ID_S", "STR_S", "STR_END_S", "COMM_S", "COMM_END_S", "FINE_S", "PT_INTERR_S", "AT_S", "UNDERLINE_S", "TONDA1_S", "TONDA2_S", "QUADRA1_S", "QUADRA2_S", "ANG1_1", "ANG2_S", "PT_VIRG_S", "VIRG_S", "PT_S", "APICE_S", "DP_APICE_S", "E_CMR_S", "DOLLARO_S", "PIU_S", "MENO_S", "PER_S", "DIVISO_S", "ADC", "ADD", "AND", "ASSUME", "CALL", "CMP", "DATE", "DB", "DUP", "DW", "DEC", "DIV", "END", "ENDM", "ENDP", "ENDS", "FAR", "IDIV", "IMUL", "INC", "INT", "JMP", "LABEL", "LEA", "LOOP", "MACRO", "MOV", "MUL", "NOT", "OR", "PARA", "POP", "PROC", "PUSH", " REP", "RET", "SEGMENT", "STACK", "SUB", "XCHG", "XOR"}; char *key[]={"ADC", "ADD", "AND", "ASSUME", "CALL", "CMP", "DATE", "DB", "DUP", "DW", "DEC", "DIV", "END", "ENDM", "ENDP", "ENDS", "FAR", "IDIV", "IMUL", "INC", "INT", "JMP", "LABEL", "LEA", "LOOP", "MACRO", "MOV", "MUL", "NOT", "OR", "PARA", "POP", "PROC", "PUSH", "REP", "RET", "SEGMENT", "STACK", "SUB", "XCHG", "XOR" }; /******************************************************************************* **********************************MAIN PROGRAM********************************** *******************************************************************************/ int main(int argc, char *argv[]){ /***********************Dichiarazioni variabili del main***********************/ FILE *src; char sorgente[MAXSIZEFILE]; char car; enum states state; miotoken token; presentazione(); /******************************************************************************* Controlla che il file da analizzare esista e lo apre in lettura. Restituisce un messaggio di errore in caso contrario. *******************************************************************************/ if (argc > 1) { if ((src = fopen(argv[1], "r+")) == NULL){ printf("\n\nFile non trovato...!:)\n"); openFile(src,sorgente); } } else openFile(src,sorgente); /******************Inizio scansione del file da analizzare...******************/ printf("\n\nInizio analisi del file sorgente in corso...\n\n"); state = ASSIOMA_S; token.col = 0; printf("%d", state); car = fgetc(src); token.col++; while (car!=EOF){ while (isspace(car)){ car = fgetc(src); token.col++; } switch (state){ case ASSIOMA_S :{ switch (car){ case ';': { printf("\nInizio commento\n"); state = COMM_S; printf("%c", car); car = fgetc(src); while((car!=EOF)&&(car!='\n')){ printf("%c", car); car = fgetc(src); } state = COMM_END_S; car = fgetc(src); printf("\nFine commento\n"); break; } case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case '?': case '@': case '_': { int j; state = ID_S; for (j=0; j