0% found this document useful (0 votes)
11 views17 pages

Smita CD

The document outlines practical programming assignments for a Compiler Design course, including the implementation of a Lexical Analyzer, counting digits, vowels, and symbols, validating usernames and passwords, and implementing predictive and recursive descent parsing. Each practical includes source code examples in C and aims to teach fundamental concepts of compiler design. The assignments are authored by a student named Smita Singh, with specific enrollment details provided.

Uploaded by

smitasingh4610
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)
11 views17 pages

Smita CD

The document outlines practical programming assignments for a Compiler Design course, including the implementation of a Lexical Analyzer, counting digits, vowels, and symbols, validating usernames and passwords, and implementing predictive and recursive descent parsing. Each practical includes source code examples in C and aims to teach fundamental concepts of compiler design. The assignments are authored by a student named Smita Singh, with specific enrollment details provided.

Uploaded by

smitasingh4610
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/ 17

Faculty of Engineering & Technology

Subject: - Complier Design (303105350)


B. Tech AI & AI-DS 6th Semester

Practical – 01
AIM: - Program to implement Lexical Analyzer.
What is Lexical Analyzer?
 A lexical analyzer, also known as a lexer or scanner.
 It is a component of a compiler that processes input text (usually source code) and
converts it into a sequence of tokens.
 Each token represents a basic element of the language, such as keywords, identifiers,
operators, and literals.
Source Code: -
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
int isKeyword(char *str) {
const char *keywords[] = {
"int", "float", "if", "else", "while", "for", "return", "void", "main"
};
for (int i = 0; i < 9; i++) {
if (strcmp(str, keywords[i]) == 0) return 1;
}
return 0;
}
int isOperator(char c) {
char operators[] = "+-*/%=<>";
for (int i = 0; operators[i] != '\0'; i++) {
if (c == operators[i])
return 1;
}
return 0;
}
void lexicalAnalyzer(char *code) {

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

int i = 0;
char token[MAX];
printf("Tokens:\n");
while (code[i] != '\0') {
if (isspace(code[i])) {
i++;
continue;
}
if (isalpha(code[i])) {
int j = 0;
while (isalnum(code[i])) {
token[j++] = code[i++];
}
token[j] = '\0';
if (isKeyword(token)) printf("Keyword: %s\n", token);
else
printf("Identifier: %s\n", token);
}
else if (isdigit(code[i]))
{
int j = 0;
while (isdigit(code[i])) { token[j++] = code[i++];
}
token[j] = '\0';
printf("Number: %s\n", token);
}
else if (isOperator(code[i])) {
printf("Operator: %c\n", code[i]); i++;
}
else {

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

printf("Delimiter: %c\n", code[i]); i++;


}
}
}
int main() {
char code[MAX];
printf("Enter a code snippet: ");
fgets(code, MAX, stdin);
lexicalAnalyzer(code);
return 0;
}
OUTPUT: -

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

Practical – 02
AIM: - Program to count digits, vowels and symbols in C.
SOURCE CODE: -
#include <stdio.h>

#include <ctype.h>

int main() {

char str[100];

int digits = 0, vowels = 0, symbols = 0;

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

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

char ch = str[i];

if (isdigit(ch))

digits++;

else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch
== 'U')

vowels++;

else if (!isalnum(ch) && !isspace(ch))

symbols++;

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

printf("Vowels: %d\n", vowels);

printf("Symbols: %d\n", symbols);

return 0;

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

OUTPUT: -

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

Practical – 03
AIM: - Program to check validation of User Name and Password in C.
SOURCE CODE: -
#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define USERNAME_MIN_LENGTH 3

#define USERNAME_MAX_LENGTH 15

#define PASSWORD_MIN_LENGTH 8

int isValidUsername(const char *username) {

int length = strlen(username);

if (length < USERNAME_MIN_LENGTH || length > USERNAME_MAX_LENGTH) {

return 0;

if (isdigit(username[0])) {

return 0;

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

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

return 0;

return 1;

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

int isValidPassword(const char *password) {

int length = strlen(password);

if (length < PASSWORD_MIN_LENGTH) {

return 0;

int hasUpper = 0, hasLower = 0, hasDigit = 0, hasSpecial = 0;

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

if (isupper(password[i])) {

hasUpper = 1;

} else if (islower(password[i])) {

hasLower = 1;

} else if (isdigit(password[i])) {

hasDigit = 1;

} else if (ispunct(password[i])) {

hasSpecial = 1;

return hasUpper && hasLower && hasDigit && hasSpecial;

int main() {

char username[20];

char password[20];

printf("Enter username: ");

scanf("%s", username);

if (isValidUsername(username)) {

printf("Username is valid.\n");

} else {

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

printf("Invalid username. It must be 3-15 characters long, start with a letter, and can only contain letters,
digits, and underscores.\n");

return 1;

printf("Enter password: ");

scanf("%s", password);

if (isValidPassword(password)) {

printf("Password is valid.\n");

} else {

printf("Invalid password. It must be at least 8 characters long and contain at least one uppercase letter, one
lowercase letter, one digit, and one special character.\n");

return 1;

printf("Username and password are valid.\n");

return 0;

OUTPUT: -

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

Practical – 04
AIM: - Program to implement Predictive Parsing LL (1) in C.
Source Code: -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
#define EMPTY "e" // For epsilon production
char table[10][10][MAX];
char nonTerminals[10];
char terminals[10];
int numNonTerminals = 0, numTerminals = 0;
char stack[MAX];
int top = -1;
void push(char *str) {
for (int i = strlen(str) - 1; i >= 0; i--) {
stack[++top] = str[i];
}
}
void pop() {
if (top >= 0) {
top--;
}
}
char peek() {
return top >= 0 ? stack[top] : '\0';
Name: - Smita Singh
Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

}
int getSymbolIndex(char *array, char symbol) {
for (int i = 0; i < strlen(array); i++) {
if (array[i] == symbol) {
return i;
}
}
return -1;
}
void displayStack() {
printf("Stack: ");
for (int i = 0; i <= top; i++) {
printf("%c", stack[i]);
}
printf("\n");
}
void predictiveParsing(char *input) {
int ptr = 0;
char currentSymbol; push("$");
push(&nonTerminals[0]); // Push the start symbol
printf("Parsing Steps:\n");
while (peek() != '$') {
currentSymbol = peek();
if (currentSymbol == input[ptr]) {
printf("Matched: %c\n", currentSymbol);
pop();
ptr++;
}
else if (strchr(nonTerminals, currentSymbol)) {
int ntIndex = getSymbolIndex(nonTerminals, currentSymbol);

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

int tIndex = getSymbolIndex(terminals, input[ptr]);


if (tIndex == -1) {
printf("Error: Unexpected symbol '%c'\n", input[ptr]); return;
}
char *production = table[ntIndex][tIndex];
if (strcmp(production, "") == 0) {
printf("Error: No rule for '%c' with '%c'\n", currentSymbol, input[ptr]);
return;
}
printf("Production: %c -> %s\n", currentSymbol, production);
pop();
if (strcmp(production, EMPTY) != 0) {
push(production);
}
}
else {
printf("Error: Unexpected symbol '%c'\n", currentSymbol);
return;
}
displayStack(); // Optional: For debugging purposes
}
if (input[ptr] == '$') {
printf("Input accepted.\n");
}
else {
printf("Error: Input not fully consumed.\n");
}
}
int main() {
int i, j;

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

char input[MAX];
printf("Enter number of non-terminals: ");
scanf("%d", &numNonTerminals);
printf("Enter non-terminals (single character each): ");
for (i = 0; i < numNonTerminals; i++) {
scanf(" %c", &nonTerminals[i]);
}
printf("Enter number of terminals: ");
scanf("%d", &numTerminals);
printf("Enter terminals (single character each): ");
for (i = 0; i < numTerminals; i++) {
scanf(" %c", &terminals[i]);
}
terminals[numTerminals++] = '$';
for (i = 0; i < numNonTerminals; i++) {
for (j = 0; j < numTerminals; j++) {
strcpy(table[i][j], "");
}
}
printf("Enter parsing table (use e for epsilon):\n");
for (i = 0; i < numNonTerminals; i++) {
for (j = 0; j < numTerminals; j++) {
printf("Production for %c with %c: ", nonTerminals[i], terminals[j]); scanf("%s",
table[i][j]);
}
}
printf("Enter the input string (end with $): ");
scanf("%s", input);
predictiveParsing(input);
return 0;

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

OUTPUT: -

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

Practical – 05
AIM: -Program to implement Recursive Descent Parsing in C.
Source Code: -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX 100

char input[MAX];
int pos = 0;

void E();
void T();
void E_prime();
void T_prime();
void F();

void error() {
printf("Error in parsing!\n");
exit(1);
}

void match(char expected) {


if (input[pos] == expected) {
printf("Matched: %c\n", input[pos]);
pos++;
} else {
Name: - Smita Singh
Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

error();
}
}

void E() {
printf("Applying Production: E -> T E'\n");
T();
E_prime();
}

void E_prime() {
if (input[pos] == '+') {
printf("Applying Production: E' -> + T E'\n");
match('+');
T();
E_prime();
} else {
printf("Applying Production: E' -> epsilon\n");
}
}

void T() {
printf("Applying Production: T -> F T'\n");
F();
T_prime();
}

void T_prime() {
if (input[pos] == '*') {
printf("Applying Production: T' -> * F T'\n");

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

match('*');
F();
T_prime();
} else {
printf("Applying Production: T' -> epsilon\n");
}
}

void F() {
if (isalnum(input[pos])) {
printf("Applying Production: F -> id\n");
match(input[pos]);
} else if (input[pos] == '(') {
printf("Applying Production: F -> ( E )\n");
match('(');
E();
match(')');
} else {
error();
}
}

int main() {
printf("Enter the input string (end with $): ");
scanf("%s", input);

if (input[strlen(input) - 1] != '$') {
printf("Error: Input string must end with '$'.\n");
return 1;
}

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9
Faculty of Engineering & Technology
Subject: - Complier Design (303105350)
B. Tech AI & AI-DS 6th Semester

printf("Parsing Steps:\n");
E();

if (input[pos] == '$') {
printf("Input accepted.\n");
} else {
printf("Parsing failed! Extra characters found.\n");
}

return 0;
}
OUTPUT: -

Name: - Smita Singh


Enrollment: - 2203031241233 Page |
Division: - 6AI9

You might also like