Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks:

Obtained Marks:

Date:

Finite Automata Theory


And
Formal Languages
Assignment # 1

Last date of Submission: 20 October 2022

Submitted To: Zain Arshad

Student Name: Abid Ali Akbar

Reg. Number: 2273107

FOP MSCS-1 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

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 a.
a. Give its transition diagram using JFlap.
b. 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 scheme that allows state
to state function-call and recursion.
c. Give the source code and the runtime screen while testing the strings aabab, abbaaba and
abbb.
Note:
1. Change the filename to your ID, e.g. 2073105.doc
2. Upload the .doc on Google Classroom.
3. Make sure that the output screen does not have black background.
4. Poor indentation and wrong format will result in deduction of marks.

Solution-
Step-01:

 All strings of the language starts with substring “a”.


 So, length of substring = 1.
 

Thus, Minimum number of states required in the DFA = 1 + 2 = 3.


It suggests that minimized DFA will have 3 states.

Step-02:
We will construct DFA for the following strings-
a
 aa
Step-03:
The required DFA is-

FOP MSCS-1 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

Regular expression for the given language = a(a + b)*


C++ Code

#include <iostream>
#include <string>
using namespace std;
int ctr;
char ch;
string str;
void state1();
void state2();
void state3();
int main()
{
do {
cout << "DFA: Strings starting with a" << endl;
cout << "String: ";
cin >> str;
if( str!="quit" ) {
ctr = 0;
state1();
cout << endl;
}
} while( str!="quit" );
return 0;
}
void state1()
{
if( ctr>=str.length() ) {
cout << "String Rejected" << endl;
return;
}
ch = str.at(ctr++);
if( ch=='a' ) state2();
else if( ch=='b' ) state3();
else {
cout << "Invaid character " << ch << "in the input string" << endl;
return;
}
}
void state2()
{
if( ctr>=str.length() ) {
cout << "String Accepted" << endl;
return;
}
ch = str.at(ctr++);
if( ch=='a' || ch=='b' )
state2();
else {
cout << "Invaid character " << ch << "in the input string" << endl;
return;
}
}
FOP MSCS-1 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
void state3()
{
if( ctr>=str.length() ) {
cout << "String Rejected" << endl;
return;
}
ch = str.at(ctr++);
if( ch=='a' || ch=='b' )
state3();
else {
cout << "Invaid character " << ch << " in the input string" <<
endl;
return;
}
}

Screen Shot

FOP MSCS-1 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

//

// main.cpp

// DFA

//

// Created by Saiyam on 08/11/20.

// Copyright © 2020 Saiyam. All rights reserved.

//

#include <iostream>

#include <string.h>

using namespace std;

bool q1(string, int);

bool q2(string, int);

FOP MSCS-1 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
bool q3(string, int);

bool q4(string, int);

bool q1(string input, int index)

if(index == input.length())

return false;

// Otherwise transition to a state

if(input[index] == 'a')

return q1(input, index+1);

else

return q3(input, index+1);

bool q2(string input, int index)

if(index == input.length())

return false;

// Otherwise transition to a state

if(input[index] == 'a')

return q4(input, index+1);

else

return q2(input, index+1);

bool q3(string input, int index)

if(index == input.length())

FOP MSCS-1 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
return true;

// Otherwise transition to a state

if(input[index] == 'a')

return q1(input, index+1);

else

return q3(input, index+1);

bool q4(string input, int index)

if(index == input.length())

return true;

// Otherwise transition to a state

if(input[index] == 'a')

return q4(input, index+1);

else

return q2(input, index+1);

bool q0(string input, int index)

if(index == input.length())

return false;

// Otherwise transition to a state

if(input[index] == 'a')

return q1(input, index+1);

else

FOP MSCS-1 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
return q2(input, index+1);

int main(int argc, const char * argv[]) {

string input;

while(1)

cout<<"Enter string: "<<endl;

cin>>input;

if(input == "quit")

exit(1);

bool result = q0(input,0);

if(result)

cout<<"String accepted"<<endl;

else

cout<<"Invalid string -- not accepted"<<endl;

return 0;

FOP MSCS-1 SZABIST-ISB

You might also like