#include <stdio.
h>
#include <ctype.h>
#include <string.h>
#define MAX_ID_LEN 20 // Maximum length of an identifier
// List of keywords
const char *keywords[] = {"int", "float", "if", "else", "while", "return", "for", "char", "void"};
#define NUM_KEYWORDS (sizeof(keywords) / sizeof(keywords[0]))
// Function to check if a string is a keyword
int isKeyword(const char *str) {
    for (int i = 0; i < NUM_KEYWORDS; i++) {
        if (strcmp(str, keywords[i]) == 0)
          return 1;
    return 0;
// Function to detect operators
int isOperator(char ch) {
    return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=' || ch == '>' || ch == '<' || ch == '!');
// Lexical Analyzer using User Input
void lexicalAnalyzer(const char *input) {
  char buffer[MAX_ID_LEN + 1];
  int bufferIndex = 0;
  int i = 0;
  while (input[i] != '\0') {
    char ch = input[i];
    // Ignore whitespace, tabs, and new lines
    if (ch == ' ' || ch == '\t' || ch == '\n') {
        i++;
        continue;
    // Detect identifiers and keywords
    if (isalpha(ch) || ch == '_') {
        bufferIndex = 0;
        while (isalnum(ch) || ch == '_') {
            if (bufferIndex < MAX_ID_LEN)
               buffer[bufferIndex++] = ch;
            ch = input[++i];
        buffer[bufferIndex] = '\0';
        if (isKeyword(buffer))
        printf("Keyword: %s\n", buffer);
    else
        printf("Identifier: %s\n", buffer);
    continue;
// Detect numbers
else if (isdigit(ch)) {
    bufferIndex = 0;
    while (isdigit(ch) || ch == '.') {
        buffer[bufferIndex++] = ch;
        ch = input[++i];
    buffer[bufferIndex] = '\0';
    printf("Number: %s\n", buffer);
    continue;
// Detect operators
else if (isOperator(ch)) {
    printf("Operator: %c\n", ch);
// Detect symbols
else if (ch == ';' || ch == ',' || ch == '(' || ch == ')' ||
               ch == '{' || ch == '}' || ch == '[' || ch == ']') {
            printf("Symbol: %c\n", ch);
        i++;
int main() {
    char input[500];
    // Prompt for user input
    printf("Enter code snippet:\n");
    fgets(input, sizeof(input), stdin); // Taking input from user
    printf("\nLexical Analysis Output:\n");
    lexicalAnalyzer(input); // Perform lexical analysis
    return 0;