You are on page 1of 5

Course name: Compiler Design Lab

Course Code: CSE332

Lab Report on:


Write a C program to take a list of identifiers from the user and display the
validity of each identifier.

Submitted To:
Fahim Faisal
Lecturer
Department of Computer Science and Engineering
Daffodil International University
Submitted By:
Name: Sabrina Sharmin
ID: 181-15-961
Section: 59_A1
Program: CSE

Date of Submission: 12th March 2024


Lab Report no: 05

Title: Write a C program to take a list of identifiers from the user and display the
validity of each identifier.

Objective:
The objective of this program is to validate each string entered by the user, separated
by spaces. It checks if each string conforms to the rules for a valid identifier in C, which
requires that the first character must be a letter (uppercase or lowercase), underscore
(_), or dollar sign ($), and subsequent characters can also include digits (0-9). Any
string not meeting these criteria is considered invalid. The program prints whether each
string entered is valid or not.

Program Logic:

● The program takes input from the user as a string and processes each word
separated by spaces.
● It iterates through each character of the word, validating whether it meets the
criteria for a valid identifier in C.
● The validation function examines each character in the string to ensure it falls
within the acceptable range of characters for an identifier.
● If any character violates the rules, the string is marked as invalid; otherwise, it's
considered valid.
● The program prints the validity status of each string entered by the user.

Procedure:

● Prompt the user to enter a string.


● Separate the string into individual words based on spaces.
● Validate each word according to C identifier rules: first character must be a letter,
underscore, or dollar sign, subsequent characters can include letters, digits,
underscores, or dollar signs.
● Print whether each word is valid or invalid.
● Repeat steps 2-4 until all words have been processed.
● After the loop, the program checks the value of the result variable. If it is 0, it
prints "Input String is Keyword"; otherwise, it prints "Input String is not keyword".
● Finally, the program returns 0 to indicate successful execution.
Source code:

#include<stdio.h>
#include<ctype.h>
#include<string.h>
void test(char str[100]) {
int c = 1, i, n;
n = strlen(str);
if (!((str[0] >= 'a' && str[0] <= 'z') || (str[0] >= 'A' && str[0] <= 'Z') || str[0] == '_' || str[0]
== '$')) {
c = 0;
}

for (i = 1; i < n; i++) {


if (!((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '_' || str[i] ==
'$' || (str[i] >= '0' && str[i] <= '9'))) {
c = 0;
break;
}
}

if (!((str[n - 1] >= 'a' && str[n - 1] <= 'z') || (str[n - 1] >= 'A' && str[n - 1] <= 'Z') || str[n -
1] == '$' || (str[n - 1] >= '0' && str[n - 1] <= '9'))) {
c = 0;
}

if (c == 1) {
printf("'%s' is Valid \n", str);
} else {
printf("'%s' is Invalid \n", str);
}
}
void main() {
int i, n, j=0;
char cr[100];
printf("Enter your string: ");
gets(cr);
n = strlen(cr);

char str[100] = "";


for (i=0; i<n; i++) {
int x=isspace(cr[i]);
if (x==0) {
str[j]=cr[i];
j++;
}
else
{
test(str);
j = 0;
str[0] = '\0';
}
}

if (j > 0) {
test(str);
}

Result:

Conclusion:

This program effectively validates user-input strings as C identifiers, ensuring


adherence to the language's rules for identifier formation. By iteratively examining each
word and character, it accurately determines the validity of each identifier. Through clear
prompts and concise output, it provides users with immediate feedback on the validity of
their input

You might also like