Sei sulla pagina 1di 16

Review

Languages and Grammars

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

Alphabets, strings, languages

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

sentence noun _ phrase noun _ phrase article predicate verb

predicate noun

Variable

Terminal

Some Terminal Rules


article a article the noun cat noun dog verb runs verb walks

A Resulting Sentence
sentence noun _ phrase noun _ phrase article the noun noun verb predicate verb verb

the dog verb the dog walks

The Resulting Language


L = { a cat runs, a cat walks, the cat runs, the cat walks, a dog runs, a dog walks, the dog runs, the dog walks }

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:

Example Grammar Notation


S aSb S

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 }

Deriving Strings in the Grammar


Grammar:

Sentential Form
A sentence that contains variables and terminals

S aSb S
Derivation of sentence

aabb : S aSb aaSbb aabb


S aSb

S aSb aaSbb aaaSbbb aaabbb


Sentential Forms sentence

General Notation for Derivations


In general we write: If:

Why Notation Is Useful


We can now write: *

w1 wn

w1 w2 w3 wn

S aaabbb

It is always the case that:

w w

Instead of:

S aSb aaSbb aaaSbbb aaabbb

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

S aSb aaSbb aaaSbbb aaabbb

S aSb aaSbb aaaSbbb aaaaSbbbb aaaabbbb


What language is being described?

The Resulting Language


S aSb S
Always add on a and b on each side resulting in: as at the left bs at the right equal number of as and bs
The image cannot be displayed. Your computer may not have enough memory to open the image,

Linear Grammars
Grammars with at most one variable at the right side of a production

Examples:

S aSb S

A Non-Linear Grammar
Grammar

Another 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

A regular grammar is any right-linear or left-linear grammar Examples:

S abS S a

What languages are generated by these grammars?

S Aab A Aab | B Ba

Languages and Grammars


S abS S a
L(G1 ) = (ab) * a

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??

S Aab A Aab | B Ba L(G2 ) = aab(ab) *

Regular Grammars Generate Regular Languages

Theorem
Languages Generated by Regular Grammars

Theorem - Part 1
Regular Languages Languages Generated by Regular Grammars Regular Languages

Any regular grammar generates a regular language

Languages Generated by Regular Grammars

Proof Part 1
Regular Languages

The case of Right-Linear Grammars


Let

Gbe a right-linear grammar


is L(G )regular

We will prove:

The language L(G ) generated by any regular grammar G is regular

Proof idea: We will construct NFA using the grammar transitions

Example
Given right linear grammar:

Step 1: Create States for Each Variable


Construct NFA M such that every state is a grammar variable:

Step 2.1: Edges for Productions


Productions of the form result in

Productions of the form are only slightly harder. Create row of states that derive w and end in

Step 2.2: Edges for Productions

Productions of the form Create row of states that derive w and end in a final state

Step 2.3: Edges for Productions

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

Any regular language L is generated by some regular grammar G

Proof idea: Any regular language L is generated by some regular grammar G Let

M be the NFA with L = L(M ).

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

NFA to Grammar Example


Since

Step 1: Convert Edges to Productions


b

L is regular there is an NFA


b

q0

q1

q0 aq1
q2
b

M
q0

q1 bq1 q1 aq2 q2 bq3

q1

q2
b

This transition in the NFA Looks a lot like a production rule

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

Edges and Final States b

M
q0

q1

q1 bq1 q1 aq2 q2 bq3 q3 q1 q3

M
q0

q1

q2
b

q3

q3
If

is a final state, add

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

But What About Left-Linear Grammars


What happens if we reverse a left linear grammar as follows: Reverses to Reverses to The result is a right linear grammar.
If the left linear grammar produced L, then what does the resulting right linear grammar produce?

Every regular language has an NFA


Can convert that NFA into a right linear grammar Thus every regular language has a right linear grammar

Combined with Part 1, we have shown right linear grammars are yet another way to describe regular languages

11

But What About Left-Linear Grammars


The previous slide reversed the language! Reverses to Reverses to If the left linear grammar produced language , then the resulting right linear grammar produces Claim we just proved left linear grammars produce regular languages? Why?

Left-Linear Grammars Produce Regular Languages


Start with a Left Linear grammar that produces want to show is regular Can produce a right linear grammar that produces All right linear grammars produce regular languages so is a regular language The reverse of a regular language is regular so is a regular language!

For regular languages we will prove that: Union: Concatenation: Star: Reversal: Complement: Intersection:

L1 and L2

We say: Regular languages are closed under Union: Concatenation:

L1 L2 L1L2 L1 *
Are regular Languages

L1 L2 L1L2 L1 *

Star: Reversal: Complement: Intersection:

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

Single final state

Single final state

Union
NFA for

Example
NFA for

L1 L2

M1

L1 L2 = {a nb} {ba} L1 = {a nb}


a
b

M2

L2 = {ba}
b

13

Concatenation
NFA for

Example
NFA for

L1L2 = {a nb}{ba} = {a nbba}

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

Next Lecture Topics from Chapter 4.2 and 4.3


Properties of regular languages The pumping lemma (for regular languages)

L1 L2
L1 L2 L1 L2

Quiz 1 in Recitation on Wednesday 9/17


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. Quiz will take the full hour on Wednesday

Homework
Homework Due Thursday

16

Potrebbero piacerti anche