diff --git a/src/lexer.c b/src/lexer.c index 58ad9a1..0103ab0 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -6,14 +6,18 @@ #include "tokens.h" Token *getTokens(char *input) { - Token *tokArr = malloc(sizeof(Token) * MAX_TOKENS); + Token *tokHead = NULL; //malloc(sizeof(Token)); + Token **tokPtr;// a var help to faster access + Token * tmp; + + tokPtr= &tokHead; + char *token = malloc(sizeof(char) * MAX_TOK_LEN); int i; int slen = 0; - int tokenCount = 0; + //int tokenCount = 0; int retType; - //If set, treat the input as code else it could be comment or string literal int isCode = 1; @@ -38,14 +42,19 @@ Token *getTokens(char *input) { slen = 0; retType = getType(token); + tmp = (Token *) malloc(sizeof(Token)); + tmp->next = NULL; + if (retType == -1) { - tokArr[tokenCount].type = ID; + tmp->type = ID; } else { - tokArr[tokenCount].type = retType; + tmp->type = retType; } - tokArr[tokenCount].value = malloc(sizeof(char) * (strlen(token) + 1)); - strcpy(tokArr[tokenCount].value, token); - tokenCount++; + tmp->value = malloc(sizeof(char) * (strlen(token) + 1)); + strcpy(tmp->value, token); + *tokPtr = tmp; + tokPtr = &(*tokPtr)->next; + printf(" "); strcpy(token, ""); break; @@ -65,10 +74,17 @@ Token *getTokens(char *input) { } else { token[slen] = '\0'; slen = 0; - tokArr[tokenCount].type = NUM; - tokArr[tokenCount].value = malloc(sizeof(char) * (strlen(token) + 1)); - strcpy(tokArr[tokenCount].value, token); - tokenCount++; + + tmp = (Token *) malloc(sizeof(Token)); + tmp->next = NULL; + + tmp->type = NUM; + tmp->value = malloc(sizeof(char) * (strlen(token) + 1)); + strcpy(tmp->value, token); + + *tokPtr = tmp; + tokPtr = &(*tokPtr)->next; + printf(" "); strcpy(token, ""); break; @@ -88,11 +104,17 @@ Token *getTokens(char *input) { literal[literalLen++] = input[i]; i++; } + + tmp = (Token *) malloc(sizeof(Token)); + tmp->next = NULL; + literal[literalLen] = '\0'; - tokArr[tokenCount].type = S_LITERAL; - tokArr[tokenCount].value = malloc(sizeof(char) * (strlen(literal) + 1)); - strcpy(tokArr[tokenCount].value, literal); - tokenCount++; + tmp->type = S_LITERAL; + tmp->value = malloc(sizeof(char) * (strlen(literal) + 1)); + strcpy(tmp->value, literal); + + *tokPtr = tmp; + tokPtr = &(*tokPtr)->next; } // if (!isCode) { // if (input[i] != '\\') { @@ -122,24 +144,47 @@ Token *getTokens(char *input) { } else { token[slen] = '\0'; slen = 0; - findLongestToken(token, tokArr, &tokenCount); + tokPtr = findLongestToken(token, tokPtr); break; } i++; } } } - tokArr[tokenCount].type = ENTER; - tokArr[tokenCount].value = "CR"; + + tmp = (Token *) malloc(sizeof(Token)); + tmp->next = NULL; + + tmp->type = ENTER; + tmp->value = "CR"; + + *tokPtr = tmp; + free(token); - return tokArr; + return tokHead; +} + +void freeTokens(Token *tok) +{ + Token *tmp; + + while(tok) + { + tmp = tok->next; + free(tok->value); + free(tok); + + tok = tmp; + } } /** * Method to find the longest valid token * from the given longest special characters token + * tokCur is the last Token in the list, will help us to link the new nodes **/ -void findLongestToken(char *token, Token *tokArr, int *tokenCount) { +Token **findLongestToken(char *token, Token **tokPtr) { + Token *tmp; char *temp = malloc(sizeof(char) * MAX_TOK_LEN); char *longToken = malloc(sizeof(char) * MAX_TOK_LEN); int i, m = 0; @@ -153,10 +198,16 @@ void findLongestToken(char *token, Token *tokArr, int *tokenCount) { } else { // printf("%s ", longToken); // printf("%d ", getType(longToken)); - tokArr[*tokenCount].type = getType(longToken); - tokArr[*tokenCount].value = malloc(sizeof(char) * (strlen(longToken) + 1 )); - strcpy(tokArr[*tokenCount].value, longToken); - (*tokenCount)++; + tmp = (Token *) malloc(sizeof(Token)); + + tmp->next = NULL; + tmp->type = getType(longToken); + tmp->value = malloc(sizeof(char) * (strlen(longToken) + 1 )); + strcpy(tmp->value, longToken); + + *tokPtr = tmp; + tokPtr = &(*tokPtr)->next; + m = 0; //To consider the already considered character in the invalid token i--; @@ -166,13 +217,19 @@ void findLongestToken(char *token, Token *tokArr, int *tokenCount) { // printf("%d ", getType(longToken)); if( m!=0 ) { - tokArr[*tokenCount].type = getType(longToken); - tokArr[*tokenCount].value = malloc(sizeof(char) * (strlen(longToken) + 1 )); - strcpy(tokArr[*tokenCount].value, longToken); - (*tokenCount)++; + tmp = (Token *) malloc(sizeof(Token)); + + tmp->next = NULL; + tmp->type = getType(longToken); + tmp->value = malloc(sizeof(char) * (strlen(longToken) + 1 )); + strcpy(tmp->value, longToken); + *tokPtr = tmp; + tokPtr = &(*tokPtr)->next; } free(longToken); free(temp); + + return tokPtr; } int isValidToken(char *token) { diff --git a/src/lexer.h b/src/lexer.h index b09a253..806a8c6 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -16,6 +16,7 @@ typedef struct Tokens{ int type; char *value; + struct Tokens *next; } Token; typedef struct Directives{ @@ -33,8 +34,9 @@ typedef struct Directives{ Directive *directives(char *); int getType(char *); Token *getTokens(char *); +void freeTokens(Token *); int isunderscore(char); int isValidToken(char *); -void findLongestToken ( char *token, Token* tokArr, int* tokenCount ); +Token **findLongestToken(char *, Token **); #endif //_LEXER_H_ diff --git a/src/parser.c b/src/parser.c index 0acd9cb..c635862 100644 --- a/src/parser.c +++ b/src/parser.c @@ -15,20 +15,21 @@ void advance(); -Token eat(int); +Token* eat(int type); char *parseExprList(); int getRelational(int); -int parseLine(); +void parseLine(); int parseStatement(); int parseExpression(); int parseTerm(); int parseFactor(); // Global Variables -int tokenIndex = 0; -Token *tokArr; +//int tokenIndex = 0; +Token *tokHead; +Token *tokCur;//current index of the Token char *mappings[LINES]; char *program[LINES]; Stack *stack=NULL; @@ -67,14 +68,13 @@ int main(int argc, char *argv[]) { } while (pc < i) { - tokenIndex = 0; - tokArr = getTokens(program[pc]); - + tokHead = getTokens(program[pc]); + tokCur = tokHead; pc++; parseLine(); - free(tokArr); + freeTokens(tokHead); } for (int j = 0; j < i; j++) { free(program[i]); @@ -126,16 +126,21 @@ int getLabelPos(char *label) { } // advance the tokencount -void advance() { tokenIndex++; } +void advance() { + //tokenIndex++; + tokCur = tokCur->next; +} -Token eat(int type) { +Token *eat(int type) { // Returns the type if correct - if (tokArr[tokenIndex].type == type) { + Token *ret = tokCur; + if (tokCur->type == type) { advance(); - return tokArr[tokenIndex - 1]; + return ret; } else { printf("Error: Could not parse\n"); // exit(1); + return NULL; } } @@ -148,22 +153,22 @@ int getRelational(int token) { return -1; } -int parseLine() { - if (tokArr[tokenIndex].type == NUM) { +void parseLine() { + if (tokCur->type == NUM) { eat(NUM); } parseStatement(); } int parseStatement() { - switch (tokArr[tokenIndex].type) { + switch (tokCur->type) { case LET: { - Token var; + Token *var; eat(LET); var = eat(ID); eat(ASSIGN); int val = parseExpression(); - insertVariable(var.value, val, NUM); + insertVariable(var->value, val, NUM); } eat(ENTER); break; @@ -180,7 +185,7 @@ int parseStatement() { int cond = 0; eat(IF); int op1 = parseExpression(); - int operator= eat(getRelational(tokArr[tokenIndex].type)).type; + int operator= eat(getRelational(tokCur->type))->type; int op2 = parseExpression(); switch (operator) { case EQUALTO: @@ -224,27 +229,27 @@ int parseStatement() { } case INPUT: { - Token var; + Token *var; eat(INPUT); // var-list parser - var = tokArr[tokenIndex]; - tokenIndex++; - while (var.type != ENTER) { + var = tokCur; + tokCur = tokCur->next; + while (var->type != ENTER) { int t; - Symbol *sVar = getSymbol(var.value); + Symbol *sVar = getSymbol(var->value); if (sVar != NULL) { scanf("%d", &t); sVar->value = t; } else { printf("Variable Not Found!"); } - if (tokArr[tokenIndex].type != COMMA) { + if (tokCur->type != COMMA) { break; } eat(COMMA); - var = tokArr[tokenIndex]; - tokenIndex++; + var = tokCur; + tokCur = tokCur->next; } } eat(ENTER); @@ -299,9 +304,9 @@ int parseStatement() { char *parseExprList() { char *expr = (char *)malloc(sizeof(char) * MAX_INP); strcpy(expr, ""); - if (tokArr[tokenIndex].type == S_LITERAL) { + if (tokCur->type == S_LITERAL) { // printf("Sl\n"); - strcat(expr, tokArr[tokenIndex].value); + strcat(expr, tokCur->value); eat(S_LITERAL); } else { // printf("Exprs\n"); @@ -311,7 +316,7 @@ char *parseExprList() { strcat(expr, value); } - if (tokArr[tokenIndex].type == COMMA) { + if (tokCur->type == COMMA) { eat(COMMA); strcat(expr, parseExprList()); } @@ -321,7 +326,7 @@ char *parseExprList() { int parseExpression() { int op = parseTerm(); - switch (tokArr[tokenIndex].type) { + switch (tokCur->type) { case PLUS: eat(PLUS); op = op + parseTerm(); @@ -337,7 +342,7 @@ int parseExpression() { int parseTerm() { int op = parseFactor(); - switch (tokArr[tokenIndex].type) { + switch (tokCur->type) { case MUL: eat(MUL); op = op * parseFactor(); @@ -353,22 +358,22 @@ int parseTerm() { } int parseFactor() { - switch (tokArr[tokenIndex].type) { + switch (tokCur->type) { case ID: { // printf("ID\n"); - Token var; + Token *var; var = eat(ID); - return getSymbol(var.value)->value; + return getSymbol(var->value)->value; } break; case NUM: { - Token var; + Token *var; var = eat(NUM); // printf("FACTOR: %d\n", atoi(var.value)); - return atoi(var.value); + return atoi(var->value); } // default: error(); } - if (tokArr[tokenIndex].type == LEFTPAR) { + if (tokCur->type == LEFTPAR) { int op = parseExpression(); eat(RIGHTPAR); return op; diff --git a/src/symboltable.c b/src/symboltable.c index c5e70f5..d295ff5 100644 --- a/src/symboltable.c +++ b/src/symboltable.c @@ -7,48 +7,57 @@ #include "lexer.h" //local declarations -int getSymbolPos(char *var); +//int getSymbolPos(char *var); // SymbolTable Symbol *st = NULL; -int stSize = 0; void insertVariable(char *var, int value, int type) { - if (st == NULL) { - st = (Symbol *)malloc(sizeof(Symbol) * ST_SIZE); + Symbol *tmp = getSymbol(var); + if(tmp != NULL) + { + tmp->value = value; + return; } - Symbol s; - s.variable = (char *)malloc(sizeof(char) * (strlen(var) + 1)); + + Symbol *s = (Symbol *)malloc(sizeof(Symbol)); + + s->variable = (char *)malloc(sizeof(char) * (strlen(var) + 1)); // s.value = (char *)malloc(sizeof(char) * MAX_TOK_LEN); - strcpy(s.variable, var); - s.value = value; - s.type = type; + strcpy(s->variable, var); + s->value = value; + s->type = type; + s->next = NULL; - int pos = getSymbolPos(var); - if(pos != -1) { - free(st[pos].variable); - st[pos] = s; - return; + if(st == NULL) + { + st = s; + } + else + { + tmp = st; + while(tmp->next) + { + tmp = tmp->next; + } + tmp->next = s; } - st[stSize] = s; - // printf("%d - %s\n", stSize, st[stSize].variable); - stSize++; } Symbol *getSymbol(char *var) { - if (st == NULL) { - return NULL; - } - for (int i = 0; i < stSize; i++) { + Symbol *tmp = st; + + while (tmp) { // printf("%d - %s\n", i, st[i].variable); - if (*(st[i].variable) == *var) { - return &st[i]; + if (*tmp->variable == *var) { + return tmp; } + tmp = tmp->next; } return NULL; } - +/* int getSymbolPos(char *var) { if (st == NULL) { return -1; @@ -60,8 +69,17 @@ int getSymbolPos(char *var) { } } return -1; -} +}*/ void freeTable() { - free(st); + //free(st); + Symbol *tmp; + + while(st) + { + tmp = st->next; + free(st); + st = tmp; + } + st = NULL; } diff --git a/src/symboltable.h b/src/symboltable.h index e9905e1..0462c90 100644 --- a/src/symboltable.h +++ b/src/symboltable.h @@ -5,6 +5,7 @@ typedef struct Symbols{ char *variable; int type; int value; + struct Symbols * next; } Symbol; Symbol *getSymbol(char *);