Header Ads

Header ADS

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):

        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():

                    popped_symbol = self.stack.pop()

                    if current_symbol == popped_symbol:

                        index += 1

                    else:

                        break

                else:

                    break


        if current_state == 'q1' and not self.stack:

            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"]

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

Powered by Blogger.