Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.4 kB
3
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFER_SIZE 1024
 
struct input{
    char label[10];
    char opcode[10];
    char oprand[10];
}store[22];

int checkOpcode(char* word){
    FILE* file = fopen("optab.txt", "r");
    char *token;
    char buffer[BUFFER_SIZE];
    const char *delimiter_characters = " \n\t}{][)(,;*";

    while(fgets(buffer, BUFFER_SIZE, file) != NULL){
        token = strtok(buffer, delimiter_characters); 
        while (token != NULL)
        {
            if (strcmp(buffer, word) == 0){
                return 1;    
            }
            token = strtok(NULL, delimiter_characters);
        }
    }
    fclose(file);
    return 0;
}

int main()
{
    char *last_token;
    char buffer[BUFFER_SIZE];
    int col=0,index=-1,ind=0,indo=1000,counter=1000;
    const char *inpFile = "SIC_input.txt";
    const char *outFile = "SIC_output.txt";
    const char *symFile = "SIC_Symbol.txt";
    const char *delimiter_characters = " \n\t}{][)(,;*";
    FILE *fileptr1 = fopen(inpFile,"r");
    FILE *fileptr2 = fopen(outFile,"w");
    FILE *fileptr3 = fopen(symFile,"w");
 
    // Task 1 - tokenization & Task 2 - Storing in Structure
    printf("Task 1 & 2 :\n");
    int count=0;
    while(fgets(buffer, BUFFER_SIZE, fileptr1) != NULL)
    {
        index++;
        last_token = strtok(buffer, delimiter_characters); 
        while (last_token != NULL)
        {
            if(col==0) strcpy(store[index].label,last_token);
            else if(col==1) strcpy(store[index].opcode,last_token);
            else strcpy(store[index].oprand,last_token);  
            last_token = strtok(NULL, delimiter_characters);
            col++;
        }
        col=0;
        count++;
    }
    printf("Stored in Structure after Tokenising.\n\n");
 
    // Task 3 - Storing in Output Task & Task 4 - Modifying Output & Task 5 - Generate Symbol table
 
    printf("Task 3 & 4 & 5 : Creating SIC_Output.txt and SIC_Symbol.txt file ...\n");
    for(int i=0;i<count;i++)
    {
        if(strcmp(store[i].label,"END") == 0){
            break;
        }
        if(checkOpcode(store[i].label)){
            indo+=3;
        }else if(strcmp(store[i].opcode,"WORD") == 0){
            indo+=3;
        }else if(strcmp(store[i].opcode,"RESW") == 0){
            indo+=atoi(store[i].oprand)*3;
        }else if(strcmp(store[i].opcode,"RESB") == 0){
            indo+=atoi(store[i].oprand);
        }else if(strcmp(store[i].opcode,"BYTE") == 0){
            indo+=strlen(store[i].oprand)-3;
        }

        if(i==0)
        { 
            fprintf(fileptr2,"%d %s %s %s\n",indo,store[i].label,store[i].opcode,store[i].oprand);
            fprintf(fileptr3,"%d\t%s\n",indo,store[i].label);

        }
        else if(strlen(store[i].oprand) != 0 && i!=0)
        {
            fprintf(fileptr2,"%d %s %s %s\n",indo,store[i].label,store[i].opcode,store[i].oprand);
            fprintf(fileptr3,"%d\t%s\n",indo,store[i].label); 
        }
        else
        {
            fprintf(fileptr2,"%d %s %s %s\n",indo,store[i].label,store[i].opcode,store[i].oprand);
            fprintf(fileptr3,"%d\t%s\n",indo,store[i].label);
        }
    }
    fprintf(fileptr2,"%d",indo-counter);
    printf("\nGenerated...");
 
    fclose(fileptr1);
    fclose(fileptr2);
    fclose(fileptr3);

}