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

CC Lab1

The document contains code for a program that reads a C source code file and extracts comments, keywords, identifiers, and operators printing each category separately. It defines functions to check for keywords, operators, identifier characters, and print each category.

Uploaded by

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

CC Lab1

The document contains code for a program that reads a C source code file and extracts comments, keywords, identifiers, and operators printing each category separately. It defines functions to check for keywords, operators, identifier characters, and print each category.

Uploaded by

sundermantu06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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