Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CS 301 - Lecture 5 Regular Grammars, Regular Languages, and Properties of Regular Languages
Fall 2008
Regular Languages
Deterministic Finite Automata Nondeterministic Finite Automata Equivalence of NFA and DFA Regular Expressions
Today:
Regular Grammars and Regular Languages Properties of Regular Languages
Grammars
Grammars express languages Example: the English language
Grammar Notation
Production Rules
noun cat noun dog
predicate noun
Variable
Terminal
A Resulting Sentence
sentence noun _ phrase noun _ phrase article the noun noun verb predicate verb verb
Definition of a Grammar
G = (V , T , S , P )
V : Set of variables T : Set of terminal symbols
S : Start variable
P:
Set of Production rules
A Simple Grammar
Grammar:
S aSb S
:
Derivation of sentence
ab
G = (V , T , S , P )
V = {S } T = {a, b}
S aSb ab S aSb
P = {S aSb, S }
Sentential Form
A sentence that contains variables and terminals
S aSb S
Derivation of sentence
w1 wn
w1 w2 w3 wn
S aaabbb
w w
Instead of:
Language of a Grammar
Grammar can produce some set of strings Set of strings over an alphabet is a language Language of a grammar is all strings produced by the grammar
Example Language
S aSb S
Consider the set of all strings that can derived from this grammar..
L(G ) = {w : S w}
String of terminals
Linear Grammars
Grammars with at most one variable at the right side of a production
Examples:
S aSb S
A Non-Linear Grammar
Grammar
G:
S SS S S aSb S bSa
G:
SA A aB | B Ab
L(G ) = {w : na ( w) = nb ( w)}
Number of in string
L(G ) = {a nb n : n 0}
Right-Linear Grammars
All productions have form:
Left-Linear Grammars
All productions have form:
A xB
or
A Bx
or
A x
Example:
A x
Example:
S abS S a
string of terminals
S Aab A Aab | B Ba
string of terminals
Regular Grammars
Regular Grammars
S abS S a
S Aab A Aab | B Ba
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
Languages Generated by Regular Grammars
Theorem - Part 1
Regular Languages Languages Generated by Regular Grammars Regular Languages
Proof Part 1
Regular Languages
We will prove:
Example
Given right linear grammar:
Productions of the form are only slightly harder. Create row of states that derive w and end in
Productions of the form Create row of states that derive w and end in a final state
In General
Given any right-linear grammar, the previous procedure produces an NFA
We sketched a proof by construction Result is both a proof and an algorithm Why doesnt this work for a non linear grammar?
Since we have an NFA for the language, the right-linear grammar produces a regular language
Proof - Part 2
Regular Languages Languages Generated by Regular Grammars
Proof idea: Any regular language L is generated by some regular grammar G Let
q0
q1
q0 aq1
q2
b
M
q0
q1
q2
b
q3
q3
10
Step 2:
q0 aq1 q1 bq1 q1 aq2 q2 bq3 q3 q1 q3
Edges and Final States b
Step 2:
q0 aq1
q2
b
M
q0
q1
M
q0
q1
q2
b
q3
q3
If
In General
Given any NFA, the previous procedure produces a right linear grammar
We sketched a proof by construction Result is both a proof and an algorithm
Combined with Part 1, we have shown right linear grammars are yet another way to describe regular languages
11
For regular languages we will prove that: Union: Concatenation: Star: Reversal: Complement: Intersection:
L1 and L2
L1 L2 L1L2 L1 *
Are regular Languages
L1 L2 L1L2 L1 *
L1R
L1
L1 L2
L1R
L1
L1 L2
12
Regular language
L1
Regular language
L2
Example
L1 = {a nb}
n0
M1
b
L(M1 ) = L1
NFA
L(M 2 ) = L2
NFA
M1
M2 M2 L2 = { } ba
b
Union
NFA for
Example
NFA for
L1 L2
M1
M2
L2 = {ba}
b
13
Concatenation
NFA for
Example
NFA for
L1L2 M2
M1
L1 = {a nb}
a
b
L2 = {ba}
Star Operation
NFA for L1 *
Example
L1 *
NFA for L1* = {a nb} *
w = w1w2 wk
M1
wi L1
L1 = {a nb}
b
14
Reverse
L1 M1
NFA for L1R
Example
a
M1
b
M1
L1 = {a nb}
1. Reverse all transitions 2. Make initial state final state and vice versa
M1
b
L1R
= {ba }
Complement
Example
L1
M1
L1
M1
L1 = {a nb}
a
b
a, b
a, b
M1
1. Take the DFA that accepts L1 2. Make final states non-final, and vice-versa
L1 = {a, b} * {a nb} a
a, b
M1
a, b
15
Intersection
DeMorgans Law:
Whats Next
Read
Linz Chapter 1,2.1, 2.2. 2.3, (skip 2.4), 3, and Chapter 4 JFLAP Startup, Chapter 1, 2.1, (skip 2.2), 3, 4
L1 L2 = L1 L2
regular regular regular regular regular
L1 , L2 L1 , L2
L1 L2
L1 L2 L1 L2
Homework
Homework Due Thursday
16