Theory of Computation Practicals-1
Theory of Computation Practicals-1
PRACTICAL 1:
*************************************************
Practical 2:
Write a program for generating regular expressions for regular grammar
import re
line = "horses are taller than dogs";
searchObj = re.search( r'(.*) ARE (.*)', re.M|re.I)
if searchObj:
print ("searchObj.group() : ", searchObj.group())
print ("searchObj.group(1) : ", searchObj.group(1))
print ("searchObj.group(2) : ", searchObj.group(2))
else:
print ("Nothing found!!")
OUTPUT:
searchObj.group() : horses are taller than dogs
searchObj.group(1) : horses
searchObj.group(2) : taller than dogs
\ – Backslash
import re
s = 'geeks.forgeeks'
# without using \
match = re.search(r'.', s)
print(match)
# using \
match = re.search(r'\.', s)
print(match)
OUTPUT:
<re.Match object; span=(0, 1), match='g'>
<re.Match object; span=(5, 6), match='.'>
[] – Square Brackets
import re
string = "The quick brown fox jumps over the lazy dog"
pattern = "[a-m]"
result = re.findall(pattern, string)
print(result)
OUTPUT:
['h', 'e', 'i', 'c', 'k', 'b', 'f', 'j', 'm', 'e', 'h', 'e', 'l', 'a', 'd', 'g']
^ – Caret
import re
regex = r'^The'
strings = ['The quick brown fox', 'The lazy dog', 'A quick brown fox']
for string in strings:
if re.match(regex, string):
print(f'Matched: {string}')
else:
print(f'Not matched: {string}')
OUTPUT:
$ – Dollar
import re
# ['Hello', 'Hello']
PRACTICAL 3:
Write a program for generating derivation sequence / language for the given sequence of
productions
def printArray(arr, size):
for i in range(size):
print(arr[i], end = " ")
print()
return
# Driver code
n=1
k=2
printSequences(n, k)
Practical 4
Design a Program for creating machine that accepts three consecutive one.
Python3 implementation of the
# DFA of accepting of three
# a's and three b's
# State A
def stateA(n):
if(n[0]=='a'):
stateB(n[1:])
elif (n[0]=='b'):
stateH(n[1:])
# State B
def stateB(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateC(n[1:])
elif (n[0]=='b'):
stateI(n[1:])
# State C
def stateC(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateD(n[1:])
elif (n[0]=='b'):
stateJ(n[1:])
# State D
def stateD(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateQ2(n)
elif (n[0]=='b'):
stateE(n[1:])
# State E
def stateE(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateQ2(n)
elif (n[0]=='b'):
stateF(n[1:])
# State F
def stateF(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateQ2(n[1:])
elif (n[0]=='b'):
stateG(n[1:])
# State G
def stateG(n):
if(len(n)== 0):
print("String Accepted")
else:
if(n[0]=='a'):
stateQ2(n)
elif (n[0]=='b'):
stateQ2(n)
# State H
def stateH(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateI(n[1:])
elif (n[0]=='b'):
stateK(n[1:])
# State I
def stateI(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateJ(n[1:])
elif (n[0]=='b'):
stateL(n[1:])
# State J
def stateJ(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateE(n[1:])
elif (n[0]=='b'):
stateM(n[1:])
# State K
def stateK(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateL(n[1:])
elif (n[0]=='b'):
stateN(n[1:])
# State L
def stateL(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateM(n[1:])
elif (n[0]=='b'):
stateO(n[1:])
# State M
def stateM(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateF(n[1:])
elif (n[0]=='b'):
stateP(n[1:])
# State N
def stateN(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateO(n[1:])
elif (n[0]=='b'):
stateQ1(n)
# State Q
def stateO(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateP(n[1:])
elif (n[0]=='b'):
stateQ1(n)
# State P
def stateP(n):
if(len(n)== 0):
print("String Not Accepted")
else:
if(n[0]=='a'):
stateG(n[1:])
elif (n[0]=='b'):
stateQ1(n[1:])
# State Q1
def stateQ1(n):
print("String Not Accepted")
# State Q2
def stateQ2(n):
print("String Not Accepted")
# call stateA
# to check the input
stateA(n)
Program 2:
class FSM:
def __init__(self):
self.state = 'START'
def is_accepting(self):
return self.state == 'S3'
def reset(self):
self.state = 'START'
def accept_consecutive_ones(input_string):
fsm = FSM()
for char in input_string:
fsm.transition(char)
if fsm.is_accepting():
return True
return False
# Example
test_cases = [
"00111",
"111",
"10011",
"0110",
"000",
"1111",
]
Practical 5
Design a Program for creating machine that accepts the string always ending with 10
or 01.
print("q1->", end="");
if (i == len(s)) :
print("NO");
return;
# state transitions
# 0 takes to q1, 1 takes to q3
if (s[i] == '0') :
q1(s, i + 1);
else :
q3(s, i + 1);
def q2(s, i) :
# state transitions
# 0 takes to q4, 1 takes to q2
if (s[i] == '0') :
q4(s, i + 1);
else :
q2(s, i + 1);
def q3(s, i) :
# state transitions
# 0 takes to q4, 1 takes to q2
if (s[i] == '0') :
q4(s, i + 1);
else :
q2(s, i + 1);
def q4(s, i) :
def q0( s, i) :
# state transitions
# 0 takes to q1, 1 takes to q2
if (s[i] == '0') :
q1(s, i + 1);
else :
q2(s, i + 1);
# Driver Code
if __name__ == "__main__" :
s = "010101";
Program 2:
class FSM:
def __init__(self):
self.state = 'S0'
def transition(self, char):
if self.state == 'S0':
if char == '0':
self.state = 'S1'
elif char == '1':
self.state = 'S2'
elif self.state == 'S1':
if char == '0':
self.state = 'S1'
elif char == '1':
self.state = 'S3'
elif self.state == 'S2':
if char == '0':
self.state = 'S4'
elif char == '1':
self.state = 'S2'
elif self.state == 'S3':
if char == '0':
self.state = 'S1'
elif char == '1':
self.state = 'S2'
elif self.state == 'S4':
if char == '0':
self.state = 'S1'
elif char == '1':
self.state = 'S2'
def is_accepted(self):
return self.state in {'S3', 'S4'}
# Test cases
check_string("110")
check_string("001")
check_string("1010")
check_string("1101")
check_string("1001")
check_string("1111")
check_string("010")
check_string("1110")
Practical 6:
Construct DFA, which accepts set of all strings over {0, 1} which interpreted as a
binary number is divisible by 2
Code1:
def stateq0(n):
#if length found 0
#print not accepted
if (len(n)==0):
print("string accepted")
else:
#if at index 0
#'0' found call
#function stateq0
if(n[0]=='0'):
stateq0(n[1:])
def stateq1(n):
#if length found 0
#print not accepted
if (len(n)==0):
print("string not accepted")
else:
#if at index 0
#'0' found call
#function stateq0
if(n[0]=='0'):
stateq0(n[1:])
#call stateA
#to check the input
stateq0(n)
Code2:
class DFA:
def __init__(self):
# Initial state is q0
self.current_state = 'q0'
def is_accepting(self):
# Check if the DFA is in the accepting state
return self.current_state == 'q0'
def reset(self):
# Reset the DFA to the initial state
self.current_state = 'q0'
def check_divisibility_by_2(number):
dfa = DFA()
# Traverse the string and use the DFA to process the last digit
for char in number_str:
if char.isdigit():
dfa.transition(char)
def main():
try:
# Accept a decimal number from the user
user_input = float(input("Enter a decimal number: "))
if __name__ == "__main__":
main()
Output:
Practical 7:
Design a program for accepting decimal number divisible by 3.
# Driver Program
if __name__ == '__main__':
A = "10101";
CheckDivisibilty(A);
OUTPUT
YES
Code2:
Args:
number (float): The decimal number to check.
Returns:
bool: True if the number is divisible by 3, False otherwise.
"""
# Convert the float to an integer by multiplying by 10 raised to the power of
the number of decimal places
# and check if that integer is divisible by 3.
return scaled_number % 3 == 0
def main():
try:
# Accept input from the user
number = float(input("Enter a decimal number: "))
except ValueError:
print("Invalid input! )
OUTPUT
Enter a decimal number: 9.0
9.0 is divisible by 3.
Do you want to enter another number? (yes/no): no