Untitled

 avatar
plain_text
14 hours ago
4.3 KB
2
Indexable
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char *keywords[] = {
    "int","float","char","double","if","else","while","for",
    "return","void","break","continue","switch","case","default",
    "do","struct","typedef","long","short","unsigned","signed",
    "const","static","sizeof","goto","enum","printf","union","extern"
};

int isKeyword(char word[])
{
    int n = sizeof(keywords)/sizeof(keywords[0]);

    for(int i=0;i<n;i++)
    {
        if(strcmp(word,keywords[i])==0)
            return 1;
    }
    return 0;
}

int main()
{
    FILE *fp, *out;
    char ch, next;
    char word[100];
    int i;

    fp = fopen("input.c", "r");

    if(fp == NULL)
    {
        printf("Cannot open input file.\n");
        return 0;
    }

    out = fopen("output.c", "w");

    printf("Tokens:\n\n");

    while((ch = fgetc(fp)) != EOF)
    {
        // Remove comments
        if(ch == '/')
        {
            next = fgetc(fp);

            // Single-line comment
            if(next == '/')
            {
                while((ch = fgetc(fp)) != '\n' && ch != EOF);

                if(ch == '\n')
                    fputc('\n', out);

                continue;
            }

            // Multi-line comment
            else if(next == '*')
            {
                char prev = ' ';

                while((ch = fgetc(fp)) != EOF)
                {
                    if(prev == '*' && ch == '/')
                        break;

                    prev = ch;
                }

                continue;
            }

            else
            {
                ungetc(next, fp);
            }
        }

        // Keywords / Identifiers
        if(isalpha(ch) || ch == '_')
        {
            i = 0;
            word[i++] = ch;

            while((ch = fgetc(fp)) != EOF && (isalnum(ch) || ch == '_'))
            {
                word[i++] = ch;
            }

            word[i] = '\0';

            // Write complete word
            fprintf(out, "%s", word);

            if(isKeyword(word))
                printf("Keyword      : %s\n", word);
            else
                printf("Identifier   : %s\n", word);

            if(ch != EOF)
                ungetc(ch, fp);

            continue;
        }

        // Numbers
        if(isdigit(ch))
        {
            i = 0;
            word[i++] = ch;

            while((ch = fgetc(fp)) != EOF && isdigit(ch))
            {
                word[i++] = ch;
            }

            word[i] = '\0';

            fprintf(out, "%s", word);

            printf("Number       : %s\n", word);

            if(ch != EOF)
                ungetc(ch, fp);

            continue;
        }

        // Operators
        if(strchr("+-*/=%<>!", ch))
        {
            printf("Operator     : %c\n", ch);
        }

        // Special Symbols
        else if(strchr("(){}[];,", ch))
        {
            printf("Special Sym. : %c\n", ch);
        }

        // Write remaining characters
        fputc(ch, out);
    }

    fclose(fp);
    fclose(out);

    printf("\nComments removed successfully.\n");
    printf("Output stored in output.c\n");

    return 0;
}
Editor is loading...
Leave a Comment