Académique Documents
Professionnel Documents
Culture Documents
1
RECAP OF LECTURE 6
by set lifting of δ
subset construction
DFA NFA
state elimination
RE Thompson’s construction
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
6
EXERCISE, GRAMMAR
Derive ‘a dog 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
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
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?
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?
SA
A aB |
B Ab
24
LEFT AND RIGHT-LINEAR GRAMMARS
Based on the definition of a linear grammar
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
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
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
32
DERIVATIONS OF A RLG
Given RLG with rules on the form
Ai → aiAj, or
A i → ai
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 }
34
EXAMPLE, CREATE NFA
Consider a right-linear grammar
S aA | B
A aa B
Bb 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?
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
39
EXERCISE
Make the following grammar left linear
S aA | B
A aa B
Bb B|a
40
CLOSEDNESS OF RL UNDER REVERSAL
The reversal of a regular language is a regular language
∀ L ∈ RL . LR ∈ RL
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
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
Ba 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)
49
THE CHOMSKY HIERARCHY
50
CHOMSKY’S LANGUAGE HIERARCHY
We will further differentiate the regular languages
Non-regular languages
Regular Languages
51