简体   繁体   English

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

[英]How to construct a PDA for n(a) is less than or equal to 2n(b)

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

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 a s we are reading.在我看来,我们能做的最好的起步是要记住多少a š我们阅读。 Until we see b s, there's no reason to do anything else.在我们看到b之前,没有理由做任何其他事情。 That is, simply read a s and push them on the stack.也就是说,只需读取a s 并将它们压入堆栈。 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 ?现在,当我们看到 a b时会发生什么? If we want at least twice as many b s as a s, we can't just cross of a s for each b , since that would give at least as many b s as a s but not at least twice as many.如果我们至少要多一倍b S作为a S,我们不能只越过a S代表每个b ,因为这将至少给尽可能多的b S作为a秒,但不是至少两倍多。

What if we cross off two a s for each b ?如果我们越过什么对开a S代表每个b Well, that gives us at least half as many b s as a s, which is the wrong direction.嗯,这为我们提供了至少一半的b S作为a s,这是错误的方向。 This suggests crossing off one a for every two b s, which turns out to be correct.这表明每两个b划掉一个a ,结果证明是正确的。 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 a s.请注意,我们创建了一个新状态q2 ,它与q0有一个共同的规则,但没有读取a的规则。 Indeed, once we start reading b s, we don't want to allow any more a s to be read.事实上,一旦我们开始读b S,我们不想让更多的a s到被读取。 Leaving out the rules will crash the automaton and reject the string.忽略规则将使自动机崩溃并拒绝字符串。

If we have exactly twice as many b s as a s, we will end up in state q2 with no more input and an empty stack.如果我们有许多准确的两倍b S作为a S,我们会在状态结束q2没有更多的输入和一个空栈。 If we have additional b s, we need a rule to allow them.如果我们有额外的b ,我们需要一个规则来允许它们。 It suffices to add a self loop on state q2:在状态 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.综合起来,这些状态和转换应该非常接近于您的语言的工作 PDA。 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.其他选择是先编写 CFG,然后应用算法将 CFG 转换为 PDA,例如,自顶向下或自底向上解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM