如何证明一种语言是上下文无关的?


26

有许多技术可以证明某种语言不是上下文无关的,但是如何证明一种语言不是上下文无关的?

有什么技术可以证明这一点?显然,一种方法是展现该语言的上下文无关语法。是否有系统的技术来查找给定语言的无上下文语法?

对于常规语言中,有 系统的方法来得到一个正规文法/状态自动:例如,迈希尔-尼罗德定理提供了一种方法。上下文无关的语言是否有相应的技术?


在这里,我的动机是(希望)建立一个参考问题,其中包含一系列试图证明给定语言不受上下文限制时通常有用的技术。由于这里有很多问题是特殊情况,因此,如果我们能记录面对此类问题时可以使用的通用方法或通用技术,那就太好了。


请允许我留下我通常的笔记:在为手头语言提供上下文无关的语法时,您需要正确性证明,这可能会使方法变得笨拙。
— 拉斐尔

为了使它成为一个适当的参考问题,我们可以向问题转储者抛出,您能否添加一个有关语法和自动机的答案,也许每个例子都包含一个?谢谢!
— 拉斐尔

在将材料移到此处之前,请注意,Rick Decker和babou 在一个重复的问题上收集了一些典型的与上下文无关的成语。
— 拉斐尔

Answers:


13

在许多示例中有效的实用方法(但并非总是如此,我知道)正在尝试查找语言中字符串的嵌套结构。必须在字符串的不同部分中同时生成“嵌套依赖项”。

我们还有基本的工具箱:

  1. 串联:如果可以将语言分成两个连续的部分,请使用此产生形式S→S1S2

  2. 并集:分成不相交的部分S→S1∣S2

  3. 迭代:S→S1S∣ε

例子1

这是嵌套的示例(谢谢Raphael)。

L={bkal(bc)manbo∣k,l,m,n,o∈N,k≠o,2l=n,m≥2}

将替换为2 l。现在我们可以将n置于条件中。n2ln

将替换为k > o  或  k < o(混淆?o是“哦”而不是“零”)。应用工会工具。我们在这里使用k > o。同样,k > o,如果k = s + o且s > 0,其中s是一个新变量。替换ķ通过小号+ Ö。k≠ok>o or k<ook>ok>ok=s+os>0sks+o

L1={bs+oal(bc)ma2lbo∣l,m,o,s∈N,s>0,m≥2}

一些简单的重写。

L1={bbsboalbcbc(bc)m(aa)lbo∣l,m,o,s∈N}

现在,我们看到了嵌套结构,并开始构建语法。

, Ť → b ü, ü → b ù | ε(参见:级联和迭代这里)S1→TVT→bUU→bU∣ε

(我们在两侧生成 o b)V→bVb∣Wo b

W→aWaa∣X

, ÿ → b Ç b Ç, ž → b Ç ž | εX→YZY→bcbcZ→bcZ∣ε

例子2

K={akblcm∣l=m+k}

第一个“显而易见的”重写。

K={akbm+kcm∣m,k≥0}={akbmbkcm∣m,k≥0}

在语言学上,这称为“跨序列依赖”:交织(通常)强烈表示无上下文无关性。当然,m + k = k + m,我们就得救了。k,m,k,mm+k=k+m

K={akbk+mcm∣m,k≥0}={akbkbmcm∣m,k≥0}

与制作,X → 一个X b | ε,ÿ → b ý Ç | εS→XYX→aXb∣εY→bYc∣ε

类似地K′={akblcm∣m=k+l}={akblclck∣k,l≥0}

与制作,X → b X C ^ | εS→aSc∣XX→bXc∣ε


最后的评论:这些技术可以帮助您提出一种与上下文无关的候选语法,希望能识别您的语言。 可能仍需要正确性证明,以确保语法确实可以识别您的语言(仅此而已,仅此而已)。


11

可以使用CFL的一个特征,即Chomsky-Schützenberger定理。

戴克语言

让为字母。我们定义戴克 -language d Ť ⊆ (Ť ∪ Ť)*的Ť由上下文无关文法ģ = ({ 小号} ,Ť ∪ Ť,δ ,小号)与δ由下式给出TDT⊆(T∪T^)∗TG=({S},T∪T^,δ,S)δ

。S→aSa^S∣ε,a∈T

乔姆斯基-舒岑贝格定理

是如果上下文且仅当有L⊆Σ∗

  • 字母表,T
  • 一个正则语言和R⊆(T∪T^)∗
  • 同态ψ:(T∪T^)→Σ∗

以便

。L=ψ(DT∩R)

请注意,同态性扩展到单词(逐个符号),然后扩展到语言(逐个单词)。

例

考虑。用L={anbncm∣n,m∈N

  • (和,规范地,Ť = { ] ,⟩ }),T={[,⟨}T^={],⟩}
  • 并且R=L([∗]∗⟨∗⟩∗)
  • ψ(x)={a,x=[b,x= ]ε,x=⟨c,x= ⟩

该定理暗示是上下文无关的,特别是因为L

。DT∩R={[n]n⟨m⟩m∣n,m∈N}

例子2

表明,是上下文无关。L={bkal(bc)manbo∣k,l,m,n,o∈N,k≠o,2l=n,m≥2}

这里,我们需要对一种类型的括号的,一个用于b Ç,一个用于b,和另一用于将模拟b那个原因ķ ≠ Ò。我们用abcbbk≠o

  • ,T={[,⟨,⊢,<}
  • 和R=L(<+>+⊢∗[∗⟨⟨+⟩+⟩]∗⊣∗)∪L(⊢∗[∗⟨⟨+⟩+⟩]∗⊣∗<+>+)
  • ψ(x)={b,x∈{⊢,⊣,<}a,x=[aa,x= ]bc,x=⟨ε,else

and apply the theorem. In order to see that L=ψ(DT∩R), we don't need more than the fact that matching symbols (e.g. [ and ]) have to occur equally often in any w∈DT. Adding this contraint to the regular expressions we defined R by, we get

DT∩R={<p>p⊢o[l⟨m⟩m]l⊣o∣p≥1,o≥0,l≥0,m≥2}∪ {…}

and therewith

ψ(DT∩R)={bp+oal(bc)ma2lbo∣p≥1,o≥0,l≥0,m≥2}∪ {…}={bkal(bc)manbo∣k,l,m,n,o∈N,k>o,2l=n,m≥2}∪ {…}=L.

To grammars and automata

If we want to have an automaton or grammar in the end, we have some more work ahead of us.

  • Towards an automaton, construct the NPDA for DT and an NFA for R. The former is standard and we have algorithms for the latter, provided the language is given in a suitable representation (see also here). Intersection both is another standard construction and ψ can be applied to every transition individually.

  • Towards a grammar, build one for R (again, should be standard), take the one for DT and intersect them. Then apply ψ to the rule set (symbol for symbol).

Arguably, this is easy since algorithmic; the complexity lies in finding suitable T, R and ψ. I don't know if this approach is (often) simpler than constructing PDA/grammars directly but it may allow to focus on the important features of the language at hand. Try for yourself!


It is undecidable whether any given language is context-free.
— reinierpost
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.