Sprawl Lemmas for Regular and Context-Free Languages

Two growth lemmas are statements that are used to prove the boundedness of important classes of formal languages: regular and context-free. The importance of these classes for programmers is easy to understand: regular expressions (one of the descriptions of regular languages) are used quite often in the work, and programming languages ​​whose syntax is described by context-free grammars are even more so.


The lemmas are very similar to each other both in formulations and in proofs. This proximity seemed so wonderful to me that I decided to devote an entire article to it.


The plan is this: we understand what regular languages ​​are and what is the relationship between regular expressions and finite automata, we formulate and prove an extension lemma for regular languages, use it to prove the irregularity of several languages. Then we do a similar trick with context-free languages, along the way figuring out how they relate to regular languages ​​and how to get around the discovered restrictions using common grammars. Go!




KDPV illustrates the growth process for KS grammars


A formal language is an arbitrary set of strings (that is, simply sequences) in a finite alphabet of characters. The lines that make up a language are also called words . The alphabet is usually denoted by large sigma:Ξ£. , .. , : Ξ΅.


(.. ), : . . , .


1.


: , , .


, :


  • βˆ… β€” , ;
  • {Ξ΅} β€” , , , ;
  • {a},a∈Σ β€” , .

. A B β€” , :


  • AβˆͺB β€” ;
  • Aβ‹…B={Ξ±Ξ²|α∈A,β∈B} β€” : , A, B;
  • Aβˆ—={Ξ±1Ξ±2...Ξ±k|k∈N0,Ξ±i∈A} β€” : k A, k .

: N0=Nβˆͺ{0}, ,


: Aβ‹…B=AB.


. , , . , PuTTY, - :


http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+

, url' , , , , url'. .


: ([0-9]), ([s]?), (a+ β€” , aa*, .. ) . , , .


, ( ), | (), * () . , .


, :


abc(deβˆ—f|de)


, :


abcde
abcdf
abcdef
abcdeef
abcdeeef
abcdeeeef

.


2.


. β€” , . , (). .


. , , .


, , . , , . , β€” , .


. , , , , .



, bab|aabβˆ—ca


. ai, aj, ak β€” . ai ak , Lik. , ai aj Lij, aj ak β€” Ljk. , aj , Ljj.


aj, ai ak ,


Lβ€²ik=LikβˆͺLijβ‹…Lβˆ—jjβ‹…Ljk


, aj. , , , .




, , . , - , Ξ΅. , . , .


, . : ; , , , , ; , , , , .


, . , , :



, .


3.


L={anbn|n∈N0}. Ρ, ab, aabb, aaabbb . ? , .


, , . , , : , a β€” b. , ? … , , ?


, ! , , .


. L n∈N , βˆ€w∈L,|w|β‰₯n x, y, z, : w=xyz; yβ‰ Ξ΅; |xy|≀n; βˆ€kβ‰₯0:xykz∈L.

Let be L β€” , n, w∈L β€” n.


w: a0, a1, a2, ..., am. , , m+1 , mβ‰₯n.


n , . ai β€” , j. x β€” i w, y β€” w, ai aj, z β€” w, aj an.


ai aj , ( !) , , , βˆ€k∈N0:xykz∈L.


ai, aj β€” . , a0, a1, ..., ajβˆ’1 . , n. , j≀n |xy|≀n, .


: , , ( .. ) , , .


L={anbn|n∈N0}. n β€” . anbn anbn=xyz,
|xy|≀n, , , xy a. y a, . xykz k>1 a, b , , L. L. , L !


(n)n, . .


4. -


β€” , .


: () T () N; Ξ£=NβˆͺT. S∈N β€” .


P. Ο† Ξ£: (s1,s2)βˆˆΟ†, s1 s2. : . , (s1,s2)βˆˆΟ†, s1β†’s2.


Ξ² Ξ±, Ξ±=xs1z, Ξ²=xs2z (s1,s2)βˆˆΟ†. , β€” . : α⊒β.


, Ξ² Ξ± ( ), s0=Ξ±, s1, s2, ..., sk+1=Ξ², : si⊒si+1. Ξ±β‡’Ξ².


s , : s∈Tβˆ—, Sβ‡’s. , , .


, - (-), β€” . , - , - .


, - :


S→(S)S
S→Ρ


, , . , - , . ; .


, , «» :


def BuildPath(queue, parents, parent):
    path = []
    while parents[parent] != parent:
        path += [queue[parent]]
        parent = parents[parent]
    return path[::-1]

def Solve(rules, target):
    queue = ['S']
    parents = [0]

    idx = 0
    while idx < len(queue):
        current = queue[idx]

        for rule in rules:
            entryIdx = current.find(rule[0])
            while entryIdx != -1:
                new = current[:entryIdx] + rule[1] + current[entryIdx + len(rule[0]):]

                if new == target:
                    path = [queue[0]] + BuildPath(queue, parents, idx) + [new]
                    return path

                queue.append(new)
                parents.append(idx)

                entryIdx = current.find(rule[0], entryIdx + 1)

        idx += 1

, , , , , ; ! :


rules = [
    ("S", "(S)S"),
    ("S", ""),
]
target = "(()())()"
print('\n'.join(Solve(rules, target)))

S
(S)S
((S)S)S
((S)(S)S)S
((S)(S)S)(S)S
(()(S)S)(S)S
(()()S)(S)S
(()())(S)S
(()())()S
(()())()

- L={anbn|n∈N0}:


rules = [
    ("S", "aSb"),
    ("S", ""),
]
target = "aaabbb"
print('\n'.join(Solve(rules, target)))

S
aSb
aaSbb
aaaSbbb
aaabbb

- , . , -, . , - : , , , .


, - .


5. -


L={anbncn|n∈N0}. , - anbn, , , - :


S→Ρ
S→AB
A→aAb
B→Bc
A→Ρ
B→Ρ


anbncm. , m n? Β« Β» , , . , , . -,


- . - L n∈N , βˆ€w∈L,|w|β‰₯n u, v, x, y, z, : w=uvxyz; vyβ‰ Ξ΅; |vxy|≀n; βˆ€kβ‰₯0:uvkxykz∈L.

, .


, . , . , :
S→Ρ
A→BC
A→a


, , : , β€” . . .



- acd


, . n=2|N|+1, |N| β€” , w∈L,|w|β‰₯n. . β€” , .. . , m , |N|+1. , -. , .


B , , B. : , , .


B , S⊒uBz. B , B, Bβ†’vBy, vyβ‰ Ξ΅, .. , . B x.



:


  • S⊒uBz
  • B⊒vBy
  • B⊒x

, βˆ€k∈N0S⊒uvkxykz, vyβ‰ Ξ΅.


vxy. .. B, , |N|. , 2|N|+1=n. |vxy|≀n.


L={anbncn|n∈N0}. , -. , n β€” . anbncn.


, anbncn=uvxyz, |vxy|≀n vyβ‰ Ξ΅, uvkxykz .


vxy a, c, .. w a c n b, vxy n.


uvkxykz k. , k>1 m, uvkxykz=ambmcm, L. , -!




6.


, - . , L={anbncn|n∈N0}, .


:


S→Ρ
S→aHbCE


E , b c. :


E→Ρ


H :


H→aHbC
H→Ρ


, , C c. - !


Cb→bC
CE→Ec


! 5 , :


rules = [
    ("S", "aHbCE"),
    ("H", ""),
    ("H", "aHbC"),
    ("Cb", "bC"),
    ("CE", "Ec"),
    ("E", ""),
]
target = "aaabbbccc"
print('\n'.join(Solve(rules, target)))

S
aHbCE
aaHbCbCE
aaaHbCbCbCE
aaaHbbCCbCE
aaaHbbCbCCE
aaaHbbbCCCE
aaaHbbbCCEc
aaaHbbbCEcc
aaaHbbbEccc
aaaHbbbccc
aaabbbccc

: «» ; C . .


( ) , . - .


All Articles