如何为n(a)构造PDA小于或等于2n(b)

时间:2018-09-18 16:37:22

标签: automata computation-theory

所以这就是我所困的地方,我必须构造一个PDA,该PDA接受条件为n(a)小于或等于2n(b)的来自{a,b} *的单词

1 个答案:

答案 0 :(得分:0)

Put yourself into the frame of mind of a pushdown automaton. All you know how to do is read input and the stack, and then push/pop and change state based on what you see. If you start reading a string and need to tell whether it's in the language, what can you do?

It seems to me the best we can do starting out is to remember how many as we are reading. Until we see bs, there's no reason to do anything else. That is, simply read as and push them on the stack. The following rules should suffice:

Q    i    s    Q'    s'
--   --   --   --    --
q0   a    Z    q0    aZ
q0   a    a    q0    aa

Now, what happens when we see a b? If we want at least twice as many bs as as, we can't just cross of as for each b, since that would give at least as many bs as as but not at least twice as many.

What if we cross off two as for each b? Well, that gives us at least half as many bs as as, which is the wrong direction. This suggests crossing off one a for every two bs, which turns out to be correct. The rules:

Q    i    s    Q'    s'
--   --   --   --    --
q0   b    a    q1    a
q1   b    a    q2    -
q2   b    a    q1    a

Note we created a new state q2 which has one rule in common with q0 but not the ones for reading as. Indeed, once we start reading bs, we don't want to allow any more as to be read. Leaving out the rules will crash the automaton and reject the string.

If we have exactly twice as many bs as as, we will end up in state q2 with no more input and an empty stack. If we have additional bs, we need a rule to allow them. It suffices to add a self loop on state q2:

Q    i    s    Q'    s'
--   --   --   --    --
q2   b    Z    q2    Z

Taken together, these states and transitions should be pretty close to a working PDA for your language. Other options would have been to write a CFG first and then apply an algorithm to convert the CFG to a PDA, for example, a top-down or bottom-up parser.