可以使用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,b,ε,c,x=[x= ]x=⟨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,a,aa,bc,ε,x∈{⊢,⊣,<}x=[x= ]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!