0% found this document useful (0 votes)
212 views5 pages

Write A C Program To Test Whether A Given Identifier Is Valid or Not

The document contains two C programs: the first tests the validity of a given identifier based on specific rules, while the second simulates a lexical analyzer to validate operators in an expression. The identifier validation checks the first character and subsequent characters against alphanumeric and underscore criteria, and also checks against reserved keywords. The operator validation identifies valid operators and reports invalid characters in the provided expression.

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)
212 views5 pages

Write A C Program To Test Whether A Given Identifier Is Valid or Not

The document contains two C programs: the first tests the validity of a given identifier based on specific rules, while the second simulates a lexical analyzer to validate operators in an expression. The identifier validation checks the first character and subsequent characters against alphanumeric and underscore criteria, and also checks against reserved keywords. The operator validation identifies valid operators and reports invalid characters in the provided expression.

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
You are on page 1/ 5

PRACTICAL 04

AIM:- Write a C program to test whether a given identifier is valid or not.


#include <stdio.h>

#include <ctype.h>

#include <string.h>

int is_valid_identifier(char str[]) {

if (!(isalpha(str[0]) || str[0] == '_')) return 0; // First char must be letter or underscore

for (int i = 1; str[i] != '\0'; i++) {

if (!(isalnum(str[i]) || str[i] == '_')) return 0; // Subsequent chars must be alphanumeric or


underscore

// Reserved keywords (for simplicity, checking only a few)

const char* keywords[] = {"int", "char", "if", "else", "return"};

for (int i = 0; i < 5; i++) {

if (strcmp(str, keywords[i]) == 0) return 0; // Match with keyword

return 1; // Valid identifier

int main() {

char identifier[100];

printf("Enter an identifier: ");

scanf("%s", identifier);

printf("%s is %sa valid identifier.\n", identifier, is_valid_identifier(identifier) ? "" : "not ");

return 0;

}
Output :-
PRACTICAL 05

AIM :- Write a C program to simulate lexical analyzer for validating


operators.
#include <stdio.h>

#include <string.h>

#include <ctype.h>

int is_operator(char c) {

// Check if the character is a valid operator

return (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' ||

c == '=' || c == '<' || c == '>' || c == '!' || c == '&' ||

c == '|' || c == '^' || c == '~');

void analyze_operator(char *expr) {

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

if (is_operator(expr[i])) {

printf("Operator: %c\n", expr[i]);

} else if (isspace(expr[i])) {

continue; // Ignore spaces

} else {

printf("Invalid character: %c\n", expr[i]);

int main() {

char expression[100];
printf("Enter an expression: ");

fgets(expression, sizeof(expression), stdin);

analyze_operator(expression);

return 0;

Output :-

You might also like