0% found this document useful (0 votes)
43 views16 pages

CS 301 - Lecture 5 Regular Grammars, Regular Languages, and Properties of Regular Languages Review

The document discusses regular grammars and regular languages. It defines regular grammars as right-linear or left-linear grammars. Regular grammars generate regular languages. Specifically, the languages generated by the example regular grammars shown are regular languages that can be described by regular expressions.

Uploaded by

Fitawu Tekola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views16 pages

CS 301 - Lecture 5 Regular Grammars, Regular Languages, and Properties of Regular Languages Review

The document discusses regular grammars and regular languages. It defines regular grammars as right-linear or left-linear grammars. Regular grammars generate regular languages. Specifically, the languages generated by the example regular grammars shown are regular languages that can be described by regular expressions.

Uploaded by

Fitawu Tekola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Review

•  Languages and Grammars


CS 301 - Lecture 5 –  Alphabets, strings, languages
•  Regular Languages
Regular Grammars, Regular
–  Deterministic Finite Automata
Languages, and Properties of –  Nondeterministic Finite Automata
Regular Languages –  Equivalence of NFA and DFA
Fall 2008 –  Regular Expressions
•  Today:
–  Regular Grammars and Regular Languages
–  Properties of Regular Languages

Grammars Grammar Notation


•  Grammars express languages
Production Rules
• 
•  Example: the English language
sentence → noun _ phrase predicate noun → cat
noun → dog

noun _ phrase → article noun


Variable Terminal

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

The Resulting Language Definition of a Grammar


G = (V , T , S , P )
L = { “a cat runs”,
“a cat walks”, V : Set of variables
“the cat runs”,
“the cat walks”, T : Set of terminal symbols
“a dog runs”,
“a dog walks”, S : Start variable
“the dog runs”,
“the dog walks” } P: Set of Production rules

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 → λ}

Deriving Strings in the Grammar


•  Grammar: Sentential Form
•  A sentence that contains variables and
S → aSb terminals
S →λ
aabb :
•  Derivation of sentence S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb
S ⇒ aSb ⇒ aaSbb ⇒ aabb

Sentential Forms sentence


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:

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb

Language of a Grammar Example Language


S → aSb
Grammar can produce some set of strings
S →λ
Set of strings over an alphabet is a language Consider the set of all strings that can derived from this
grammar…..
Language of a grammar is all strings produced by the grammar
∗ S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb
L(G ) = {w : S ⇒ w}
S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb
⇒ aaaaSbbbb ⇒ aaaabbbb
String of terminals What language is being described?

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 →λ

A Non-Linear Grammar Another Linear Grammar


Grammar G: S → SS
Grammar G: S→A
S →λ
A → aB | λ
S → aSb
B → Ab
S → bSa
L(G ) = {w : na ( w) = nb ( w)} L(G ) = {a nb n : n ≥ 0}

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

Any regular grammar generates


a regular language

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

Step 1: Create States for


Example
Each Variable
Given right linear grammar: •  Construct NFA M such that every
state is a grammar variable:

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:

Any regular language L is generated Let M be the NFA with L = L(M ).


by some regular grammar G
Construct from M a regular grammar G
such that L( M ) = L(G )

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 → λ

But What About Left-Linear


In General
Grammars
•  Given any NFA, the previous procedure produces a
right linear grammar •  What happens if we reverse a left linear grammar as
–  We sketched a proof by construction follows:
–  Result is both a proof and an algorithm
•  Every regular language has an NFA Reverses to
–  Can convert that NFA into a right linear grammar
–  Thus every regular language has a right linear grammar Reverses to
•  Combined with Part 1, we have shown right linear •  The result is a right linear grammar.
–  If the left linear grammar produced L, then what does the
grammars are yet another way to describe regular resulting right linear grammar produce?
languages

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

•  Can produce a right linear grammar that produces


Reverses to

•  All right linear grammars produce regular languages


Reverses to so is a regular language
•  If the left linear grammar produced language ,
then the resulting right linear grammar produces •  The reverse of a regular language is regular so
Claim we just proved left linear grammars is a regular language!
produce regular languages? Why?

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 λ

Star Operation Example


NFA for L1 * λ w = w1w2  wk
NFA for
λ ∈ L1 * L1* = {a nb} * λ wi ∈ L1
M1
λ λ L1 = {a nb}
a
λ b λ

λ
λ

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

1. Take the DFA that accepts L1 M1′


a, b
L1 = {a, b} * −{a nb} a
2. Make final states non-final, b a, b
and vice-versa

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy