Lab 3 and 4
Lab 3 and 4
David Jijo
C-52
220905478
Lexical Analyzer:
#token.h
#ifndef TOKEN_H
#define TOKEN_H
#include "utils.h"
extern int TOKEN_TABLE_SIZE;
Token* createToken(const char* name, int index, int pos, TokTypes type) {
Token* t = (Token*)malloc(sizeof(Token));
strcpy(t->token_name, name);
t->token_name[sizeof(t->token_name) - 1] = '\0';
t->index = index;
t->pos = pos;
t->type = type;
return t;
}
return m[t->type];
}
int left = 0;
int right = 0;
int i = 0;
char buff[TOKEN_TABLE_SIZE];
Token* t;
if ( isInteger(buff) ){
allTokens[i] = createToken(buff, i, pos, NUMBER);
}else if ( isKeyword(buff) ){
allTokens[i] = createToken(buff, i, pos, KEYWORD);
}else{
allTokens[i] = createToken(buff, i, pos, IDENTIFIER);
}
i++;
left = right;
}
if (isDelimiter(input[right])) {
if (isLogicalOperator(&input[right]) == 1){
buff[0] = input[right];
buff[1] = '\0';
pos = right;
allTokens[i] = createToken(buff, i, pos, LOGICALOP);
i++;
}else{
buff[0] = input[right];
buff[1] = input[right+1];
buff[2] = '\0';
pos = right+1;
allTokens[i] = createToken(buff, i, pos, LOGICALOP);
right = right + 2;
left = right;
i++;
continue;
}
}else if (isAssignmentOperator(&input[right])) {
if (isAssignmentOperator(&input[right]) == 1){
buff[0] = input[right];
buff[1] = '\0';
pos = right;
allTokens[i] = createToken(buff, i, pos, ASSIGNOP);
i++;
}else{
buff[0] = input[right];
buff[1] = input[right+1];
buff[2] = '\0';
pos = right+1;
allTokens[i] = createToken(buff, i, pos, ASSIGNOP);
right = right + 2;
left = right;
i++;
continue;
}
}
else if (isArithmeticOperator(input[right])) {
buff[0] = input[right];
buff[1] = '\0';
pos = right;
allTokens[i] = createToken(buff, i, pos, ARITHOP);
i++;
}
else if (isPunctuation(input[right])) {
buff[0] = input[right];
buff[1] = '\0';
pos = right;
allTokens[i] = createToken(buff, i, pos, PUNCTUATION);
i++;
}
right++;
left = right;
}
}
}
}
#endif
#utils.h
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool isDelimiter(char c) { // a delimiter is a piece of data that marks the boundary between
lexemes, and can themselves be lexemes
return (
c == ' ' ||
c == '\n' ||
c == '+' ||
c == '-' ||
c == '*' ||
c == '/' ||
c == '%' ||
c == ',' ||
c == ';' ||
c == ':' ||
c == '>' ||
c == '<' ||
c == '=' ||
c == '!' ||
c == '(' ||
c == ')' ||
c == '[' ||
c == ']' ||
c == '{' ||
c == '}' ||
c == '|' ||
c == '&' ||
c == '^'
);
}
c == ',' ||
c == ';' ||
c == '(' ||
c == ')' ||
c == '[' ||
c == ']' ||
c == '{' ||
c == '}' ||
c == ':'
);
}
if (input[0] == '=') {
if (input[1] == '=') return 2;
}
else if (input[0] == '!') {
if (input[1] == '=') return 2;
}
else if (input[0] == '>') {
if (input[1] == '=') return 2;
return 1;
}
else if (input[0] == '<') {
if (input[1] == '=') return 2;
return 1;
}
return 0;
}
Symbol Table:
#symboltable.h
#include "token.h"
#include "string.h"
#ifndef SYMBOL_TABLE_H
#define SYMBOL_TABLE_H
void initializeSymbol(Symbol* symbol, Token* tok, int size, const char* type) {
symbol->tok = tok;
symbol->size = size;
strncpy(symbol->type, type, sizeof(symbol->type) - 1);
symbol->type[sizeof(symbol->type) - 1] = '\0';
}
#endif
Input Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
float c;
char da;
int main(){
int a;
int b;
int sum;
a = 1;
b = 1;
// singline lcomment
sum = a + b;
sum += 35;
printf("The sum of %d and %d is: %d\n", a, b, sum);
/*
here are some
multine comments here yaaay
*/
return 0;
}
main
#main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "token.h"
#include "preprocessor.h"
#include "symbolTable.h"
FILE* fa;
int SYMBOL_TABLE_SIZE = 1024;
int TOKEN_TABLE_SIZE = 1024;
void main(){
fa = fopen("./sample.c", "r");
int row = 0;
int col = 0;
char c;
if (fa == NULL){
printf("Cannot open file \n");
return;
}
c = fgetc(fa);
char text[4096];
int i = 0;
while (c != EOF){
text[i] = c;
i++;
c = fgetc(fa);
}
text[i] = '\0';
runPreprocessor(text);
printf("%s \n--------\n is the code to be passed through the lexical analyzer \n---------\n",
text);
Token* alltokens[TOKEN_TABLE_SIZE];
for (int i = 0; i < TOKEN_TABLE_SIZE; i++){
alltokens[i] = createToken("EOF", -1, -1, TOK_EOF);
}
getTokens(text, alltokens);
Symbol* symTable[SYMBOL_TABLE_SIZE];
initializeSymbolTable(symTable);
getSymbolTableFromTokens(symTable, alltokens);
}
Output: