0% found this document useful (0 votes)
16 views16 pages

CD Experiment 1 to 10-1

Sure sir please

Uploaded by

phoenixdark280
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)
16 views16 pages

CD Experiment 1 to 10-1

Sure sir please

Uploaded by

phoenixdark280
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/ 16

Experiment – 1

Aim:- Program to identify whether given String is Keyword or not.


Program :-
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isKeyword(const string& str) {
vector<string> keywords = {
"auto", "break", "case", "char", "const", "continue", "default", "do",
"double", "else", "enum", "extern", "float", "for", "goto", "if",
"inline", "int", "long", "register", "restrict", "return", "short",
"signed", "sizeof", "static", "struct", "switch", "typedef", "union",
};
for (const auto& keyword : keywords) {
if (str == keyword) {
return true;
}
}
return false;
}

int main() {
string str;
cout << "Enter a string: ";
cin >> str;
if (isKeyword(str)) {
cout << str << " is a C++ keyword.\n";
} else {
cout << str << " is not a C++ keyword.\n";
}
return 0;
}
Output :-
Enter a string: break
break is a C++ keyword.
Experiment – 2
Aim :- Program to identify whether a given Line is Comment or not
Program:-
#include <iostream>
#include <string>
using namespace std;
bool isComment(const string& line) {
if (line.substr(0, 2) == "//") {
return true; // Single-line comment
}
if (line.substr(0, 2) == "/*" && line.substr(line.size() - 2, 2) == "*/") {
return true; // Multi-line comment }
return false;
}
int main() {
string line;
cout << "Enter a line: ";
getline(cin, line);
if (isComment(line)) {
cout << "The line is a comment.\n";
} else {
cout << "The line is not a comment.\n";
}
return 0;
}
Output:-
Enter a line: //
The line is a comment.
Experiment – 3
Aim :- Program to validate an Identifier
Program :-
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
bool isValidIdentifier(const string& str) {
if (!isalpha(str[0]) && str[0] != '_') {
return false; // Must start with an alphabet or underscore
}
for (size_t i = 1; i < str.size(); i++) {
if (!isalnum(str[i]) && str[i] != '_') {
return false; // Must contain only alphanumeric characters or
underscores
}
}
return true;
}
int main() {
string str;
cout << "Enter an identifier: ";
cin >> str;
if (isValidIdentifier(str)) {
cout << str << " is a valid identifier.\n";
} else {
cout << str << " is not a valid identifier.\n";
}
return 0;
}
Output:-
Enter an identifier: my_variable
my_variable is a valid identifier.
Experiment – 4
Aim :- Program to Count total number of Words, Blank spaces in a String
Program:-
#include <iostream>
#include <string>
#include <ctype>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
int wordCount = 0, spaceCount = 0;
bool inWord = false;
for (char ch : str) {
if (isspace(ch)) {
spaceCount++;
inWord = false;
} else if (!inWord) {
wordCount++;
inWord = true;
}
}
cout << "Total words: " << wordCount << endl;
cout << "Total blank spaces: " << spaceCount << endl;
return 0;
}
Output:-
Enter a string: hello world
Total words: 2
Total blank spaces: 1
Experiment – 5
Aim :- From a given vertex in a weighted connected graph, find shortest
paths to other vertices using Dijkstra's algorithm.
Program :-
#include <iostream>
#include <string>
using namespace std;
int main() {
string expression;
cout << "Enter an expression: ";
getline(cin, expression);
int operatorCount = 0;
for (char ch : expression) {
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {
operatorCount++;
}
}
cout << "Total number of operators: " << operatorCount << endl;
return 0;
}
Output:-
Enter an expression: 5 + 3 * 2 - 7 / 4
Total number of operators: 4
Experiment – 6
Aim :- Program to Count the Total Number of Keywords in a String.
Program:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// List of C keywords
const char *keywords[] = {
"auto", "break", "case", "char", "const", "continue", "default", "do",
"double", "else", "enum", "extern", "float", "for", "goto", "if",
"int", "long", "register", "return", "short", "signed", "sizeof",
"static", "struct", "switch", "typedef", "union", "unsigned",
"void", "volatile", "while"
};
const int keywordCount = sizeof(keywords) / sizeof(keywords[0]);
// Function to check if a word is a C keyword
int isKeyword(const char *word) {
for (int i = 0; i < keywordCount; i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1; // It's a keyword
}
}
return 0; // Not a keyword
}
int main() {
char str[200];
char word[50];
int keywordCounter = 0;
int j = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
word[j++] = str[i];
} else if (j > 0) {
word[j] = '\0'; // Null-terminate the word
if (isKeyword(word)) {
keywordCounter++;
}
j = 0; // Reset for the next word
}
}
// Check the last word
if (j > 0) {
word[j] = '\0';
if (isKeyword(word)) {
keywordCounter++;
}
}
printf("Total number of keywords: %d\n", keywordCounter);
return 0;
}
Output:-
Enter a string: if, int, float a =0;
Total number of keywords: 3
Experiment – 7
Aim :- Program to Count total number of Vowels and Consonants in a
String
Program:-
#include <stdio.h>
int main() {
char str[100];
int vowels = 0, consonants = 0;
printf("Enter a string: ");
gets(str); // Read the input string
for (int i = 0; str[i] != '\0'; i++) {
// Check for vowels
if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' ||
str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
vowels++;
}
// Check for consonants
else if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
consonants++;
}
}
printf("Total number of vowels: %d\n", vowels);
printf("Total number of consonants: %d\n", consonants);
return 0;
}
Output:-
Enter a string: Hello World
Total number of vowels: 3
Total number of consonants: 7
Experiment – 8
Aim :- Program to find FIRST of any Grammar.
Program:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char production[10][10];
int n;
void findFIRST(char c, char* result) {
for (int i = 0; i < n; i++) {
if (production[i][0] == c) {
if (!isupper(production[i][2])) {
strncat(result, &production[i][2], 1);
} else {
findFIRST(production[i][2], result);
}
}
}
}
int main() {
char result[10] = "";
char c;
printf("Enter number of productions: ");
scanf("%d", &n);
printf("Enter productions (e.g., E=T+F):\n");
for (int i = 0; i < n; i++) {
scanf("%s", production[i]);
}
printf("Enter the symbol to find FIRST: ");
scanf(" %c", &c);
findFIRST(c, result);
printf("FIRST(%c) = { %s }\n", c, result);
return 0;
}
Output:-
How many number of productions? : 4
Enter production Number 1: E=TD
Enter production Number 2: D=+TD
Enter production Number 3: D=$
Enter production Number 4: T=a

Find the FIRST of: E


FIRST(E) = { T }
Press 'y' to continue: n
Experiment – 9
Aim :- Program to find FOLLOW of any Grammar.
Program:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char production[10][10];
int n;
char followResult[10];
int followCount = 0;
void addToResult(char c) {
if (!strchr(followResult, c)) {
followResult[followCount++] = c;
}
}
void findFOLLOW(char c) {
if (production[0][0] == c) addToResult('$');
for (int i = 0; i < n; i++) {
for (int j = 2; production[i][j] != '\0'; j++) {
if (production[i][j] == c) {
if (production[i][j + 1] != '\0') {
addToResult(production[i][j + 1]);
} else if (production[i][0] != c) {
findFOLLOW(production[i][0]);
}
}
}
}
}
int main() {
char c;
printf("Enter number of productions: ");
scanf("%d", &n);
printf("Enter productions (e.g., E=T+F):\n");
for (int i = 0; i < n; i++) {
scanf("%s", production[i]);
}
printf("Enter the symbol to find FOLLOW: ");
scanf(" %c", &c);
findFOLLOW(c);
printf("FOLLOW(%c) = { %s }\n", c, followResult);
return 0;
}
Output:-
Enter the number of productions: 4
Enter the productions (e.g., S->AB, A->a, B->b):
S->AB
A->a
B->b
A->E
FOLLOW(S) = { $ }
FOLLOW(A) = { b }
FOLLOW(B) = { $ }
Experiment – 10
Aim :- Program to perform Shift Reduce Parsing for any grammar..
Program:-
#include <stdio.h>
#include <string.h>
char stack[100], input[100], production[10][10];
int top = -1, n;
void push(char c) {
stack[++top] = c;
}
void reduce() {
for (int i = 0; i < n; i++) {
if (strstr(stack, production[i] + 3)) {
top -= strlen(production[i] + 3) - 1;
stack[top] = production[i][0];
}
}
}
int main() {
printf("Enter number of productions: ");
scanf("%d", &n);
printf("Enter productions (e.g., E=TX):\n");
for (int i = 0; i < n; i++) {
scanf("%s", production[i]);
}
printf("Enter input string (e.g., a$): ");
scanf("%s", input);
printf("\nStack\tInput\tAction\n");
push('$');
char *ip = input;
while (1) {
printf("%s\t%s\t", stack, ip);
if (stack[top] == '$' && *ip == '$') {
printf("Accept\n");
break;
}
push(*ip++);
printf("Shift\n");
reduce();
}
return 0;
}
Output:-
GRAMMAR is –
E->2E2
E->3E3
E->4
Enter the input string (maximum length 15): 32432
stack input action
$ 32432$ SHIFT
$3 2432$ SHIFT
$32 432$ SHIFT
$324 32$ REDUCE TO E -> 4
$32E 32$ SHIFT
$32E3 2$ SHIFT
$32E32 $ Reject

You might also like