Hw 2: Dr. Euripides Montagne

Words: 1026
Pages: 5

/* Noel Gayle COP 3402 HW 2: Lexical Analyzer Professor: Dr. Euripides Montagne
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_IDENT_LENGTH 11
#define MAX_NUM_LENGTH 5

typedef int bool;
#define TRUE 1
#define FALSE 0

typedef enum { nulsym = 1, identsym, numbersym, plussym, minussym, multsym, slashsym, oddsym, eqsym, neqsym, lessym, leqsym, gtrsym, geqsym, lparentsym, rparentsym, commasym, semicolonsym, periodsym, becomessym, beginsym, endsym, ifsym, thensym, whilesym, dosym, callsym, constsym, varsym, procsym, writesym, readsym , elsesym } token_type;

char * symbols[] = {"", "nulsym", "identsym", "numbersym", "plussym", "minussym", "multsym", "slashsym", "oddsym", "eqsym", "neqsym", "lessym",
…show more content…
"leqsym", "grtsym", "geqsym", "lparentsym", "rparentsym", "commasym", "semicolonsym", "periodsym", "becomessym", "beginsym", "endsym", "ifsym", "thensym", "whilesym", "dosym", "callsym", "constsym", "varsym", "procsym", "outsym", "insym", "elsesym"};

char* reservedWords [] ={"begin", "end", "if", "then", "while", "do", "call", "const", "var", "proc", "out", "in", "else"}; int numReserve = sizeof(reservedWords) / sizeof(char *);

char * specialSymbols[] = {"plus", "minus", "times", "slash", "rparetn", "lparent", "rbracket", "lbracket", "equal", "nequal", "comman","period", "lss", "grt", "leq", "geq", "semicolon", "colon"}; int numSpecials = sizeof(specialSymbols)/ sizeof(char *);

typedef struct{ int type; char name[MAX_IDENT_LENGTH+1]; int value; bool hasValue;
}input;

char currChar; char line[500]; char prevChar; //keeps track of prevChar character for identifying purposes int charCount; int lineCount; int c; //info pointer bool invalid = FALSE; fpos_t currPos;

input
…show more content…
fclose(ofp2); fclose(ofp3);

return 0;
}

// Prints the Lexeme List void printList(){ int i; for(i = 0; i < c-1; i++){ if(info[i].hasValue == TRUE) fprintf(ofp3, "%d %d ", info[i].type, info[i].value); else if(info[i].type == identsym) fprintf(ofp3, "%d %s ", info[i].type, info[i].name); else fprintf(ofp3, "%d ", info[i].type); }
}

// Prints the Lexeme Table void printTable(){ int i; fprintf(ofp2, " lexeme token type\n");

for( i = 0; i < c-1; i++){ if(info[i].hasValue == TRUE) fprintf(ofp2,"%9d %8d\n", info[i].value, info[i].type); else fprintf(ofp2,"%9s %8d\n", info[i].name, info[i].type); }

}

// Moves to next character void nextChar(){

if(line[charCount] == '\0'){ int i = 0, j = 0; char c; char newLine[sizeof(line)]; char* fetchLine = fgets(newLine, sizeof(line), ifp); lineCount++; charCount = 0;

while(c = newLine[i++]){ if(isprint(c)) line[j++] = c; else if( c == '\t'){ line[j++] = ' '; while (j % 8 != 1) line[j++] = ' '; } } line[j++] = '\n'; line[j] = '\0'; } currChar = line[charCount++];
}

//Read through white space void noWhite(){ while(currChar <= ' '){ nextChar(); }