Automata Lab
INDEX
Problem Name of the problem Page
no.
Regular Expressions
1 Give an RE that specifies each of the following languages 3
over {0, 1}.
a. a or bb or bab
b. Only as
c. All binary strings
d. Begins with a, ends with a
e. Ends with aa
2 Write a regular expression to describe inputs over the 8
alphabet {a, b, c} that are in sorted order.
3 Write a regular expression for binary strings with at least 14
two 0s but not consecutive 0s.
4 Write a regular expression for each of the following sets of 15
binary strings.
1. Has at least 3 characters, and the third character is 0
2. Number of 0s is a multiple of 3
3. Odd length
4. Length is at least 1 and at most 3
5 Write a regular expression that matches all strings over the 19
alphabet {a, b, c} that contain:
1. Starts and ends with a
2. At most one a
3. At least two a's
4. An even number of a's
5. Number of a's plus number of b's is even
6 Write a program that reads in text from standard input and 27
prints it back out, removing any trailing whitespace on a line
and replacing all tabs with 4 spaces.
DFAs and NFAs
7 Draw a 3-state DFA that accepts the set of all bitstrings 28
ending with 11.
8 Draw a DFA for bitstrings with at least one 0 and at least one 31
1.
9 Draw an NFA that recognize the language of all strings that 32
end in aaab.
10 Draw an NFA that recognize the language of all strings 33
whose 4th to the last character is a.
Regular Expressions
Problem No 1:
Give an RE that specifies each of the following languages over {0, 1}.
a. a or bb or bab
b. Only as
c. All binary strings
d. Begins with a, ends with a
e. Ends with aa
Solution for (a): a or bb or bab
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool function_for_1st_case(string s){
if(s.size()==1) /// 1 or 0 will be accepted
return true;
if(s.size()==2){
if(s[0]==s[1]) ///11 or 00 will be accepted
return true;
else
return false;
}
else if(s.size()==3){
if(s[0]==s[2]&&s[0]!=s[1])
return true;
else
return false;
}
}
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false;
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
bool a=function_for_1st_case(s);
if(is_valid(s)==false || s.size()==0)
cout<<"The given string is not accepted."<<endl;
else{
if(a==true)
cout<<"For RE a or bb or bab, given string is
accepted."<<endl;
else
cout<<"The given string is not accepted."<<endl;
}
return 0;
}
Solution for (b): Only aS.
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool function_for_2nd_case(string s){
/// 000....00 or 111....11 will be accepted.
int length=s.size();
char c=s[0];
for(int i=0;i<length;i++){
if(s[i]!=c){
return false; ///0001.. or 1110.., not accepted
}
}
return true;
}
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output
in text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
bool b=function_for_2nd_case(s);
if(is_valid(s)==false)
cout<<"The given string is not accepted."<<endl;
else{
if(b==true)
cout<<"For RE a*, given string is accepted."<<endl;
else
cout<<"The given string is not accepted."<<endl;
}
return 0;
}
Solution For (c): All binary strings
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool function_for_3rd_case(string s){
int length=s.size();
for(int i=0;i<length;i++){
if(s[i]!='0'&&s[i]!='1'){
return false;
}
}
return true;
}
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
bool c=function_for_3rd_case(s);
if(c==true)
cout<<"For RE (a|b)*, given string "<<s<<" is
accepted."<<endl; //(a|b)*={e,a,b,aa,ab,ba...}
else
cout<<"The given string is not accepted."<<endl;
return 0;
}
Solution for (d): Begins with a, ends with a
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool function_for_4th_case(string s){
int length=s.size();
if(s[0]==s[length-1]){ ///10001, 011110, 101,010 , first last
same will be accepted
return true;
}
else
return false;
}
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
bool d=function_for_4th_case(s);
if(is_valid(s)==false||s.size()==0)
cout<<"The given string is not accepted."<<endl; //NULL
string will not accepted
else{
if(d==true)
cout<<"For RE a(a|b)*a|a, given string is
accepted."<<endl;
else
cout<<"The given string is not accepted."<<endl;
}
return 0;
}
Solution For (e): Ends with aa
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool function_for_5th_case(string s){
int lenght=s.size();
if(s[lenght-2]==s[lenght-1]) ///example: 01011,00011 ….
return true;
else
return false;
}
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
bool e=function_for_5th_case(s);
if(is_valid(s)==false || s.size()==0)
cout<<"The given string is not accepted."<<endl; //NULL
string will not accepted
else{
if(e==true)
cout<<"For RE (a|b)*aa, given string is accepted."<<endl;
else
cout<<"The given string is not accepted."<<endl;
}
return 0;
}
Problem no 2:
Write a regular expression to describe inputs over the alphabet {a, b, c} that are in
sorted order.
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int flag=0;
for(int i=0;i<s.size();i++){
if(s[i]!='a'&&s[i]!='b'&&s[i]!='c')
flag=1;
}
if(flag==1)
cout<<"The given string is not accepted."<<endl; //char except
{a,b,c} will not accepted
else{
string tmp=s;
sort(tmp.begin(),tmp.end());
if(s==tmp)
cout<<"The given string is accepted."<<endl;
else
cout<<"The given string is not accepted."<<endl;
}
return 0;
}
Problem no 3:
Write a regular expression for binary strings with at least two 0s but not
consecutive 0s.
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
int count_zero(string s){
int cnt=0;
for(int i=0;i<s.size()-1;i++){
if(s[i]=='0'){
cnt++;
}
if(s[i]=='0'&&s[i+1]=='0'){
cnt=0;
return cnt;
}
}
return cnt;
}
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int no_of_zero=count_zero(s);
if(is_valid(s)==true && no_of_zero>=2)
cout<<s<<" is accepted by the language."<<endl;
else
cout<<s<<" is not accepted by the language."<<endl;
return 0;
}
Problem no 4:
Write a regular expression for each of the following sets of binary strings.
a. has at least 3 characters, and the third character is 0
b. number of 0s is a multiple of 3
c. odd length
d. length is at least 1 and at most 3
Solution for (a): Has at least 3 characters, and the third character is 0
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int length=s.size();
if(is_valid(s)==true&&length>=3&&s[2]=='0'){
cout<<s<<" is accepted by the language."<<endl;
cout<<"The RE is: (0|1)(0|1)0(0|1)*"<<endl;
}
else
cout<<s<<" is not accepted by the language."<<endl;
return 0;
}
Solution for (b): Number of 0s is a multiple of 3
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in
text file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int cnt=0;
for(int i=0;i<s.size();i++){
if(s[i]=='0')
cnt++;
}
if(is_valid(s)==true && cnt>0 && cnt%3==0){
cout<<s<<" is accepted by the language."<<endl;
cout<<"The RE is: 1*|(1*01*01*01*)"<<endl;
}
else{
cout<<s<<" is not accepted by the language."<<endl;
}
return 0;
}
Solution for (c): Odd length
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int length=s.size();
if(is_valid(s)==true && length%2==1){
cout<<s<<" is accepted by the language."<<endl;
cout<<"The RE is: (0|1)((0|1)(0|1))*"<<endl;
}
else{
cout<<s<<" is not accepted by the language."<<endl;
}
return 0;
}
Solution for (d): Length is at least 1 and at most 3
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='1'&&s[i]!='0')
return false; /// except 0,1 the string is not accepted,
example: 2+1@101
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int length=s.size();
if(is_valid(s)==true && length>=1 && length<=3){
cout<<s<<" is accepted by the language."<<endl;
cout<<"The RE is: (0|1)|(0|1)(0|1)|(0|1)(0|1)(0|1)"<<endl;
}
else{
cout<<s<<" is not accepted by the language."<<endl;
}
return 0;
}
Problem no 5:
Write a regular expression that matches all strings over the alphabet {a, b, c} that
contain:
1. starts and ends with a
2. at most one a
3. at least two a's
4. an even number of a's
5. number of a's plus number of b's is even
Solution for (1): Starts and ends with a
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='a'&&s[i]!='b'&&s[i]!='c')
return false; /// except a,b,c the string is not accepted,
example: acde
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int last_position=s.size();
if(is_valid(s)==true && s[0]=='a' && s[last_position-1]=='a'){
cout<<s<<" is accepted by the language."<<endl;
cout<<"The RE is: a(a|b|c)*a"<<endl;
}
else{
cout<<s<<" is not accepted by the language."<<endl;
}
return 0;
}
Solution for (2): At most one a
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='a'&&s[i]!='b'&&s[i]!='c')
return false; /// except a,b,c the string is not accepted,
example: acde
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int last_position=s.size();
int no_of_a=0;
for(int i=0;i<last_position;i++){
if(s[i]=='a')
no_of_a++;
}
if(is_valid(s)==true &&no_of_a<=1){
cout<<s<<" is accepted by the language."<<endl;
cout<<"The RE is: ab*c* | b*c*a"<<endl;
}
else{
cout<<s<<" is not accepted by the language."<<endl;
}
return 0;
}
Solution for (3): At least two a's
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='a'&&s[i]!='b'&&s[i]!='c')
return false; /// except a,b,c the string is not accepted,
example: acde
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int last_position=s.size();
int no_of_a=0;
for(int i=0;i<last_position;i++){
if(s[i]=='a')
no_of_a++;
}
if(is_valid(s)==true &&no_of_a>=2){
cout<<s<<" is accepted by the language."<<endl;
}
else{
cout<<s<<" is not accepted by the language."<<endl;
}
return 0;
}
Solution for (4): An even number of a's
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='a'&&s[i]!='b'&&s[i]!='c')
return false; /// except a,b,c the string is not accepted,
example: acde
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int last_position=s.size();
int no_of_a=0;
for(int i=0;i<last_position;i++){
if(s[i]=='a')
no_of_a++;
}
if(is_valid(s)==true &&no_of_a%2==0){
cout<<s<<" is accepted by the language."<<endl;
}
else{
cout<<s<<" is not accepted by the language."<<endl;
}
return 0;
}
Solution for (5): Number of a's plus number of b's is even
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
bool is_valid(string s){
for(int i=0;i<s.size();i++){
if(s[i]!='a'&&s[i]!='b'&&s[i]!='c')
return false; /// except a,b,c the string is not accepted,
example: acde
}
return true;
}
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
cout<<"Please enter your input: ";
string s;
getline(cin,s);
cout<<endl;
int last_position=s.size();
int cnt=0;
for(int i=0;i<last_position;i++){
if(s[i]=='a'||s[i]=='b')
cnt++;
}
if(is_valid(s)==true &&cnt%2==0){
cout<<s<<" is accepted by the language."<<endl;
}
else{
cout<<s<<" is not accepted by the language."<<endl;
}
return 0;
}
Problem no 6:
Write a program that reads in text from standard input and prints it back out,
removing any trailing whitespace on a line and replacing all tabs with 4 spaces.
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("in.txt","r",stdin); ///function for taking input in text
file
freopen("out.txt","w",stdout); /// function for giving output in
text file
string s;
getline(cin,s);
cout<<s<<endl;
s.erase(remove(s.begin(),s.end(),' '),s.end()); ///removes
all space
s.erase(remove(s.begin(),s.end(),'\t'),s.end()); ///removes
all tab
cout<<s<<endl;
return 0;
}
DFAs and NFAs
Problem no 7:
Draw a 3-state DFA that accepts the set of all bitstrings ending with 11.
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int state=0;
int q[4][3]={{0,1},{0,2},{0,2}};
for(int i=0;i<=3;i++){
printf("q%d\tq%d\tq%d\n",i,q[i][0],q[i][1]);
}
getline(cin,s);
cout<<endl;
for(int i=0;i<s.size();i++){
if(s[i]=='0'){
if(state==0)state=0;
else if(state==1)state=0;
else if(state==2)state=0;
}
else if(s[i]=='1'){
if(state==0)state=1;
else if(state==1)state=2;
else if(state==2)state=2;
}
}
if(state==2)
cout<<"YES"<<endl;
else
cout<<"No"<<endl;
return 0;
}
States and test results:
Problem no 8:
Draw a DFA for bitstrings with at least one 0 and at least one 1.
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int state=0;
int q[4][2]={{3,1},{2,1},{2,2},{3,2}};
for(int i=0;i<=3;i++){
printf("q%d\tq%d\tq%d\n",i,q[i][0],q[i][1]);
}
cin>>s;
cout<<endl;
for(int i=0;i<s.size();i++){
if(s[i]=='0'){
if(state==0)state=1;
else if(state==1)state=1;
else if(state==2)state=3;
else if(state==3)state=3;
}
else if(s[i]=='1'){
if(state==0)state=2;
else if(state==1)state=3;
else if(state==2)state=2;
else if(state==3)state=3;
}
}
if(state==3)
cout<<"YES"<<endl;
else
cout<<"No"<<endl;
return 0;
}
States and test results:
Problem no 9:
Draw an NFA that recognize the language of all strings that end in aaab.
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int len=0,state=0;
cin>>s;
len=s.size();
if(len>=4)
{
if(s[len-1]=='b'&&s[len-2]=='a'&&s[len-3]=='a'&&s[len-4]=='a')
state=1;
else
state=0;
}
else
state=0;
if(state==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}
States and test results:
Problem no 10:
Draw an NFA that recognize the language of all strings whose 4th to the last
character is a.
CPP Solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int state=0;
int q[7][3]={{1,1,1},{2,2,2},{3,3,3},{4,5,5},{4,5,5},{5,5,5}};
for(int i=0;i<=5;i++){
printf("q%d\tq%d\tq%d\tq%d\n",i,q[i][0],q[i][1],q[i][2]);
}
cin>>s;
cout<<endl;
for(int i=0;i<s.size();i++){
if(s[i]=='a'){
if(state==0)state=1;
else if(state==1)state=2;
else if(state==2)state=3;
else if(state==3)state=4;
else if(state==4)state=4;
else if(state==5)state=5;
}
else if(s[i]=='b'){
if(state==0)state=1;
else if(state==1)state=2;
else if(state==2)state=3;
else if(state==3)state=5;
else if(state==4)state=5;
else if(state==5)state=5;
}
else if(s[i]=='c'){
if(state==0)state=1;
else if(state==1)state=2;
else if(state==2)state=3;
else if(state==3)state=5;
else if(state==4)state=5;
else if(state==5)state=5;
}
}
if(state==4)
cout<<"YES"<<endl;
else
cout<<"No"<<endl;
return 0;
}
States and test results: