0% found this document useful (0 votes)
120 views51 pages

DVA337 HT17 Lecture7 PDF

The document discusses regular grammars and their relationship to regular languages. It begins by defining grammars, linear grammars, right linear grammars, left linear grammars, and regular grammars. It then proves that regular grammars produce regular languages by showing that both right linear grammars and left linear grammars produce regular languages. For right linear grammars, it constructs a corresponding NFA that accepts the same language. For left linear grammars, it shows how to transform them into equivalent right linear grammars, and uses the fact that regular languages are closed under reversal.

Uploaded by

Kamal Walia
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)
120 views51 pages

DVA337 HT17 Lecture7 PDF

The document discusses regular grammars and their relationship to regular languages. It begins by defining grammars, linear grammars, right linear grammars, left linear grammars, and regular grammars. It then proves that regular grammars produce regular languages by showing that both right linear grammars and left linear grammars produce regular languages. For right linear grammars, it constructs a corresponding NFA that accepts the same language. For left linear grammars, it shows how to transform them into equivalent right linear grammars, and uses the fact that regular languages are closed under reversal.

Uploaded by

Kamal Walia
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/ 51

DVA337 – LECTURE 7 Definition of grammars, linear

and regular grammars.

1
RECAP OF LECTURE 6
by set lifting of δ

subset construction

DFA NFA

state elimination

RE Thompson’s construction

(subset construction) nfa-to-dfa implied:


(state elimination) nfa-to-re dfa-to-re(x) = nfa-to-re(dfa)
(Thompson) re-to-nfa re-to-dfa(x) = nfa-to-dfa(re-to-nfa(x))
2
EXERCISE, STATE ELIMINATION
Construct a regular expression for the
following automaton
 Create unique isolated start and final states
 Successively eliminate states, introducing and
joining transition between predecessors and
successors
 until only the start and final states remain

3
TODAY
Grammars
 linear grammars (LG)
 right and left linear grammars (RLG and LLG)

Linear grammars
 right and left linear grammars define regular languages (RLG → NFA, LLG in other way)
 each regular language can be represented as a right linear grammar (DFA → RLG)

Regular grammar
 left linear or right linear
 defines regular languages

4
GRAMMARS examples and definitions

5
EXAMPLE, GRAMMAR
Grammars express languages, e.g., a
subset of the English language

sentence  noun _ phrase predicate


noun _ phrase  article noun
predicate  verb
article  a | the
noun  bird | dog
verb  sings | barks

6
EXERCISE, GRAMMAR
Derive ‘a dog barks’

sentence  noun _ phrase predicate


noun _ phrase  article noun
predicate  verb
article  a | the
noun  bird | dog
verb  sings | barks

7
THE LANGUAGE DEFINED BY A GRAMMAR
The language of a grammar is the set of L={ “a bird barks”,
strings that can be derived.
“a bird sings”,
sentence  noun _ phrase predicate “the bird barks”,
noun _ phrase  article noun “the bird sings”,
predicate  verb “a dog barks”,
article  a | the
“a dog sings”,
noun  bird | dog
“the dog barks”,
verb  sings | barks
“the dog sings” }

8
NOTATION
S  aSb
S 

9
EXERCISE
Derive ab using the grammar

S  aSb
S 

10
EXERCISE, LANGUAGE OF GRAMMAR
Which is the language of the grammar

S  aSb
S 

11
S  aSb
FORMAL DEFINITION S 

A grammar, G, is a four tuple (V,T,S,P), where

V is a set of non-terminals
T is a set of terminals
S is the start non-terminal
P is a set of production rules

12
DERIVATIONS
Sentence – a string of terminals

Sentential form – a string of terminals and non-terminals


 a sentence is a special case of a sentential form

S  aSb  aaSbb  aaaSbbb  aaabbb


13
S  aSb
SET DEFINITION OF P? S 

A grammar, G, is a four tuple (V,T,S,P), where

V is a set of non-terminals
T is a set of terminals
S is the start non-terminal
P is a set of production rules

14
DERIVATION STEPS AS A RELATION
How can we define ⇒ as a relation?

S  aSb  aaSbb  aaaSbbb  aaabbb


15
S  aSb  aaSbb
 aaaSbbb  aaabbb
DERIVATIONS AS RELATIONS
Define ⇒* to be zero or more derivation
steps

⇒* is the reflexive transitive closure of ⇒

16
EXERCISE
What derivations are possible given

S  Ab
A  aAb
A

17
LANGUAGE OF A GRAMMAR
The language of a grammar G = (V, T, S, P) are all derivable sentences

18
EXERCISE
What is the language defined by

S  Ab
A  aAb
A

19
LINEAR GRAMMARS
20
LINEAR GRAMMARS
In general, the production rules form a relation on sentential forms

A context free grammar is a grammar, where all production rules are restricted to a
single non-terminal on the left hand side

A linear grammar is a context free grammar with at most one non-terminal on the right
hand side

21
EXAMPLE
Are the following grammars context free or linear?

S  aSb S  Ab S  SS
S  A  aAb S 
A S  aSb
S  bSa
22
EXERCISE
What language does the grammar define?

S  SS
S 
S  aSb
S  bSa

23
EXERCISE
What type of grammar is the following and what language does it define?

SA
A  aB | 
B  Ab

24
LEFT AND RIGHT-LINEAR GRAMMARS
Based on the definition of a linear grammar

what could a left-linear, and right-linear grammar be?

25
EXERCISE
Create a right-linear grammar for the language
L = { w : w ∈ {0,1}*, w contains the substring 00 or 01}

26
REGULAR GRAMMARS
27
REGULAR GRAMMARS

A grammar is a regular grammar if and only if it is right-linear or left-linear.

Theorem: The regular grammars define exactly the regular languages.


Proof:

28
RG PRODUCE RL
29
ARGUMENT OUTLINE
All regular grammars produce regular languages
 must show that RLG and LLG produce regular languages
 different types of proof

All right linear grammars produce regular languages


 by construction; construct an NFA from an RLG accepting the same language

All left linear grammars produce regular language


 translate an LLG into an RLG that accepts the reversed language
 use that regular languages are closed under reversal (i.e., a the reversal of a RL is a RL)

30
PROOF – RIGHT LINEAR GRAMMAR
Lemma 1: ∀G ∈ RLG ∃M ∈ NFA . L(G) = L(M)
Proof: By construction. Two parts 1) show how to create NFA, M, from RLG, G and 2)
show that L(G) = L(M).

31
SIMPLIFYING ASSUMPTION
WLOG, assume all rules are on the form
 Ai → aiAj, or
 A i → ai

Why is this possible?

32
DERIVATIONS OF A RLG
Given RLG with rules on the form
 Ai → aiAj, or
 A i → ai

Consider the form of derivations of some string w

33
GENERAL ALGORITHM
For a right-linear grammar (V, T, S, P) let
 the states Q = V ∪ { Vf }
 the alphabet Σ = T
 the start state q0 = S
 the final states F = { Vf }

δ is then defined by the productions P


 A → aB gives transition from A to B labeled a
 A → a gives transition from A to Vf labeled a

34
EXAMPLE, CREATE NFA
Consider a right-linear grammar

S  aA | B
A  aa B
Bb B|a

35
EXERCISE
Create an NFA for the following grammar

V0  a1V1 | a3V2
V1  a2 a4V3 | a3a4 a8V4
V2  a5V4
V3  a9V1 | a5
V4  a0

36
CORRECTNESS OF THE TRANSLATION
How can we argue L(G) = L(M), where M is the NFA obtained from G as shown?

Must show that


 any derived string can be simulated by the NFA, i.e. ∀w ∈ Σ* . S ⇒* w → Vf ∈ δ*(S, w)

 all strings accepted by the NFA can be derived ∀w ∈ Σ* . Vf ∈ δ*(S, w) → S ⇒* w

Check the book for an argument for why this is the case

37
PROOF – LEFT LINEAR GRAMMARS
Lemma 2: ∀G ∈ LLG . L(G) is regular
Proof:

38
LLG TO RLG
How can a left-linear grammar with productions on the form
 A → Bu and A → u

be turned into a right-linear grammar


 A → uB and A → u

39
EXERCISE
Make the following grammar left linear

S  aA | B
A  aa B
Bb B|a

40
CLOSEDNESS OF RL UNDER REVERSAL
The reversal of a regular language is a regular language
 ∀ L ∈ RL . LR ∈ RL

Can you prove this?

41
COROLLARY, RG PRODUCE RL
Regular grammars produce regular languages
 RG are either RLG or LLG
 RLG produce RL by Lemma 1
 LLG produce RL by Lemma 2

42
EXERCISE
Construct the NFA for the following grammar

S → 0S | 1S | 1T | 0V
T → 1U
V → 0U
U → 0U | 1U | λ

43
ALL RL CAN BE DESCRIBED BY RG
44
ALL RL CAN BE DESCRIBED BY RG
A regular language is a language that
 can be described by an NFA
 can be described by an DFA
 can be described by a RE

A regular grammar is a grammar that is either


 right linear, or
 left linear

45
PROOF - PART 2
Lemma 2: ∀M ∈ DFA ∃G ∈ RL . L(M) = L(G)
Proof: By construction. Given M = (Q, Σ, δ, q0, F) construct G = (Q, Σ, q0,P), where
P is given by
 δ(q1,a) = q2 gives q1 → aq2 ∈ P
 q ∈ F gives q → λ ∈ P

46
EXAMPLE
Create a grammar for the following NFA
 what is L(M)?
b
M a
a
q0 q1 q2

 b
q3
47
EXERCISE, REGULAR GRAMMARS
What are the regular expressions corresponding to the following grammars?

S  abS S  Aab S  SS
S a A  Aab | B S 
Ba S  aSb
S  bSa
48
SUMMARY
Grammars
 non-terminal, terminals, production rules, derivations

Regular grammars
 left linear and right linear
 RLG define RL (by translating into NFA)
 LLG define RL (by translating into RLG defining reverse of language, RL closed under reverse)
 RL can be defined by RG (by translating DFA into RLG)

Conclusion: regular grammars define exactly the regular languages


 DFA  NFA  RE  RG

49
THE CHOMSKY HIERARCHY
50
CHOMSKY’S LANGUAGE HIERARCHY
We will further differentiate the regular languages

Non-regular languages

Regular Languages

51

You might also like