0% found this document useful (0 votes)
56 views28 pages

Toc Prac

toc practical

Uploaded by

psoni20112003
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)
56 views28 pages

Toc Prac

toc practical

Uploaded by

psoni20112003
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/ 28

THEORY OF COMPUTATION

PRACTICAL FILE
NAME : POOJA SONI
ROLL NO. 22/CS/48
COURSE : B.SC.(H) COMPUTER SCIENCE
SUBMITTED TO : PROF. HARENDRA PRATAP SINGH

Question 1 ) Design a Finite Automata (FA) that accepts all strings over S={0, 1} having three consecutive 1's as
a substring. Write a program to simulate this FA.

#include <iostream>
#include <string>
using namespace std;

void State0(const string& w, int i);


void State1(const string& w, int i);

void State2(const string& w, int i);


void State3(const string& w, int i);
int main() {
string w;
cout << "Enter a string of '0's and '1's: ";
cin >> w;
State0(w, 0);
return 0;

void State0(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == '0') {
State0(w, i + 1); // Stay in State 0
} else if (w[i] == '1') {
State1(w, i + 1); // Transition to State 1
}
}

void State1(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == '0') {
State0(w, i + 1); // Transition back to State 0
} else if (w[i] == '1') {
State2(w, i + 1); // Transition to State 2
}
}

void State2(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == '0') {
State0(w, i + 1); // Transition back to State 0
} else if (w[i] == '1') {
State3(w, i + 1); // Transition to State 3 (accepting state)
}
}
void State3(const string& w, int i) {
if (i == w.length()) {
cout << "String is accepted" << endl; // End in accepting state
return;
}

// Stay in State 3 regardless of the input


State3(w, i + 1);
}

Output :
Enter a string of '0's and '1's: 01101000
String is rejected

Question 2) Design a Finite Automata (FA) that accepts all strings over S={0, 1} having either exactly two 1's or
exactly three 1's, not more nor less. Write a program to simulate this FA.

Code :
#include <iostream>
#include <string>
using namespace std;

void State0(const string& w, int i);


void State1(const string& w, int i);
void State2(const string& w, int i);
void State3(const string& w, int i);
void State4(const string& w, int i);

int main() {
string w;
cout << "Enter a string of '0's and '1's: ";
cin >> w;
State0(w, 0);
return 0;
}

void State0(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == '0') {
State0(w, i + 1); // Stay in State 0
} else if (w[i] == '1') {
State1(w, i + 1); // Transition to State 1
}
}

void State1(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == '0') {
State1(w, i + 1); // Stay in State 1
} else if (w[i] == '1') {
State2(w, i + 1); // Transition to State 2
}
}

void State2(const string& w, int i) {


if (i == w.length()) {
cout << "String is accepted" << endl; // End in accepting state
return;
}

if (w[i] == '0') {
State2(w, i + 1); // Stay in State 2
} else if (w[i] == '1') {
State3(w, i + 1); // Transition to State 3
}
}

void State3(const string& w, int i) {


if (i == w.length()) {
cout << "String is accepted" << endl; // End in accepting state
return;
}

if (w[i] == '0') {
State3(w, i + 1); // Stay in State 3
} else if (w[i] == '1') {
State4(w, i + 1); // Transition to dead state
}
}

void State4(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

// Remain in dead state


State4(w, i + 1);
}

Output :
Enter a string of '0's and '1's: 0111
String is accepted.

Question 3 )Design a Finite Automata (FA) that accepts language L1, over S={a, b}, comprising of all strings (of
length 4 or more) having first two characters same as the last two. Write a program to simulate this
FA.

Code :
#include <iostream>
#include <string>
using namespace std;

void checkString(const string& w) {


if (w.length() < 4) {
cout << "String is rejected" << endl; // Length less than 4
return;
}

// Get first two characters


char first1 = w[0];
char first2 = w[1];

// Get last two characters


char last1 = w[w.length() - 1];
char last2 = w[w.length() - 2];

// Check if the first two match the last two


if (first1 == last1 && first2 == last2) {
cout << "String is accepted" << endl;
} else {
cout << "String is rejected" << endl;
}
}

int main() {
string w;
cout << "Enter a string of 'a's and 'b's: ";
cin >> w;

checkString(w);
return 0;
}
Output :
Enter a string of 'a's and 'b's: abaaabbabab
String is rejected
Enter a string of 'a's and 'b's: baaaababbbabab
String is accepted

Question 4). Design a Finite Automata (FA) that accepts language L2, over S= {a, b} where L2= a(a+b)*b. Write
a program to simulate this FA.

Code :
#include <iostream>
#include <string>
using namespace std;

void State0(const string& w, int i);


void State1(const string& w, int i);
void State2(const string& w, int i);

int main() {
string w;
cout << "Enter a string of 'a's and 'b's: ";
cin >> w;
State0(w, 0);
return 0;
}

void State0(const string& w, int i) {


if (i >= w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == 'a') {
State1(w, i + 1); // Move to State 1
} else {
cout << "String is rejected" << endl; // Can't start with 'b'
}
}

void State1(const string& w, int i) {


if (i >= w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == 'a') {
State1(w, i + 1); // Stay in State 1
} else if (w[i] == 'b') {
State2(w, i + 1); // Move to State 2
}
}

void State2(const string& w, int i) {


if (i >= w.length()) {
cout << "String is accepted" << endl; // End in accepting state
return;
}

if (w[i] == 'a') {
State1(w, i + 1); // Move to State 1
} else if (w[i] == 'b') {
State2(w, i + 1); // Stay in State 2
}
}

Output :
Enter a string of 'a's and 'b's: aaabbbbababb
String is accepted
Enter a string of 'a's and 'b's: ab
String is accepted
Question 5.) Design a Finite Automata (FA) that accepts language EVEN-EVEN over S={a, b}. Write a program
to simulate this FA

Code :
#include <iostream>
#include <string>
using namespace std;

void State0(const string& w, int i);


void State1(const string& w, int i);
void State2(const string& w, int i);
void State3(const string& w, int i);

int main() {
string w;
cout << "Enter a string of 'a's and 'b's: ";
cin >> w;

State0(w, 0);
return 0;
}

void State0(const string& w, int i) {


if (i == w.length()) {
cout << "String is accepted" << endl; // End in accepting state
return;
}

if (w[i] == 'a') {
State1(w, i + 1); // Move to State 1
} else if (w[i] == 'b') {
State2(w, i + 1); // Move to State 2
} else {
cout << "String is rejected" << endl; // Invalid character
}
}

void State1(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == 'a') {
State0(w, i + 1); // Move to State 0
} else if (w[i] == 'b') {
State3(w, i + 1); // Move to State 3
} else {
cout << "String is rejected" << endl; // Invalid character
}
}

void State2(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}

if (w[i] == 'a') {
State3(w, i + 1); // Move to State 3
} else if (w[i] == 'b') {
State0(w, i + 1); // Move to State 0
} else {
cout << "String is rejected" << endl; // Invalid character
}
}

void State3(const string& w, int i) {


if (i == w.length()) {
cout << "String is rejected" << endl; // End in non-accepting state
return;
}
if (w[i] == 'a') {
State2(w, i + 1); // Move to State 2
} else if (w[i] == 'b') {
State1(w, i + 1); // Move to State 1
} else {
cout << "String is rejected" << endl; // Invalid character
}
}

Output :
Enter a string of 'a's and 'b's: aabaaa
String is rejected
Enter a string of 'a's and 'b's: aabbaa
String is accepted

Question 6.) Write a program to simulate an FA that accepts


a. Union of the languages L1 and L2
b. Intersection of the languages L1 and L2
c. Language L1 L2 (concatenation)

code :
#include <iostream>
#include <string>
#include <regex>

using namespace std;

// Function to check if a string is in L1 based on the provided pattern


bool isInL1(const string& w, const string& pattern) {
return regex_match(w, regex(pattern)); // Match against user-defined pattern for L1
}

// Function to check if a string is in L2 based on the provided pattern


bool isInL2(const string& w, const string& pattern) {
return regex_match(w, regex(pattern)); // Match against user-defined pattern for L2
}

// Function to check union of L1 and L2


bool unionLanguages(const string& w, const string& pattern1, const string& pattern2) {
return isInL1(w, pattern1) || isInL2(w, pattern2); // Accepted if in L1 or L2
}

// Function to check intersection of L1 and L2


bool intersectionLanguages(const string& w, const string& pattern1, const string& pattern2) {
return isInL1(w, pattern1) && isInL2(w, pattern2); // Accepted if in both L1 and L2
}
// Function to check concatenation of L1 and L2
bool concatenationLanguages(const string& w, const string& pattern1, const string& pattern2) {
for (size_t i = 1; i < w.length(); i++) {
string part1 = w.substr(0, i);
string part2 = w.substr(i);
if (isInL1(part1, pattern1) && isInL2(part2, pattern2)) {
return true; // Accepted if part1 is in L1 and part2 is in L2
}
}
return false;
}

int main() {
string pattern1, pattern2, w;

// Input for language definitions


cout << "Enter a regular expression for Language L1: ";
getline(cin, pattern1);

cout << "Enter a regular expression for Language L2: ";


getline(cin, pattern2);

cout << "Enter a string to test: ";


getline(cin, w);
// Check operations
cout << "Union of L1 and L2: " << (unionLanguages(w, pattern1, pattern2) ? "Accepted" : "Rejected") << endl;
cout << "Intersection of L1 and L2: " << (intersectionLanguages(w, pattern1, pattern2) ? "Accepted" : "Rejected") <<
endl;
cout << "Concatenation of L1 and L2: " << (concatenationLanguages(w, pattern1, pattern2) ? "Accepted" :
"Rejected") << endl;

return 0;
}

Output :
Enter a regular expression for Language L1: (ab)*
Enter a regular expression for Language L2: (a|ab)*
Enter a string to test: abab
Union of L1 and L2: Accepted
Intersection of L1 and L2: Accepted
Concatenation of L1 and L2: Accepted

Enter a regular expression for Language L1: ab*


Enter a regular expression for Language L2: (a|ab)*
Enter a string to test: aa
Union of L1 and L2: Accepted
Intersection of L1 and L2: Rejected
Concatenation of L1 and L2: Accepted
Question 7 ) Design a PDA and write a program for simulating the machine which accepts the language
{anbn where n>0, S= {a, b}}.

Code :
#include <iostream>
#include <stack>
#include <string>
using namespace std;

bool isAccepted(const string& input) {


stack<char> pdaStack;

// Start in state q0
int state = 0;

for (char c : input) {


if (state == 0) { // q0 state
if (c == 'a') {
pdaStack.push('A'); // Push 'A' for each 'a'
} else if (c == 'b') {
if (pdaStack.empty()) {
return false; // No matching 'a' for 'b'
}
pdaStack.pop(); // Pop for each 'b'
state = 1; // Transition to q1
} else {
return false; // Invalid character
}
} else if (state == 1) { // q1 state
if (c == 'b') {
if (pdaStack.empty()) {
return false; // No matching 'a' for 'b'
}
pdaStack.pop(); // Pop for each 'b'
} else {
return false; // Invalid character
}
}
}

// Accepted if stack is empty and at least one 'a' was read


return pdaStack.empty() && input.length() > 0 && input[0] == 'a';
}

int main() {
string input;
cout << "Enter a string to test (only a's and b's): ";
getline(cin, input);

if (isAccepted(input)) {
cout << "Accepted\n";
} else {
cout << "Rejected\n";
}

return 0;
}

Output :
Enter a string to test (only a's and b's): aaabbb
Accepted

Question 8). Design a PDA and write a program for simulating the machine which accepts the language {wXwr| w is
any string over S={a, b} and wr is reverse of that string and X is a special symbol }.
Code :

#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isAccepted(const string& input) {
stack<char> pdaStack;

// Start in state q0
int state = 0;

for (char c : input) {


if (state == 0) { // q0 state
if (c == 'a' || c == 'b') {
pdaStack.push(c); // Push 'a' or 'b' onto the stack
} else if (c == 'X') {
if (pdaStack.empty()) {
return false; // X encountered but no characters in stack
}
state = 1; // Transition to q1
} else {
return false; // Invalid character
}
} else if (state == 1) { // q1 state
if (c == 'a' || c == 'b') {
if (pdaStack.empty()) {
return false; // More characters read than pushed
}
char topChar = pdaStack.top();
if (topChar == c) {
pdaStack.pop(); // Pop if it matches
} else {
return false; // Mismatch
}
} else {
return false; // Invalid character
}
}
}

// Accepted if stack is empty after processing


return pdaStack.empty() && state == 1; // Must be in q1 and stack empty
}

int main() {
string input;

cout << "Enter a string to test (format wXwR where w is over {a,b}): ";
getline(cin, input);

if (isAccepted(input)) {
cout << "Accepted\n";
} else {
cout << "Rejected\n";
}

return 0;
}

Output :
Enter a string to test (format wXwR where w is over {a,b}): ababbXbbaba
Accepted

Enter a string to test (format wXwR where w is over {a,b}): babbbbXbabbbb


Rejected

Question 9.) Design and simulate a Turing Machine that accepts the language anbncn where n >0.

Code :

Question 10.) Design and simulate a Turing Machine which will increment the given binary number by 1.

Code :
#include <iostream>
#include <string>
using namespace std;

class TuringMachine {

public:
TuringMachine(const string& input) : tape(input + ' '), head(0) {}

void increment() {
// Move to the end of the number
while (tape[head] != ' ') {
head++;
}
head--; // Move back to the last digit

// Start the increment process


while (head >= 0) {
if (tape[head] == '0') {
tape[head] = '1'; // Change 0 to 1
break; // Increment done, break out
} else if (tape[head] == '1') {
tape[head] = '0'; // Change 1 to 0 (carry)
} else {
break; // Shouldn't reach here, just in case
}
head--; // Move left
}

// If we exit the loop and head is out of bounds, we need to add a new '1'

if (head < 0) {
tape = '1' + tape; // Prepend '1' to the tape
}
}

string getResult() const {


return tape;
}

private:
string tape;
int head;
};

int main() {
string input;
cout << "Enter a binary number: ";
getline(cin, input);
// Validate input
if (input.empty() || input.find_first_not_of("01") != string::npos) {
cout << "Invalid input. Please enter a binary number.\n";
return 1;
}

TuringMachine tm(input);
tm.increment();
cout << "Incremented binary number: " << tm.getResult() << endl;

return 0;
}

Output :
Enter a binary number: 000110000101
Incremented binary number: 000110000110

THANK YOU

You might also like