Untitled

 avatar
plain_text
14 hours ago
6.5 KB
8
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int isDataType(char word[])
{
    char *datatype[] = {
        "int", "float", "double", "char",
        "long", "short", "void", "bool"
    };

    int n = sizeof(datatype) / sizeof(datatype[0]);
    int i;

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

int isKeyword(char word[])
{
    char *keyword[] = {
        "if", "else", "for", "while", "do",
        "switch", "case", "break", "continue",
        "return", "goto", "sizeof", "struct",
        "union", "enum", "typedef", "const",
        "static", "extern", "register", "auto",
        "default","printf","scanf","get"
    };

    int n = sizeof(keyword) / sizeof(keyword[0]);
    int i;

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

int isOperator(char ch)
{
    char operators[] = "+-*/%=<>!&|^";
    int i;

    for(i = 0; operators[i] != '\0'; i++)
    {
        if(ch == operators[i])
            return 1;
    }
    return 0;
}

int isSeparator(char ch)
{
    char separators[] = ",;(){}[]";
    int i;

    for(i = 0; separators[i] != '\0'; i++)
    {
        if(ch == separators[i])
            return 1;
    }
    return 0;
}

int main()
{
    FILE *fp = fopen("input.c", "r");

    if(fp == NULL)
    {
        printf("Error opening input file.\n");
        return 1;
    }

    char code[10000];
    int index = 0;
    char ch, next;

    /* Remove comments */
    while((ch = fgetc(fp)) != EOF)
    {
        /* String literal */
        if(ch == '"')
        {
            code[index++] = ch;
            while((ch = fgetc(fp)) != EOF)
            {
                code[index++] = ch;

                if(ch == '\\')
                {
                    ch = fgetc(fp);
                    if(ch == EOF)
                        break;
                    code[index++] = ch;
                }
                else if(ch == '"')
                    break;
            }
        }

        /* Character literal */
        else if(ch == '\'')
        {
            code[index++] = ch;
            while((ch = fgetc(fp)) != EOF)
            {
                code[index++] = ch;

                if(ch == '\\')
                {
                    ch = fgetc(fp);
                    if(ch == EOF)
                        break;
                    code[index++] = ch;
                }
                else if(ch == '\'')
                    break;
            }
        }

        /* Comments */
        else if(ch == '/')
        {
            next = fgetc(fp);

            /* Single-line comment */
            if(next == '/')
            {
                while((ch = fgetc(fp)) != EOF && ch != '\n');
                if(ch == '\n')
                    code[index++] = '\n';
            }

            /* Multi-line comment */
            else if(next == '*')
            {
                char prev = 0;
                while((ch = fgetc(fp)) != EOF)
                {
                    if(prev == '*' && ch == '/')
                        break;
                    prev = ch;
                }
            }

            /* Not a comment */
            else
            {
                code[index++] = ch;
                if(next != EOF)
                    ungetc(next, fp);
            }
        }

        else
        {
            code[index++] = ch;
        }
    }

    code[index] = '\0';
    fclose(fp);

    printf("Code after removing comments:\n\n");
    printf("%s\n", code);

    printf("\nLexical Analysis\n");
    printf("---------------------------------\n");
    printf("Token\t\tType\n");
    printf("---------------------------------\n");

    char token[100];
    int j = 0,i;

    for( i = 0;; i++)
    {
        if(isalnum(code[i]) || code[i] == '_')
        {
            token[j++] = code[i];
        }
        else
        {
            if(j > 0)
            {
                token[j] = '\0';

                if(isDataType(token))
                    printf("%-15s Data Type\n", token);

                else if(isKeyword(token))
                    printf("%-15s Keyword\n", token);

                else if(isdigit(token[0]))
                    printf("%-15s Number\n", token);

                else
                    printf("%-15s Identifier\n", token);

                j = 0;
            }

            if(isOperator(code[i]))
            {
                printf("%-15c Operator\n", code[i]);
            }

            if(isSeparator(code[i]))
            {
                printf("%-15c Separator\n", code[i]);
            }

            if(code[i] == '\0')
                break;
        }
    }

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