对于 Σ = {0, 1, 2} 上的上下文无关文法 G,起始变量为 S:
S → 0S0 | 1S1 | 2S2 |是
是 → 22
我如何将其变成等效的下推自动机
下推自动机可以将符号推入堆栈顶部并将其弹出。它还可以将其转换基于最顶层的堆栈符号。我们需要考虑一种机制,允许我们通过操作堆栈来接受正确的语言。
您的语法生成的语言具有以下特征:
- It has
22
在中间
- 这是一个回文
{0, 1, 2}
。也就是说,它向前读和向后读是一样的。
我们需要“记住”字符串的开头,以便能够判断字符串的结尾是否向后重复。这是堆栈的一个很好的用例:我们可以在看到符号时将它们推入堆栈,然后在读回它们时将它们弹出。另请注意:我们知道我们只能尝试在阅读后开始回读22
.
首先,我们读取所有内容并将其压入堆栈,直到找到“22”:
Q s S Q' S'
----------------------
// read 0s and 1s and push them on the stack
q0 0 Z q0 0Z
q0 0 0 q0 00
q0 0 1 q0 01
q0 1 Z q0 1Z
q0 1 0 q0 10
q0 1 1 q0 11
// if you read a 2, pus it on the stack and
// go to a new state where we look for a second 2
q0 2 Z q1 2Z
q0 2 0 q1 20
q0 2 1 q1 21
// if you read a 2 and then 0 or 1, go back to the
// initial state and keep reading input. otherwise,
// if you find a second two, proceed
q1 0 2 q0 02
q1 1 2 q0 12
q1 2 2 q2 22
// assume the '22' we just saw was not the one in
// the middle of the input string and that we need
// to keep reading input.
q2 0 2 q0 02
q2 1 2 q0 12
q2 2 2 q2 22
// assume the '22' we just saw was the one in the
// middle of the input string and that we now need
// to start reading from the stack.
q2 - 2 q3 -
q3 - 2 q4 -
q4 0 0 q4 -
q4 1 1 q4 -
q4 2 2 q4 -
q4 - Z q5 Z
// we read a string in the language and are
// ready to accept in q5. go to dead state q6
// explicitly if still have input.
q5 0 Z q6 Z
q5 1 Z q6 Z
q5 2 Z q6 Z
// consume the rest of the input in the dead state.
q6 0 Z q6 Z
q6 1 Z q6 Z
q6 2 Z q6 Z
的过渡q5
and q6
如果您将机器崩溃定义为字符串被明确拒绝(这是典型的情况),那么这并不是绝对必要的。我包含了这些转换,以便 PDA 能够优雅地耗尽所有输入而不会崩溃。
该 PDA 不是确定性的。这种语言没有确定性的 PDA。基本上,在我们读取任何子字符串之后22
,我们必须猜测该实例是否22
就是中间那个。如果是这样,我们需要开始读回初始字符串,看看是否有回文。如果没有,我们需要继续将符号压入堆栈。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)