C Programs Showcase

Functional First Set Program

#include <stdio.h>
#define MAX 10

int main() {
    int i, numNT, numT;
    char NT[MAX], T[MAX];
    char prod[MAX][MAX];
    char first[MAX];

    printf("Enter number of non-terminals: ");
    scanf("%d", &numNT);
    printf("Enter non-terminals:\n");
    for (i = 0; i < numNT; i++)
        scanf(" %c", &NT[i]);

    printf("Enter number of terminals (use { for epsilon): ");
    scanf("%d", &numT);
    printf("Enter terminals:\n");
    for (i = 0; i < numT; i++)
        scanf(" %c", &T[i]);

    printf("Enter one production for each non-terminal (end RHS with $):\n");
    for (i = 0; i < numNT; i++) {
        printf("Production for %c: ", NT[i]);
        scanf(" %s", prod[i]);
    }

    for (i = 0; i < numNT; i++) {
        first[i] = prod[i][1];
    }

    printf("\nFirst Sets:\n");
    for (i = 0; i < numNT; i++) {
        printf("First(%c) = { %c }\n", NT[i], first[i]);
    }

    return 0;
}
💡 Sample I/O:

Input:
2
A B
3
a b {
Aa$
Bb$

Output:
First(A) = { a }
First(B) = { b }

String Identifier Checker

#include <stdio.h>
#include <ctype.h>

int main() {
    char str[50];
    int i, flag = 1;

    printf("Enter the string: ");
    scanf("%s", str);

    if (!(isalpha(str[0]) || str[0] == '_')) {
        flag = 0;
    } else {
        for (i = 1; str[i] != '\0'; i++) {
            if (!(isalnum(str[i]) || str[i] == '_')) {
                flag = 0;
                break;
            }
        }
    }

    if (flag)
        printf("Identifier\n");
    else
        printf("Not Identifier\n");
}
💡 Sample I/O:

Input: _value2
Output: Identifier

Keyword Checker

#include <stdio.h>
#include <string.h>

int main() {
    char keywords[5][10] = {"printf", "scanf", "if", "else", "break"};
    char str[10];
    int i, flag = 0;

    printf("Enter the string: ");
    scanf("%s", str);

    for (i = 0; i < 5; i++) {
        if (strcmp(str, keywords[i]) == 0) {
            flag = 1;
            break;
        }
    }

    if (flag)
        printf("Keyword\n");
    else
        printf("String\n");

    return 0;
}
💡 Sample I/O:

Input: scanf
Output: Keyword

Left Recursion Remover

#include <stdio.h>

int main() {
    char nonTerminal;
    char prod1[20], prod2[20];

    printf("Enter non-terminal: ");
    scanf(" %c", &nonTerminal);

    printf("Enter first production: ");
    scanf("%s", prod1);

    printf("Enter second production: ");
    scanf("%s", prod2);

    if (prod1[0] == nonTerminal) {
        printf("\nLeft recursion exists!\n");
        printf("After removing left recursion:\n");

        printf("%c -> %s%c'\n", nonTerminal, prod2, nonTerminal);
        printf("%c' -> ", nonTerminal);
        for (int i = 1; prod1[i] != '\0'; i++) {
            printf("%c", prod1[i]);
        }
        printf("%c' | ε\n", nonTerminal);
    } else {
        printf("\nNo left recursion.\n");
        printf("Grammar remains same: %c -> %s | %s\n", nonTerminal, prod1, prod2);
    }

    return 0;
}
💡 Sample I/O:

Input:
A
Aa
b

Output:
Left recursion exists!
A -> bA'
A' -> aA' | ε

Lexical Analyzer

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char keywords[10][10] = {"int", "if", "else", "while", "return", "float", "char", "for", "do", "void"};

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

int main() {
    char input[100], token[100];
    int i = 0, j = 0;

    printf("Enter a line of code: ");
    fgets(input, sizeof(input), stdin);

    while (input[i] != '\0') {
        if (isalnum(input[i])) {
            token[j++] = input[i];
        } else {
            if (j != 0) {
                token[j] = '\0';
                j = 0;

                if (isKeyword(token))
                    printf("[Keyword: %s]\n", token);
                else if (isdigit(token[0]))
                    printf("[Number: %s]\n", token);
                else
                    printf("[Identifier: %s]\n", token);
            }

            if (ispunct(input[i]))
                printf("[Symbol: %c]\n", input[i]);
        }
        i++;
    }

    return 0;
}
💡 Sample I/O:

Input: int x = 10;
Output:
[Keyword: int]
[Identifier: x]
[Symbol: =]
[Number: 10]
[Symbol: ;]

Expression Validator with Tokens

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char expr[100], tok[100];
    int i = 0, j = 0;

    printf("Enter expression: ");
    fgets(expr, sizeof(expr), stdin);

    while (expr[i]) {
        if (isdigit(expr[i])) {
            tok[j++] = '6';
            while (isdigit(expr[i])) i++;
        } else if (expr[i] == '+') tok[j++] = '2', i++;
        else if (expr[i] == '*') tok[j++] = '3', i++;
        else if (expr[i] == '(' || expr[i] == '{') tok[j++] = '4', i++;
        else if (expr[i] == ')' || expr[i] == '}') tok[j++] = '5', i++;
        else i++;
    }
    tok[j] = '\0';

    printf("Tokens: ");
    for (i = 0; tok[i]; i++) {
        printf("%c ", tok[i]);
    }
    printf("\n");

    int changed;
    do {
        changed = 0;
        for (i = 0; tok[i + 2]; i++) {
            if ((tok[i] == '6' && (tok[i + 1] == '2' || tok[i + 1] == '3') && tok[i + 2] == '6') ||
                (tok[i] == '4' && tok[i + 1] == '6' && tok[i + 2] == '5')) {
                tok[i] = '6';
                strcpy(&tok[i + 1], &tok[i + 3]);
                changed = 1;
                break;
            }
        }
    } while (changed);

    if (strcmp(tok, "6") == 0)
        printf("Valid\n");
    else
        printf("Invalid\n");

    return 0;
}
💡 Sample I/O:

Input: (2 + 3) * 5
Output:
Tokens: 4 6 2 6 5 3 6
Valid