caleb531 / automata

A Python library for simulating finite automata, pushdown automata, and Turing machines
https://caleb531.github.io/automata/
MIT License
349 stars 64 forks source link

No support for epsilon stack moves in PDAs #236

Open Muon opened 1 month ago

Muon commented 1 month ago

The way Sipser's textbook defines PDAs, they can have epsilon stack moves. So for example "a, eps -> x" says "read a, push x" and "a, eps -> eps" says just "read a". These are compatible with the Hopcroft and Ullman definition which is used here. (Just a slight extension!) Could support for them please be added? Sipser's textbook is very popular and I just had to post this workaround to my class:

def fill_in_epsilon_stack_moves(stack_alphabet, table):
    """Convert a Sipser-style transition table to a Hopcroft-Ullman-style transition table.

    Table is modified in-place, and returned from this function."""
    for f in table.values():
        for g in f.values():
            if '' not in g:
                continue
            for b in stack_alphabet:
                new_transitions = set()
                for (q, s) in g['']:
                    if s == '':
                        new_transitions.add((q, b))
                    else:
                        new_transitions.add((q, (s, b)))
                if b in g:
                    g[b] |= new_transitions
                else:
                    g[b] = new_transitions
            del g['']
    return table
caleb531 commented 1 month ago

@eliotwrobson Do you have any thoughts on this? I can't say I'm too familiar with the specific algorithms, but adding support for this seems like a reasonable idea to me.

eliotwrobson commented 3 weeks ago

This seems like a reasonable thing to add! I think this just generalizes the definition of PDAs. There aren't too many algorithms implemented for those, so should be a straightforward change to add. I'm swamped with other projects now, but @Muon if you or someone else were to implement this change, I could definitely review.