【问题标题】:CFG to PDA (Context free grammar to Push Down Automata)CFG 到 PDA(下推自动机的上下文无关语法)
【发布时间】:2013-11-28 03:03:24
【问题描述】:

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

S-> abScB | e

B-> bB | b

【问题讨论】:

    标签: grammar pushdown-automaton


    【解决方案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 可以不同。 nm都是大于等于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”)。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      相关资源
      最近更新 更多