#include #include #include struct INPUT { int lnum; char line[99]; struct INPUT *nextPtr; }; struct TABLEENTRY { int symbol; char type; int location; }; typedef struct INPUT input; typedef input *inputPtr; typedef struct TABLEENTRY tableEntry; void insert(int, inputPtr *, char *); void tableIns(char *, char); int search(int, char *); tableEntry symbolTable[99]; int sml[99]; // array for sml int flags[99]; // flag unresolved gotos int instructionCounter = 0; // keep track of how many instructions int tableLocation = 0; // store the current place in symbolTable int variables = 99; // for variables char currentcmd[99]; // ------------------------ void main(void) { inputPtr startPtr = NULL; inputPtr currentPtr = NULL; char commandline[99]; char commandline2[99]; char variableName; int i, q; int total = 0; int n = 0; int sentinel = 1; int dog = 0; /* initialize all of flags to -1 */ for(i = 0; i<100; i++) flags[i] = -1; printf("\nSIMPLE to SML converter.\n"); while(dog != 1) { printf("? "); gets(commandline); if(strlen(commandline) == 0) dog = 1; strcpy(commandline2, commandline); insert((int)strtol(strtok(commandline, " "),NULL,10), &startPtr, commandline); total++; } /* do pass 1 */ for(i=0; i < total; i++) { /* set currentPtr to the current part of the linklist */ currentPtr->nextPtr = startPtr->nextPtr; for(n=0; n < i; n++) { currentPtr = currentPtr->nextPtr; } /* tokenize currentPtr and put into symbol table */ q=0; // keeps track of where we are in the current commandline strcpy(currentcmd, strtok(commandline, " ")); while(sentinel) { // first part is a line number if(q == 0) { // search, if not found, then install if(search((int)strtol(currentcmd, NULL, 10), NULL) == -1) tableIns(currentcmd, 'L'); } else strtok(NULL, " "); // get new one // second part is a command if(q == 1) { // rem statement if(strcmp(currentcmd, "REM") == 0) sentinel = 0; // input statement if(strcmp(currentcmd, "INPUT") == 0) { // store the following variable in the table strcpy(variableName, strtok(NULL," ")); tableIns(variableName, "V"); sml[instructionCounter] = 1000 + search(NULL, variableName); instructionCounter++; sentinel = 0; } // print / write statement if(strcmp(currentcmd, "PRINT") == 0) { // store the following variable in the table strcpy(variableName, strtok(NULL, " ")); tableIns(variableName, "V"); sml[instructionCounter] = 1100 + search(NULL, variableName); instructionCounter++; sentinel = 0; } } ++q; } } /* do pass 2 */ } /* ---------------------------- */ void insert(int lnum, inputPtr *ip, char *line) { inputPtr newPtr, previousPtr, currentPtr; newPtr = malloc(sizeof(input)); if(newPtr != NULL ) { strcpy(newPtr->line, line); newPtr->nextPtr = NULL; previousPtr = NULL; currentPtr = *ip; while(currentPtr!=NULL &&lnum > currentPtr->lnum) { previousPtr = currentPtr; currentPtr = currentPtr->nextPtr; } if(previousPtr == NULL) { newPtr->nextPtr = *ip; *ip = newPtr; } else { previousPtr->nextPtr = newPtr; newPtr->nextPtr = currentPtr; } } /* end if */ else printf("No memory available.\n"); } /* end function */ void tableIns(char *symbol, char type) { if(strcmp(type, "L") == 0) { symbolTable[tableLocation].symbol = (int)strtol(currentcmd, NULL, 10); strcpy(symbolTable[tableLocation].type, "L"); symbolTable[tableLocation].location = instructionCounter; tableLocation++; } if(strcmp(type, "V") == 0) { strcpy(symbolTable[tableLocation].symbol, symbol); strcpy(symbolTable[tableLocation].type, "V"); symbolTable[tableLocation].location = variables--; } } int search(int number, char *nonNumber) { int i; // searching for number? if(number != NULL) { for(i = 0; i < 100; i++) { if(symbolTable[i].symbol == number) return symbolTable[i].location; // found } return -1; // not found } // searching for variable or keyword if(nonNumber != NULL) { for(i = 0; i < 100; i++) { if(strcmp(symbolTable[i].symbol, nonNumber == 0)) return symbolTable[i].location; // found } return -1; // not found } }