fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. // Function to check if a string is a keyword
  7. int isKeyword(char buffer[])
  8. {
  9. char keywords[32][10] = {
  10. "auto","break","case","char","const","continue","default",
  11. "do","double","else","enum","extern","float","for","goto",
  12. "if","int","long","register","return","short","signed",
  13. "sizeof","static","struct","switch","typedef","union",
  14. "unsigned","void","volatile","while"
  15. };
  16.  
  17. for(int i = 0; i < 32; ++i)
  18. {
  19. if(strcmp(keywords[i], buffer) == 0)
  20. {
  21. return 1; // Keyword found
  22. }
  23. }
  24. return 0; // Not a keyword
  25. }
  26.  
  27. // Function to check if a character is an operator
  28. int isOperator(char ch)
  29. {
  30. char op[] = {'+', '-', '*', '/', '%', '='};
  31. for(int i = 0; i < 6; i++)
  32. {
  33. if(ch == op[i])
  34. {
  35. return 1; // Operator found
  36. }
  37. }
  38. return 0; // Not an operator
  39. }
  40.  
  41. // Function to check if a character is a symbol
  42. int isSymbol(char ch)
  43. {
  44. char sy[] = {',', ';', '(', ')', '{', '}'};
  45. for(int i = 0; i < 6; i++)
  46. {
  47. if(ch == sy[i])
  48. {
  49. return 1; // Symbol found
  50. }
  51. }
  52. return 0; // Not a symbol
  53. }
  54.  
  55. int main()
  56. {
  57. char ch, buffer[15],token=0;
  58. FILE *fp;
  59. int j = 0;
  60.  
  61. // Open the file in read mode
  62. fp = fopen("NUB.txt", "r");
  63. if(fp == NULL)
  64. {
  65. printf("Error while opening the file\n");
  66. exit(0);
  67. }
  68.  
  69. printf("Lexical Analysis:\n\n");
  70. while((ch = fgetc(fp)) != EOF)
  71. {
  72. // Check for alphanumeric characters
  73. if(isalnum(ch))
  74. {
  75. buffer[j++] = ch; // Add to buffer
  76. }
  77. else
  78. {
  79. if(j != 0) // Process the buffer if it contains something
  80. {
  81. token++;
  82. buffer[j] = '\0'; // Null-terminate the string
  83. j = 0;
  84.  
  85. if(isKeyword(buffer))
  86. printf("%s is a keyword\n", buffer);
  87. else
  88. printf("%s is an identifier\n", buffer);
  89. }
  90.  
  91. // Check for operators
  92. if(isOperator(ch))
  93. {
  94. token++;
  95. printf("%c is an operator\n", ch);
  96. }
  97. // Check for symbols
  98. else if(isSymbol(ch))
  99. {
  100. token++;
  101. printf("%c is a symbol\n", ch);
  102. }
  103. }
  104. }
  105. printf("Total Token: %d \n\n",token);
  106. fclose(fp); // Close the file
  107. return 0;
  108. }
  109.  
Success #stdin #stdout 0.03s 25956KB
stdin
Standard input is empty
stdout
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

// Function to check if a string is a keyword
int isKeyword(char buffer[])
{
    char keywords[32][10] = {
        "auto","break","case","char","const","continue","default",
        "do","double","else","enum","extern","float","for","goto",
        "if","int","long","register","return","short","signed",
        "sizeof","static","struct","switch","typedef","union",
        "unsigned","void","volatile","while"
    };

    for(int i = 0; i < 32; ++i)
    {
        if(strcmp(keywords[i], buffer) == 0)
        {
            return 1; // Keyword found
        }
    }
    return 0; // Not a keyword
}

// Function to check if a character is an operator
int isOperator(char ch)
{
    char op[] = {'+', '-', '*', '/', '%', '='};
    for(int i = 0; i < 6; i++)
    {
        if(ch == op[i])
        {
            return 1; // Operator found
        }
    }
    return 0; // Not an operator
}

// Function to check if a character is a symbol
int isSymbol(char ch)
{
    char sy[] = {',', ';', '(', ')', '{', '}'};
    for(int i = 0; i < 6; i++)
    {
        if(ch == sy[i])
        {
            return 1; // Symbol found
        }
    }
    return 0; // Not a symbol
}

int main()
{
    char ch, buffer[15],token=0;
    FILE *fp;
    int j = 0;

    // Open the file in read mode
    fp = fopen("NUB.txt", "r");
    if(fp == NULL)
    {
        printf("Error while opening the file\n");
        exit(0);
    }

    printf("Lexical Analysis:\n\n");
    while((ch = fgetc(fp)) != EOF)
    {
        // Check for alphanumeric characters
        if(isalnum(ch))
        {
            buffer[j++] = ch; // Add to buffer
        }
        else
        {
            if(j != 0) // Process the buffer if it contains something
            {
                token++;
                buffer[j] = '\0'; // Null-terminate the string
                j = 0;

                if(isKeyword(buffer))
                    printf("%s is a keyword\n", buffer);
                else
                    printf("%s is an identifier\n", buffer);
            }

            // Check for operators
            if(isOperator(ch))
            {
                token++;
                printf("%c is an operator\n", ch);
            }
            // Check for symbols
            else if(isSymbol(ch))
            {
                token++;
                printf("%c is a symbol\n", ch);
            }
        }
    }
    printf("Total Token: %d \n\n",token);
    fclose(fp); // Close the file
    return 0;
}