Languages and automata
László Kabódi
Example
I L4 ={ww|w ∈ {0,1}∗} I L5 ={1n2}
Solution IV
I L4 ={ww|w ∈ {0,1}∗} I Let x=0p10p1
I Because |uv| ≤p,u andv can only contain 0.
I If we pump v with k >1, the rst half of x will be dierent from the second, so it won't be ww. Souvkw ∈/ L4
I L4 cannot be a regular language.
Solution V
I L5 ={1n2} I Let x=1p2
I |v| ≥1 and |uv| ≤p, so 1≤ |v| ≤p.
I |uvw|=p2. The next square number is (p+1)2. But if we usek =2 then
uv2w
can not be(p+1)2, but it is more than p2, so it is not a square number. uv2w ∈/ L5
I L5 cannot be a regular language.
How to use the pumping lemma
To show that a languageLis not regular, you have to I for a chosen word x∈L
I check for all possiblex =uvw partitions, where |uv| ≤p and
|v| ≥1
I that there is at least onek touvkw ∈/L
But if a language is pumpable, it does not mean that it is regular.
Denition
Grammar
A grammarG is:
I an alphabet (a,b, . . . ,∈Σ, terminals, do not change) I non-terminals or variables (V, they can change during a
deduction)
I starting variable or starting non-terminal (S) I deduction or production or substitution rules
I α→β
I αandβ are strings of terminals and/or non-terminals I αcontain at least one element fromV
I α∈(Σ∪V)∗V(Σ∪V)∗ I β∈(Σ∪V)∗
I derivation: using the substitution rules
I L(G): all the words that can be derived using the substitutions rules from the starting symbol
Examples
Examples I
What languages these grammars dene?
I Grammar I:
S →0S1 S →01 I Grammar II:
S →AB A→0A A→0 B →B1 B →1
Examples
Solutions I
I Grammar I:
S →0S1|01
L1 ={0n1n|n >0} I Grammar II:
S →AB A→0A|0 B →B1|1
L2 ={0i1j|i,j >0}
Examples
Example II
What language this grammar denes?
S →aSBC|abC CB →BC bB →bb bC →bc cC →cc
Examples
Solution II
I S →aSBC|abC,CB →BC,bB→bb,bC →bc,cC →cc I L={anbncn|n >0}
I Not every derivation can be nished:
S →aS BC →aaS BCBC →aaabC BCBC → aaabcBCBC →aaabcBBCC
I But:
S →aS BC →aaS BCBC →aaabCBCBC → aaabBC CBC →aaabBCBCC →aaabBBCCC → aaabbBCCC →aaabbbC CC →aaabbbcC C → aaabbbccC →aaabbbccc
Examples
Examples III
1. Construct a grammar for palindromes! (Σ ={a,b})
2. Construct a grammar for a language where the number of as and the number of bs are the same!
3. Is there a grammar for all languages that contain nite number of words?
4. Is there a grammar for every language?
5. Construct a grammar for even length words!
6. Construct a grammar for the correct parentheses!
7. Construct a grammar for L={akbncm |n=k+m}
Examples
Solutions III
1. Construct a grammar for palindromes! (Σ ={a,b}) S →aSa|bSb|a|b|ε
2. Construct a grammar for a language where the number of as and the number of bs are the same!
S →aSbS|bSaS|ε
3. Is there a grammar for all languages that contain nite number of words?
Yes, there is. You can create a rule for all words.
4. Is there a grammar for every language?
No, the number of languages are greater than the number of possible grammars.
Examples
Solutions III
5. Construct a grammar for even length words!
S →SS|aa|ab|ba|bb|ε
6. Construct a grammar for the correct parentheses!
S →SS|(S)|ε
7. Construct a grammar for L={akbncm |n=k+m} S →S1S2
S1 →aS1b|ε S2 →bS2c|ε