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.

:
, β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 . .
( ) , . - .