CS 301 - Lecture 5 Regular Grammars, Regular Languages, and Properties of Regular Languages Review
CS 301 - Lecture 5 Regular Grammars, Regular Languages, and Properties of Regular Languages Review
predicate → verb
1
Some Terminal Rules A Resulting Sentence
article → a
article → the sentence ⇒ noun _ phrase predicate
⇒ noun _ phrase verb
noun → cat ⇒ article noun verb
noun → dog ⇒ the noun verb
⇒ the dog verb
verb → runs ⇒ the dog walks
verb → walks
2
A Simple Grammar Example Grammar Notation
• Grammar: S → aSb S → aSb
S →λ S →λ
G = (V , T , S , P )
• Derivation of sentence : ab
S ⇒ aSb ⇒ ab
V = {S } T = {a, b}
S → aSb S →λ
P = {S → aSb, S → λ}
3
General Notation for Derivations Why Notation Is Useful
*
• In general we write: w1 ⇒ wn
• We can now write:
w1 ⇒ w2 ⇒ w3 ⇒ ⇒ wn *
• If:
S ⇒ aaabbb
*
w ⇒ w • Instead of:
• It is always the case that:
4
The Resulting Language
Linear Grammars
S → aSb
• Grammars with at most one variable at
S →λ the right side of a production
Always add on a and b on each side resulting in:
a’s at the left
b’s at the right
equal number of a’s and b’s
• Examples: S → aSb
The image cannot be displayed.
Your computer may not have
enough memory to open the image, S →λ
Number of in string
5
Right-Linear Grammars Left-Linear Grammars
• All productions have form:
• All productions have form:
A → Bx
A → xB
or
or A→ x
A→ x • Example: S → Aab
• Example: S → abS A → Aab | B string of
string of
terminals
S →a terminals B→a
Regular Grammars
• A regular grammar is any right-linear or
Regular Grammars left-linear grammar
• Examples:
S → abS S → Aab
S →a A → Aab | B
B→a
What languages are generated by these grammars?
6
Languages and Grammars
S → abS S → Aab
A → Aab | B
Regular Grammars
S →a
B→a Generate
L(G1 ) = (ab) * a L(G2 ) = aab(ab) * Regular Languages
Note both these languages are regular
we have regular expressions for these languages (above)
we can convert a regular expression into an NFA (how?)
we can convert an NFA into a DFA (how?)
we can convert a DFA into a regular expression (how?)
Do regular grammars also describe regular languages??
Theorem - Part 1
Theorem
Languages
Generated by
Regular Grammars
= Regular
Languages
Languages
Generated by
Regular Grammars
Regular
Languages
7
The case of Right-Linear
Proof – Part 1
Languages Grammars
Regular
Generated by
Languages • Let Gbe a right-linear grammar
Regular Grammars
• We will prove:
L(Gis)regular
The language L(G ) generated by
any regular grammar G is regular • Proof idea: We will construct NFA using the
grammar transitions
8
Step 2.1: Step 2.2:
Edges for Productions Edges for Productions
• Productions of the form
• Productions of the form
are only slightly harder…. Create row
result in
of states that derive w and end in
Step 2.3:
In General
Edges for Productions
• Productions of the form • Given any right-linear grammar, the previous
Create row of states that derive w and procedure produces an NFA
end in a final state – We sketched a proof by construction
– Result is both a proof and an algorithm
– Why doesn’t this work for a non linear grammar?
• Since we have an NFA for the language, the
right-linear grammar produces a regular
language
9
Proof - Part 2 Any regular language L is generated
Languages by some regular grammar G
Regular
Generated by
Languages
Regular Grammars
Proof idea:
Step 1:
NFA to Grammar Example Convert Edges to Productions
• Since L is regular there is an NFA b
b
q0 → aq1 M
a a
a a q0 q1 q2
q0 q1 q2 q1 → bq1
This transition in the NFA q1 → aq2 λ b
Looks a lot like a production rule λ b
q3 q2 → bq3 q3
10
Step 2: Step 2:
λ Edges and Final States λ Edges and Final States
b b
q0 → aq1 q0 → aq1
q1 → bq1 M a q1 → bq1 M a
a a
q0 q1 q2 q0 q1 q2
q1 → aq2 q1 → aq2
λ b λ b
q2 → bq3 q2 → bq3
q3 q3
q3 → q1 q3 → q1
If is a final state, add
q3 → λ q3 → λ
11
But What About Left-Linear Left-Linear Grammars
Grammars Produce Regular Languages
• Start with a Left Linear grammar that produces
• The previous slide reversed the language! want to show is regular
For regular languages L1 and L2 We say: Regular languages are closed under
we will prove that:
Union: L1 ∪ L2 Union: L1 ∪ L2
Concatenation: L1L2 Concatenation: L1L2
Star: L1 * Are regular Star: L1 *
Languages
Reversal: L1R Reversal: L1R
Complement: L1 Complement: L1
Intersection: L1 ∩ L2 Intersection: L1 ∩ L2
12
Example
Regular language L1 Regular language L2 M1
a
L(M1 ) = L1 L(M 2 ) = L2 L1 = {a nb} b
n≥0
NFA M1 NFA M2
M2
b a
L2 = {ba}
Single final state Single final state
Union Example
M1 NFA for L1 ∪ L2 = {a nb} ∪ {ba}
• NFA for
L1 ∪ L2 L1 = {a nb}
a
λ λ
b
λ λ
λ M2 λ
λ L2 = {ba} λ
b a
13
Concatenation Example
L1L2 = {a nb}{ba} = {a nbba}
NFA for
NFA for L1L2
M1 M2
L1 = {a nb}
λ λ a
L2 = {ba}
b λ b a λ
λ
λ
14
Example
Reverse
M1
NFA for L1R
a
L1 M1 M1′ L1 = {a nb} b
M1′
1. Reverse all transitions
a
L1R n
= {ba } b
2. Make initial state final state
and vice versa
Complement Example
M1
L1 M1 L1 M1′
a a, b
L1 = {a nb} b a, b
15
Intersection What’s Next
• Read
DeMorgan’s Law: L1 ∩ L2 = L1 ∪ L2 – Linz Chapter 1,2.1, 2.2. 2.3, (skip 2.4), 3, and Chapter 4
L1 , L2 – JFLAP Startup, Chapter 1, 2.1, (skip 2.2), 3, 4
regular
• Next Lecture Topics from Chapter 4.2 and 4.3
– Properties of regular languages
L1 , L2 regular – The pumping lemma (for regular languages)
• Quiz 1 in Recitation on Wednesday 9/17
L1 ∪ L2 regular – Covers Linz 1.1, 1.2, 2.1, 2.2, 2.3, and JFLAP 1, 2.1
– Closed book, but you may bring one sheet of 8.5 x 11 inch paper
with any notes you like.
L1 ∪ L2 regular – Quiz will take the full hour on Wednesday
• Homework
L1 ∩ L2 regular – Homework Due Thursday
16