PDA to accept a language of strings containing mor

2019-04-02 04:10发布

Produce a PDA to recognise the following language : the language of strings containing more a's than b's

I have been struggling with this question for several days now, I seem to have hit a complete mental block. Would any one be able to provide some guidance or direction to how I can solve this problem?

4条回答
Deceive 欺骗
2楼-- · 2019-04-02 04:48

I'm assuming you mean strings of the form a^nb^m, where n>m.

The idea is relatively easy, for a you push it on the stack (in a loop), for b you switch to a separate loop to pop a from the stack. If the stack is ever empty, you give up with a FAIL. If in the first loop you get anything other than a or b, or in the second loop you get anything other than b, you give up with FAIL.

At the end you try to pop another a and if the stack is empty you give up with a FAIL (ie, you had at least as many b's as a's on the stack, possibly more). If not, SUCCESS.

Edit: As a side note, I'm not convinced this is the right site for this question, might be better on programmers. Not sure though so not voting to close.

查看更多
叛逆
3楼-- · 2019-04-02 04:55

Your problem of "more a's than b's" can be solved by PDA.

All you have to do is:

  • When input is a and the stack is either empty or has an a on the top, push a on the stack; pop b, if b is the top.

  • When input is b and the stack is either empty or has an b on the top, push b on the stack; pop a, if a is on the top.

  • Finally, when the string is finished, go to the final state with null input if a is on top of the stack. Otherwise you don't have more a's than b's.

查看更多
疯言疯语
4楼-- · 2019-04-02 04:56

Basic infromation of transaction is given in below figure. enter image description here

Here is the complete transaction.

In it є is equal to NULL. $ sign is pushed into stack at the start of the string and pop at the end to determine the string has been completely read and end now. qo is start state and q5 is final state

It is Non-deterministic push down Automata (NPDA).So Due to NPDA the transaction of rejection string is not shown in it. enter image description here

查看更多
闹够了就滚
5楼-- · 2019-04-02 05:06

I have come up with a more general solution to the problem concerning the number of as and bs, see the picture below:

PDA for relations between as and bs

where a > b means more as than bs and so does a < b , a = b.

Z means the bottom of stack, and A/B are stack symbols.

I'm excited about it because this PDA seperates the 3 different states. In your problem, you can just set the a > b state to be the final state and let a = b be the start state.

And if you want to step further, you can use this PDA to easily generate PDAs for a >= b, a - b >= 1, 2 <= a - b <= 3 etc, which is fascinating.

查看更多
登录 后发表回答