I have written a scanner program for oberon language compiler but its incomplete,in the botton i need to write certain code to make this program work properly,so can anyone help me to make this program work properly.
FILE *input;
int row, col;
token t;
char buff[bufflen];
int tabwidth;
int KWidx[] = { 29, 0, 6, 32, 2, 0, 9, 0, 25, 0,
4, 0, 21, 12, 0, 0, 0, 0, 0, 0,
19, 0, 7, 8, 14, 13, 28, 0, 0, 0,
15, 31, 11, 17, 0, 16, 30, 18, 1, 0,
10, 3, 0, 5, 22, 26, 23, 24, 0, 0,
27, 20, 0
};
char *KWstr[] = { "", "ARRAY", "BEGIN", "BOOLEAN", "BY",
"CHAR", "CONST", "DIV", "DO", "ELSE",
"ELSIF", "END", "EXIT", "FALSE", "FOR",
"IF", "INTEGER", "LOOP", "MOD", "MODULE",
"OF", "OR", "PROCEDURE", "REAL", "REPEAT",
"RETURN", "STRING", "THEN", "TO", "TRUE",
"UNTIL", "VAR", "WHILE"
};
tokenType KWtok[] = { tokIDENT, kwARRAY, kwBEGIN,
kwBOOLEAN, kwBY, kwCHAR, kwCONST,
kwDIV, kwDO, kwELSE, kwELSIF, kwEND, kwEXIT, kwFALSE,
kwFOR, kwIF, kwINTEGER, kwLOOP, kwMOD,
kwMODULE, kwOF, kwOR, kwPROCEDURE, kwREAL,
kwREPEAT, kwRETURN, kwSTRING, kwTHEN, kwTO,
kwTRUE, kwUNTIL, kwVAR, kwWHILE
};
int KWhash(char *s) {
return (strlen(s) + 83*(s[0]+s[1]) + 139*s[strlen(s)-1]) % 53;
}
tokenType KWsearch(char *s) {
int h = KWhash(s);
message(debug, "hash: keyword hash value = %i", h);
message(debug, "hash: keyword index value = %i", KWidx[h]);
message(debug, "hash: matching to keyword \"%s\"", KWstr[KWidx[h]]);
if (strcmp(KWstr[KWidx[h]], s) == 0)
return KWtok[KWidx[h]];
else
return KWtok[0];
}
void strnapp(char *s, char c, int n) {
int len = strlen(s);
if (len < n-1) {
s[len] = c;
s[len+1] = '\0';
}
}
int peekCh() {
int c;
if (feof(input))
c = -1;
else {
c = getc(input);
ungetc(c, input);
}
return c;
}
int nextCh() {
int c;
if (feof(input))
c = -1;
else {
c = getc(input);
strnapp(buff, (char)c, bufflen);
if (c == '\r' || c == '\n') { // Mac or Unix EOL
if (c == '\r' && peekCh() == '\n') // DOS style EOL
c = getc(input);
++row;
col = 0;
} else if (c == '\t')
col = col/tabwidth * tabwidth + tabwidth; // calculate the tab column
else
++col;
}
return c;
}
void scannerInit(FILE *fin) {
input = fin;
row = 1; col = 0; // index of current character
buff[0] = '\0';
tabwidth = DEFtabwidth;
message(debug, "scanner: initialized");
}
int setTabWidth(int t) {
return tabwidth = t<1?DEFtabwidth:t;
}
int getTabWidth() {
return tabwidth;
}
token scannerRead() {
int c; int count;
c = nextCh();
/* PLACE YOUR CODE HERE */
message(debug, "scanner: found %s", tokenString(t));
return t;
}