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

Practical CD

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

Practical CD

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

Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

Practical-1
AIM: Program to implement Lexical Analyzer.
CODE:
code = input("Enter the string: ")
items = code.split()

for x in items:
if x in ["int", "float"]:
print("Keyword",x)
elif x.isdigit():
print("Constant", x)
elif x in ["+", "-", "*", "/", "%", "="]:
print("Operator", x)
elif x in [",", ";", "(", ")"]:
print("Symbol", x)
else:
print("Identifier", x)
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

OUTPUT:
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

Practical-2
AIM: Program to count digits, vowels and symbols in C
CODE:
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void countDigitsVowelsSymbols(const char *str, int


*digits, int *vowels, int *symbols) {
*digits = 0;
*vowels = 0;
*symbols = 0;

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


char ch = str[i];

if (isdigit(ch)) {
(*digits)++;
} else if (isalpha(ch)) {
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

switch (tolower(ch)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
(*vowels)++;
break;
default:
break;
}
} else {

(*symbols)++;
}
}
}
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

int main() {
char inputString[100];
int digitCount = 0, vowelCount = 0, symbolCount = 0;

printf("Enter a string: ");


fgets(inputString, sizeof(inputString), stdin);

inputString[strcspn(inputString, "\n")] = '\0';

countDigitsVowelsSymbols(inputString, &digitCount,
&vowelCount, &symbolCount);

printf("Digits: %d\n", digitCount);


printf("Vowels: %d\n", vowelCount);
printf("Symbols: %d\n", symbolCount);

return 0;
}
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

OUTPUT:
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

Practical-3
AIM: Program to check validation of User Name and
Password in C.
CODE: #include <stdio.h>
#include <string.h>
#include <ctype.h>

int isValidUsername(const char *username) {

if (strlen(username) == 0) {
return 0;
}

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


if (!isalnum(username[i])) {
return 0;
}
}

return 1;
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

int isValidPassword(const char *password) {

if (strlen(password) < 8) {
return 0;
}

int hasDigit = 0;
for (int i = 0; password[i] != '\0'; i++) {
if (isdigit(password[i])) {
hasDigit = 1;
break;
}
}

int hasUppercase = 0;
for (int i = 0; password[i] != '\0'; i++) {
if (isupper(password[i])) {
hasUppercase = 1;
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

break;
}
}

if (hasDigit && hasUppercase) {


return 1;
} else {
return 0;
}
}

int main() {
char username[50];
char password[50];

printf("Enter username: ");


scanf("%s", username);

if (!isValidUsername(username)) {
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

printf("Invalid username. Username must contain only


alphanumeric characters and cannot be empty.\n");
return 1;
}

printf("Enter password: ");


scanf("%s", password);

if (!isValidPassword(password)) {
printf("Invalid password. Password must be at least 8
characters long and contain at least one digit and one
uppercase letter.\n");
return 1;
}

printf("Username and password are valid. Registration


successful!\n");

return 0;
}
Faculty of Engineering & Technology

Subject-Name: Compiler Design

Subject-Code:203105361

B.Tech CSE Year: 3RD Semester: 6TH

OUTPUT:

You might also like