0% found this document useful (0 votes)
24 views4 pages

Compiler .CPPP

This document contains a C program that implements a lexical analyzer to identify keywords, identifiers, numbers, operators, and symbols from user input. It defines a set of keywords and includes functions to check for keywords and operators. The main function prompts the user for a code snippet and processes it through the lexical analyzer to produce output based on the detected elements.

Uploaded by

sachinkartik956
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Compiler .CPPP

This document contains a C program that implements a lexical analyzer to identify keywords, identifiers, numbers, operators, and symbols from user input. It defines a set of keywords and includes functions to check for keywords and operators. The main function prompts the user for a code snippet and processes it through the lexical analyzer to produce output based on the detected elements.

Uploaded by

sachinkartik956
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#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;

You might also like