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

Compiler Design Lab 2

Uploaded by

srinidhi.k2022
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)
20 views

Compiler Design Lab 2

Uploaded by

srinidhi.k2022
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/ 15

Compiler Design Lab 2

Name:Chavva Sai Yashwanth Reddy

Reg No:22BCE1889

a)Write a program to determine whether the given input is a valid identifier or not (with
and without regular expression )C program

With regular expression:-

#include <stdio.h>

#include <regex.h>

int isValidIdentifier(const char *str) {

regex_t regex;

int result;

const char *pattern = "^[A-Za-z_][A-Za-z0-9_]*$";

result = regcomp(&regex, pattern, REG_EXTENDED);

if (result) {

printf("Could not compile regex\n");

return 0;

result = regexec(&regex, str, 0, NULL, 0);

regfree(&regex);

if (!result) {

return 1;

} else if (result == REG_NOMATCH) {

return 0;

} else {
printf("Regex match failed\n");

return 0;

int main() {

char input[100];

printf("Enter an identifier: ");

scanf("%s", input);

if (isValidIdentifier(input)) {

printf("Valid identifier.\n");

} else {

printf("Invalid identifier.\n");

printf("22BCE1889 Chavva Sai Yashwanth");

return 0;
}

Without regular expression:-

#include <stdio.h>

#include <ctype.h>

int isValidIdentifier(const char *str) {

if (!isalpha(str[0]) && str[0] != '_') {

return 0;
}

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

if (!isalnum(str[i]) && str[i] != '_') {

return 0;

return 1;

int main() {

char input[100];

printf("Enter an identifier: ");

scanf("%s", input);

if (isValidIdentifier(input)) {

printf("Valid identifier.\n");

} else {

printf("Invalid identifier.\n");

printf("22BCE1889 Chavva Sai Yashwanth");

return 0;
}

b)Write a program to check whether a given string will get accepted for the
below regular expression C program
i)ab

#include <stdio.h>

#include <regex.h>

int matchRegex(const char *str, const char *pattern) {


regex_t regex;

int result;

result = regcomp(&regex, pattern, REG_EXTENDED);

if (result) {

printf("Could not compile regex\n");

return 0;

result = regexec(&regex, str, 0, NULL, 0);

regfree(&regex);

if (!result) {

return 1; // Match

} else if (result == REG_NOMATCH) {

return 0; // No match

} else {

printf("Regex match failed\n");

return 0;

int main() {

char input[100];

printf("Enter a string: ");

scanf("%s", input);
if (matchRegex(input, "ab")) {

printf("String accepted.\n");

} else {

printf("String not accepted.\n");

printf("22BCE1889 Chavva Sai Yashwanth");

return 0;

}
ii)(a)*abb

#include <stdio.h>

#include <regex.h>

int matchRegex(const char *str, const char *pattern) {

regex_t regex;

int result;

result = regcomp(&regex, pattern, REG_EXTENDED);

if (result) {

printf("Could not compile regex\n");

return 0;

result = regexec(&regex, str, 0, NULL, 0);

regfree(&regex);

if (!result) {
return 1; // Match

} else if (result == REG_NOMATCH) {

return 0; // No match

} else {

printf("Regex match failed\n");

return 0;

int main() {

char input[100];

printf("Enter a string: ");

scanf("%s", input);

if (matchRegex(input, "(a)*abb")) {

printf("String is matching with the regular expression (a)*abb.\n");

} else {

printf("String not accepted.\n");

return 0;

}
iii)(ab)*a

#include <stdio.h>

#include <regex.h>

int matchRegex(const char *str, const char *pattern) {

regex_t regex;

int result;
result = regcomp(&regex, pattern, REG_EXTENDED);

if (result) {

printf("Could not compile regex\n");

return 0;

result = regexec(&regex, str, 0, NULL, 0);

regfree(&regex);

if (!result) {

return 1; // Match

} else if (result == REG_NOMATCH) {

return 0; // No match

} else {

printf("Regex match failed\n");

return 0;

int main() {

char input[100];

printf("Enter a string: ");

scanf("%s", input);

if (matchRegex(input, "(ab)*a")) {

printf("String accepted.\n");

} else {
printf("String not accepted.\n");

printf("22BCE1889 Chavva Sai Yashwanth");

return 0;

iv)b(a)+b
#include <stdio.h>

#include <regex.h>

int matchRegex(const char *str, const char *pattern) {

regex_t regex;

int result;

result = regcomp(&regex, pattern, REG_EXTENDED);

if (result) {

printf("Could not compile regex\n");

return 0;

result = regexec(&regex, str, 0, NULL, 0);

regfree(&regex);

if (!result) {

return 1; // Match

} else if (result == REG_NOMATCH) {

return 0; // No match

} else {

printf("Regex match failed\n");

return 0;

int main() {
char input[100];

printf("Enter a string: ");

scanf("%s", input);

if (matchRegex(input, "b(a)+b")) {

printf("String is matching with the regular expression b(a)+b.\n");

} else {

printf("String not accepted.\n");

printf("22BCE1889 Chavva Sai Yashwanth");

return 0;

You might also like