op

 avatar
unknown
plain_text
4 years ago
3.0 kB
6
Indexable
#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;
    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(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);
            indo+=3;

        }
        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); 
            indo+=3;  
        }
        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);
            indo+=3;
        }
    }
    printf("\nGenerated...");
 
    fclose(fileptr1);
    fclose(fileptr2);
    fclose(fileptr3);

    for(int i=0; i<count; i++){
        if(checkOpcode(store[i].label)){
            printf("\nSiiiiuu\n");
        }
    }
}
Editor is loading...