For each stack symbol t E G, we look for a pair of states, qi and qj,
        such that the PDA in state qi can read some input a E S and push t
        on the stack and in state state qj can read some input b E S and pop t
        off the stack. In that case, we add the rule Aik -> a Alj b
        where (ql,t) E d(qi,a,e) and (qk,e) E d(qj,b,t).

def insert_symbol_pushpop(self):
        """
        For each stack symbol t E G, we look for a pair of states, qi and qj,
        such that the PDA in state qi can read some input a E S and push t
        on the stack and in state state qj can read some input b E S and pop t
        off the stack. In that case, we add the rule Aik -> a Alj b
        where (ql,t) E d(qi,a,e) and (qk,e) E d(qj,b,t).
        """

        for state_a in self.statediag:
            if state_a.type == 1:
                found = 0
                for state_b in self.statediag:
                    if state_b.type == 2 and state_b.sym == state_a.sym:
                        found = 1
                        for j in state_a.trans:
                            if state_a.trans[j] == [0]:
                                read_a = ''
                            else:
                                new = []
                                for selected_transition in state_a.trans[j]:
                                    if selected_transition == ' ':
                                        new.append('&')
                                    else:
                                        new.append(selected_transition)

                                read_a = " | ".join(new)
                            for i in state_b.trans:
                                if state_b.trans[i] == [0]:
                                    read_b = ''
                                else:
                                    new = []
                                    for selected_transition in state_b.trans[i]:
                                        if selected_transition == ' ':
                                            new.append('&')
                                        else:
                                            new.append(selected_transition)
                                    read_b = " | ".join(new)
                                self.rules.append(
                                    'A' + repr(state_a.id)
                                    + ',' + repr(i)
                                    + ':' + read_a
                                    + ' A' + repr(j)
                                    + ',' + repr(state_b.id)
                                    + ' ' + read_b)
                if found == 0:

                    # A special case is required for State 2, where the POPed symbols
                    # are part of the transitions array and not defined for "sym" variable.

                    for state_b in self.statediag:
                        if state_b.type == 2 and state_b.sym == 0:
                            for i in state_b.trans:
                                if state_a.sym in state_b.trans[i]:
                                    for j in state_a.trans:
                                        if state_a.trans[j] == [0]:
                                            read_a = ''
                                        else:
                                            read_a = " | ".join(
                                                state_a.trans[j])
                                        self.rules.append(
                                            'A' + repr(state_a.id)
                                            + ',' + repr(i)
                                            + ':' + read_a
                                            + ' A' + repr(j)
                                            + ',' + repr(state_b.id))
                                        # print
                                        # 'A'+`state_a.id`+','+`i`+':'+read_a+'
                                        # A'+`j`+','+`state_b.id`
                                        found = 1
                if found == 0:
                    print "ERROR: symbol " + repr(state_a.sym) \
                          + ". It was not found anywhere in the graph."