CFG到PDA(无下文自动语法推送自动机)

时间:2013-11-28 03:03:25

标签: grammar pushdown-automaton

如何将下一个CFG转换为PDA?

S-> abScB | e

B-> bB | b

1 个答案:

答案 0 :(得分:1)

我首先看一下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}。Bottom只是表示我们已经到达堆栈底部的符号。现在是困难部分,delta转换,假设通过空堆栈接受:

(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”)。

希望有所帮助!