0% found this document useful (0 votes)
0 views

Compiler Design Task1

The document contains a C program that identifies keywords and identifiers from user-inputted C code, ending input with a '#'. It processes the input to extract numbers and counts the total number of lines. The program outputs the identified keywords, identifiers, the numbers found, and the total line count.

Uploaded by

sachinkartik956
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Compiler Design Task1

The document contains a C program that identifies keywords and identifiers from user-inputted C code, ending input with a '#'. It processes the input to extract numbers and counts the total number of lines. The program outputs the identified keywords, identifiers, the numbers found, and the total line count.

Uploaded by

sachinkartik956
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Compiler Design Task1

#include <stdio.h>

#include <string.h>

#include <ctype.h>

void keyword(char str[10]) {

if (strcmp("for", str) == 0 || strcmp("while", str) == 0 || strcmp("do", str) == 0 ||

strcmp("int", str) == 0 || strcmp("float", str) == 0 || strcmp("char", str) == 0 ||

strcmp("double", str) == 0 || strcmp("static", str) == 0 || strcmp("switch", str) == 0 ||

strcmp("case", str) == 0 || strcmp("return", str) == 0 || strcmp("void", str) == 0) {

printf("\n%s is a keyword", str);

} else {

printf("\n%s is an identifier", str);

int main() {

char code[1000], str[10], c;

int num[100], lineno = 1, tokenvalue = 0, i = 0, j = 0, k = 0;

printf("Enter the C program (End with #):\n");

// Read input from user

int index = 0;
while ((c = getchar()) != '#') { // Using '#' as EOF (since online compilers don't support
Ctrl+Z)

code[index++] = c;

code[index] = '\0'; // Null-terminate the string

printf("\nProcessing...\n");

for (int pos = 0; code[pos] != '\0'; pos++) {

c = code[pos];

// Handling numbers

if (isdigit(c)) {

tokenvalue = c - '0';

while (isdigit(code[++pos])) {

tokenvalue = tokenvalue * 10 + (code[pos] - '0');

num[i++] = tokenvalue;

pos--; // Step back to process next character

// Handling identifiers and keywords

else if (isalpha(c)) {

k = 0;

while (isalnum(c) || c == '_' || c == '$') {


str[k++] = c;

c = code[++pos];

str[k] = '\0'; // Null terminate the token

keyword(str);

pos--; // Step back

// Handling line count

else if (c == '\n') {

lineno++;

// Output results

printf("\nThe numbers in the program are: ");

for (j = 0; j < i; j++)

printf("%d ", num[j]);

printf("\nTotal number of lines: %d\n", lineno);

return 0;

You might also like