You are on page 1of 4

//cmt.

c -- Keywords, Comments, Identifiers, operators

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

#define MAX_IDENTIFIER_LENGTH 100


#define MAX_LINE_LENGTH 1000

bool isKeyword(char *word);


bool isOperator(char c);
bool isIdentifierCharacter(char c);
void printComments(FILE *file);
void printKeywords(FILE *file);
void printIdentifiers(FILE *file);
void printOperators(FILE *file);

int main() {
FILE *file;

// Open the file


file = fopen("input.c", "r");

// Check if file exists


if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}

// Print comments
printf("Comments:\n");
printComments(file);
printf("\n");

// Reset file pointer


fseek(file, 0, SEEK_SET);

// Print keywords
printf("Keywords:\n");
printKeywords(file);
printf("\n");

// Reset file pointer


fseek(file, 0, SEEK_SET);

// Print identifiers
printf("Identifiers:\n");
printIdentifiers(file);
printf("\n");

// Reset file pointer


fseek(file, 0, SEEK_SET);

// Print operators
printf("Operators:\n");
printOperators(file);
printf("\n");
// Close the file
fclose(file);

return 0;
}

void printComments(FILE *file) {


int c, next;

while ((c = fgetc(file)) != EOF) {


if (c == '/') {
next = fgetc(file);
if (next == '/') {
printf("//");
while ((next = fgetc(file)) != '\n' && next != EOF) {
printf("%c", next);
}
printf("\n");
} else if (next == '*') {
printf("/*");
while (1) {
c = fgetc(file);
if (c == '*') {
next = fgetc(file);
if (next == '/') {
printf("*/\n");
break;
} else {
printf("*%c", next);
}
} else {
printf("%c", c);
}
}
} else {
ungetc(next, file);
}
}
}
}

bool isKeyword(char *word) {


char keywords[][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 < sizeof(keywords) / sizeof(keywords[0]); i++) {


if (strcmp(word, keywords[i]) == 0) {
return true;
}
}

return false;
}
void printKeywords(FILE *file) {
char word[MAX_IDENTIFIER_LENGTH];
int index = 0;
int c;

while ((c = fgetc(file)) != EOF) {


if (isalnum(c) || c == '_') {
word[index++] = c;
} else {
if (index > 0) {
word[index] = '\0';
if (isKeyword(word)) {
printf("%s\n", word);
}
index = 0;
}
}
}
}

bool isOperator(char c) {
char operators[] = "+-*/%=<>!&|^~?:.";

for (int i = 0; i < strlen(operators); i++) {


if (c == operators[i]) {
return true;
}
}

return false;
}

void printOperators(FILE *file) {


int c;

while ((c = fgetc(file)) != EOF) {


if (isOperator(c)) {
printf("%c\n", c);
}
}
}

bool isIdentifierCharacter(char c) {
return isalnum(c) || c == '_';
}

void printIdentifiers(FILE *file) {


char identifier[MAX_IDENTIFIER_LENGTH];
int index = 0;
int c;

while ((c = fgetc(file)) != EOF) {


if (isIdentifierCharacter(c)) {
identifier[index++] = c;
} else {
if (index > 0) {
identifier[index] = '\0';
printf("%s\n", identifier);
index = 0;
}
}
}
}

//input.c

#include<stdio.h>
#include<stdlib.h>
//input.c is used to read this file and mention comments

int main(){
//Printing Hello World
printf("Hello World");
//Printed Hello WOrld
return 0;
//returning 0
}

You might also like