Professional Documents
Culture Documents
Obtained Marks:
Question
Consider a language defined over ∑={a,b} that accepts the strings starting with b and ending in
b.
• Draw its DFA using JFlap.
• Give its transition table.
• Write a C/C++ program that stays in an infinite loop, prompts the user for a string,
terminates if the string is QUIT, and otherwise implements the DFA using the transition
table.
• Give the source code and the runtime screen while testing the strings aabab, bbbab, bba
and abbb.
Note:
• Change the filename to your ID, e.g. 2073105.doc
• Upload the .doc on Google Classroom.
• Do not use system calls.
• Make sure that the output screen does not have black background.
• Poor indentation and wrong format will result in deduction of marks.
Solution
c)
#include <iostream>
#include <string>
int main() {
int input_val;
char c;
int transition_table[4][2] = {{1, 3}, {1, 1}, {2, 3},
{2,3}};
while (true) {
int state = 0;
string input_string;
cout << "Enter a string (QUIT to exit): ";
cin >> input_string;
if (input_string == "QUIT") {
break;
}
switch (c) {
case 'a':
input_val = 0;
state = transition_table[state][input_val];
break;
case 'b':
input_val = 1;
state = transition_table[state]
[input_val];
break;
default:
cout << "Invalid!" << endl;
break;
}
if (state == 3) {
cout << "Accepted" << endl;
}
else {
cout << "Rejected" << endl;
}
return 0;
}
d)