Languages and automata
László Kabódi
Chomsky hierarchy
Chomsky hierarchy - Type-3
I also called Regular grammars
I Can have only two types of rules (A andB are non-terminals, a is a terminal):
I A→aB I A→a
I Is the starting symbol S is not on the right hand side of any rule, there can be S →ε
Chomsky hierarchy
Chomsky hierarchy - Type-2
I also called Context-free grammars
I Can have only one type of rule (A is a non-terminal,
α∈(V ∪Σ)+ is a string of terminals and/or non-terminals):
I A→α
I If the starting symbolS is not on the right hand side of any rule, there can be S →ε
I It is called context-free, because you can use a rule to change a non-terminalA regardless of the context, the terminals and non-terminals next toA
Chomsky hierarchy
Chomsky hierarchy - Type-1
I also called Context sensitive grammars
I Can have only one type of rule (A is a non-terminal,
α∈(V ∪Σ)+, β, γ∈(V ∪Σ)∗ are strings of terminals and/or non-terminals):
I βAγ→βαγ
I It is called context sensitive, because you can only change a non-terminalA toα in the right context, if there isβ before andγ after A.
Chomsky hierarchy
Chomsky hierarchy - Type-0
I Every other grammar
I a languageL is a Type-i language if there is a grammar that is Type-i, but there is no Type-i+1 grammar
Examples
Examples for regular grammars
1. Create a regular grammar forL={a,b}∗!
2. Is there a regular grammar for all languages that contain nite number of words?
3. Create a regular grammar for the language that contain the even length words!
Examples
Solutions
1. Create a regular grammar forL={a,b}∗! S →ε|aA|bA|a|b
A→aA|bA|a|b
2. Is there a regular grammar for all languages that contain nite number of words?
Yes, there is. You can break up the rules in the non-regular grammar for the same language.
3. Create a regular grammar for the language that contain the even length words!
S →aA|bA A→aS|bS|a|b
Regular grammars and regular languages
I Theorem: For every nite automaton M there is anM0 nite automaton where the initial state does not appear later.
I Proof: Create a new state S0, redirect all transitions that would go to the initial state S to go to S0. The transitions fromS0 are the same as from S. If S is also an accepting state, make S0 an accepting state.
Regular grammars and regular languages
Regular grammars and regular languages
I Theorem: The language Lis regular if and only if there is a regular grammar G for it (L(G) =L).
I The proof has two parts:
I If a languageLis regular, then there is a regular grammar for it.
I For a regular grammar, there is a nite automaton that accepts the same language.
I If we prove both of these, that means, that regular grammars and regular languages are the same set of languages.
Regular grammars and regular languages
Language L is regular ⇒ there is a regular grammar for L
I If Lis regular, then there is a nite automatonM for it, where L(M) =L. (Withoutεtransitions.)
I Let thisM be one, where the initial state does not appear later.
I We can create a regular grammar from this automaton:
I We create a non-terminal from each state otM. The starting non-terminal is from the initial state.
I From every transition we create one or two deduction rules.
Regular grammars and regular languages
Deduction rules from transitions
I A a B
: AA →aAB I A a B
: AA →aAB|a
I If the initial state is an accepting state: S →ε
I The grammar will be a regular grammar, because all the rules are inA→aB or A→a form.
I The accepted language will be the same, because the rules follow the transitions. We only nish a derivation with a deduction rule created form an accepting state.
Regular grammars and regular languages
There is a regular grammar for L ⇒ L is a regular language
I We do the inverse of the previous algorithm.
I Create a state for every non-terminal, and a plus stateF, that will be the only accepting state.
I For every ruleA→aB create a transition A a B
I For every ruleA→acreate a transition A a F I If there isS →ε, stateS will be an accepting state. (Because
this rule only allowed if S is not on the right hand side of any other rule, this won't accept unwanted words.)
Examples
Example I
Create a regular grammar from the following nite automaton:
S
A
B
C
D a
b
a a
a
b b a
Examples
Solution I
S
A
B
C
D a
b
a a
a
b b a
S →aA|bB A→aC|a
B →aA|aC|bD|a|b C →aD|a
D →bC|b
Examples
Example II
Create a regular grammar from the following nite automaton:
S
A
B
C
D E
a
b a
a
b b
b
a
b
Examples
Solution II
First, change the automaton, becauseS has incoming transitions:
S
S0 A
B C
E a
b a
b a
a b
b b
a
S →ε|aA|a|bE|b A→aC|a|aS0|bB B →bS0|bD|b C →aC|a D →bD|b
Examples
Example III
Create a regular grammar from the following nite automaton:
A B C
D 0
1 0
1 0 1
0, 1
Examples
Solution III
A B C
D 0
1 0
1 0 1
0, 1
A→0B|1D|0 B →0D|1C C →0B|1D|0 D →0D|1D
Examples
Example IV
Create a nite automaton from the following regular grammar:
S →ε|aB|bC A→aB|a B→bA|b C →bB|aA
Examples
Solution IV
S →ε|aB|bC A→aB|a B →bA|b C →bB|aA
S
B
C
A F
a
b
b b
a
a
a b
Examples
Example V
Create a nite automaton from the following regular grammar:
S →aA|bB|b A→aS|bC B→aC|bS C →aB|bA|a
Examples
Solution V
S →aA|bB|b A→aS|bC B →aC|bS C →aB|bA|a
S F
A
B
C a
b b a
b
a
b a
b a