如何将下一个 CFG 转换为 PDA?
S-> abScB | e
B-> bB | b
我将首先查看 CFG 生成的语言。它实际上是一种相当复杂的语言。只需 S 的一次扩展,它将是abc(b(b*))
. 两个,你会得到ab[abc(b(b*))]c(b(b*))
,三个会ab[ab[abc(b(b*))]c(b(b*))]c(b(b*))
,依此类推。我已经把从 S 过渡添加的部分放在方括号中。这种语言似乎{(ab)^n (c(b(b^x_i)))^m}
是x_i
一个大于或等于 0 的整数,并且每个x_1, x_2, ... , x_i
都可以不同。n和m都是大于或等于 0 的整数。
要转换为 PDA,我们可以从简单的部分开始。您的字母表是 {a,b,c},“ab”部分需要一个状态,“c(b(b^x_i)”部分需要一个状态。我们称第一个状态为p,第二个q . 你的堆栈字母表将是 {bottom,A,C}。底部只是表示我们已经到达堆栈底部的符号。现在最困难的部分,增量转换,假设被空堆栈接受:
(p,e,bottom),(p,e) - this is for "m" = 0, accepting the empty string "e" or the case (ab)^n (this is an accepting state)
(p,a,bottom),(p, A bottom) - if we read an "a" and the stack is empty, push an A to the stack
(p,b,A),(p,e) - if we get a "b" and there was already an A on the stack, remove it
(p,c,bottom),(q,C bottom) - if we get a "c" and the stack was empty (i.e. we've finished the (ab)^n section), go to state q and push a C onto the stack
(q,b,C),(q,e) - if we get a "b" after a c, remove the C from the stack
(q,b,bottom),(q,bottom) - if we get a "b" in the b^x_i section, pop then immediately push bottom onto the stack
(q,c,bottom),(q,C bottom) - if we get a "c" after the b^x_i section, push a C onto the stack
(q,e,bottom),(q,e) - if we finish our string and our stack is empty (as it should be), then this is an accepting state
上述读取空字符串并产生空堆栈的转换是接受状态。不包括所有不允许的转换(例如,当堆栈上已经有一个 C 时得到一个“c”)。
希望有帮助!