toc p: 9 Design a PDA to accept WCWR where w is any string and WR is the reverse of that string and C is a Special symbol.
class PDA:
def __init__(self):
self.stack = []
def process_input(self, input_string):
if not input_string:
# Handle empty input string case
return False
current_state = 'q0'
index = 0
while index < len(input_string):
current_symbol = input_string[index]
if current_state == 'q0':
if current_symbol.isalpha() or current_symbol == 'C':
self.stack.append(current_symbol)
index += 1
elif current_symbol == 'W':
current_state = 'q1'
index += 1
else:
break
elif current_state == 'q1':
if current_symbol.isalpha():
if self.stack:
popped_symbol = self.stack.pop()
if current_symbol == popped_symbol:
index += 1
else:
break
else:
# Stack underflow, not in language
return False
else:
break
# Check if input processed completely and stack is empty
if current_state == 'q1' and not self.stack and index == len(input_string):
return True
else:
return False
# Example usage:
def check_wcwr(input_str):
pda = PDA()
result = pda.process_input(input_str)
return result
# Test the PDA
input_strings = ["WCCW", "WCWR", "WCWRC", "WCW", "WCCCW", "W"]
for input_str in input_strings:
result = check_wcwr(input_str)
print(f'The string "{input_str}" is in the language WCWR: {result}')
No comments