You are on page 1of 5

Total Marks: 04

Obtained Marks:

Finite Automata Theory and


Formal Languages
Assignment # 02
Last date of Submission: 24 Mar 2023

Submitted To: Muhammad Nadeem Khokhar


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Student Name: Abdullah Siraj


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Reg Number: 2112280


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.

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

--- --- ---


a)
b)
Old States New states
a b
0- 1 2
1 1 1
2+ 3 2
3 3 2

c)
#include <iostream>
#include <string>

using namespace std;

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;
}

for (int i = 0; i < input_string.length();i++)


{
c = input_string[i];

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)

You might also like