0% found this document useful (0 votes)
23 views13 pages

CD Labmanual

The document outlines practical assignments for a Compiler Designing course, including the implementation of a lexical analyzer, a program to count characters in a string, and a basic username and password validation system. Each practical includes the aim, objectives, and corresponding C code. The document also features example outputs for each program.

Uploaded by

kandrakarthik15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views13 pages

CD Labmanual

The document outlines practical assignments for a Compiler Designing course, including the implementation of a lexical analyzer, a program to count characters in a string, and a basic username and password validation system. Each practical includes the aim, objectives, and corresponding C code. The document also features example outputs for each program.

Uploaded by

kandrakarthik15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Faculty of engineering and technology

Subject name: Compiler Designing .


Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

PRACTICAL:1

Aim : Program to Implementation lexical Analyzer in c

OBJECTIVE: A lexical analyzer, also known as a lexical or scanners a component of a


compiler or interpreter that breaks down the source code into token, remove whitespace
and commas, manage a symbol table, handles errors, and user regular experiment and
finite automation to define token patterns . It is the first phase in the compilation process,
providing and a stream of token as input for subsequent analyses by the pares and other
compiler component

CODE:

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100
bool isDelimiter(char chr)
{
return (chr == ' ' || chr == '+' || chr == '-'
|| chr == '*' || chr == '/' || chr == ','
|| chr == ';' || chr == '%' || chr == '>'
|| chr == '<' || chr == '=' || chr == '('
|| chr == ')' || chr == '[' || chr == ']'
|| chr == '{' || chr == '}');
}
bool isOperator(char chr)
{
return (chr == '+' || chr == '-' || chr == '*'
|| chr == '/' || chr == '>' || chr == '<'
|| chr == '=');
}
bool isValidIdentifier(char* str)
{
return (str[0] != '0' && str[0] != '1' && str[0] != '2'
&& str[0] != '3' && str[0] != '4'
&& str[0] != '5' && str[0] != '6'
&& str[0] != '7' && str[0] != '8'
&& str[0] != '9' && !isDelimiter(str[0]));
}
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

bool isKeyword(char* str)


{
const char* keywords[]
= { "auto", "break", "case", "char",
"const", "continue", "default", "do",
"double", "else", "enum", "extern",
" oat", "for", "goto", "if",
"int", "long", "register", "return",
"short", "signed", "sizeof", "static",
"struct", "switch", "typedef", "union",
"unsigned", "void", "volatile", "while" };
for (int i = 0;
i < sizeof(keywords) / sizeof(keywords[0]); i++) {
if (strcmp(str, keywords[i]) == 0) {
return true;
}
}
return false;
}

bool isInteger(char* str)


{
if (str == NULL || *str == '\0') {
return false;
}
int i = 0;
while (isdigit(str[i])) {
i++;
}
return str[i] == '\0';
}
char* getSubstring(char* str, int start, int end)
{
int length = strlen(str);
int subLength = end - start + 1;
char* subStr
= (char*)malloc((subLength + 1) * sizeof(char));
strncpy(subStr, str + start, subLength);
subStr[subLength] = '\0';
return subStr;
}

int lexicalAnalyzer(char* input)


{
int left = 0, right = 0;
int len = strlen(input);

while (right <= len && left <= right) {


if (!isDelimiter(input[right]))
right++;

if (isDelimiter(input[right]) && left == right) {


if (isOperator(input[right]))
printf("Token: Operator, Value: %c\n",
input[right]);
fl
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

right++;
left = right;
}
else if (isDelimiter(input[right]) && left != right
|| (right == len && left != right)) {
char* subStr
= getSubstring(input, left, right - 1);

if (isKeyword(subStr))
printf("Token: Keyword, Value: %s\n",
subStr);

else if (isInteger(subStr))
printf("Token: Integer, Value: %s\n",
subStr);

else if (isValidIdenti er(subStr)


&& !isDelimiter(input[right - 1]))
printf("Token: Identi er, Value: %s\n",
subStr);

else if (!isValidIdenti er(subStr)


&& !isDelimiter(input[right - 1]))
printf("Token: Unidenti ed, Value: %s\n",
subStr);
left = right;
}
}
return 0;
}

int main()
{
char lex_input[MAX_LENGTH] = "int a = b + c";
printf("For Expression \"%s\":\n", lex_input);
lexicalAnalyzer(lex_input);
printf(" \n");
char lex_input01[MAX_LENGTH]
= "int x=ab+bc+30+switch+ 0y ";
printf("For Expression \"%s\":\n", lex_input01);
lexicalAnalyzer(lex_input01);
return (0);
}
fi
fi
fi
fi
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

OUTPUT:
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

PRACTICAL: 02
AIM:Write a program to count Digits, Vowel, Symbols from the given sting.

Objective:-this code counts vowels ,consonants, digits, spaces, symbols, and


special-
tokens in the given input string.
CODE:

#include <ctype.h>
#include <stdio.h>
int main() {

char line[150];
int vowels, consonant, digit, space;

vowels = consonant = digit = space = 0;

printf("Enter a line of string: ");


fgets(line, sizeof(line), stdin);

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

line[i] = tolower(line[i]);

if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||


line[i] == 'o' || line[i] == 'u') {

++vowels;
}

else if ((line[i] >= 'a' && line[i] <= 'z')) {


++consonant;
}

else if (line[i] >= '0' && line[i] <= '9') {


++digit;
}

}
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

else if (line[i] == ' ') {


++space;
}
}

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


printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);

return 0;
}

OUTPUT:
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

PRACTICAL: 03
AIM: Program to check validation of User Name and Password in C.

Objective:- This code implements a basic username and password


Authentication system to validate user credentials.
CODE:

#include <stdio.h>
#include <string.h>

int main() {
char username[20], password[20];

printf("Enter Username: ");


scanf("%s", username);
printf("Enter Password: ");
scanf("%s", password);

// Basic Validation Checks


if (strlen(username) < 5 || strlen(password) < 8) {
printf("Invalid credentials: Username must be at least 5 characters and password
must be at least 8 characters.\n");
return 1;
}

// Check against a prede ned user (replace with database access in real
applications)
if (strcmp(username, "admin") == 0 && strcmp(password, "password123") == 0) {
printf("Login successful!\n");
} else {
printf("Invalid username or password.\n");
}

return 0;
}`
fi
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

Output:
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .
Faculty of engineering and technology
Subject name: Compiler Designing .
Student Name: Maanas S Vihaayas .
ERP: 2203031240744 .
Subject Code : 303105350 .

You might also like