Documente Academic
Documente Profesional
Documente Cultură
1
COURSE SO FAR
Regular languages
Regular Expressions, RE
Regular Grammars, RG; left linear and right linear
Deterministic Finite Accepter, DFA
Nondeterministic Finite Accepter, NFA
Equivalences
NFA to DFA, subset construction
RE to NFA, Thompson’s construction
NFA to RE, state elimination
DFA optimization, set partitioning
Properties of regular languages
closure over union, intersection, difference, repetition, reversal
decidability of membership problem, empty language problem, finiteness problem.
2
EXERCISE
Create a regular grammar corresponding to ab*+a*b
3
REVISITING THE PUMPING LEMMA
The pumping lemma says that you can pump strings over some cut-off length m
Pump(L) =
∃m ∀w . w ∈ L ∧ |w| ≥ m →
∃x,y,z . w = xyz ∧ |xy| ≤ m ∧ |y| ≥ 1 ∧ ∀i . xyiz ∈ L
4
PROVING NON REGULARITY BY CONTRADICTION
Pump(L) =
∃m ∀w . w ∈ L ∧ |w| ≥ m →
∃x,y,z . w = xyz ∧ |xy| ≤ m ∧ |y| ≥ 1 ∧ ∀i . xyiz ∈ L
To prove that a language is non-regular, prove that the language does not have this
regular structure – find a counter example; a string that cannot be pumped, i.e. prove
∃w . w ∈ L ∧ |w| ≥ m ∧ ∀x,y,z . (w = xyz ∧ |xy| ≤ m ∧ |y| ≥ 1 → ∃i . xyiz ∉ L)
From this we can easily derive a contradiction. Let wc be the string that cannot be
pumped.
5
Pump(L) =
∃m ∀w . w ∈ L ∧ |w| ≥ m →
∃x,y,z . w = xyz ∧ |xy| ≤ m ∧ |y| ≥ 1 ∧ ∀i . xyiz ∈ L
EXAMPLE
Prove that L = {anblcn+l : n, l ≥ 0 } is not regular.
By contradiction, assume Pump(L) and show that
∃w . w ∈ L ∧ |w| ≥ m ∧ ∀x,y,z . (w = xyz ∧ |xy| ≤ m ∧ |y| ≥ 1 → ∃i . xyiz ∉ L)
The contradiction follows
6
TODAY
Context-Free Languages
Context-Free Grammars
Ambiguity
Derivation Trees
7
CONTEXT-FREE LANGUAGES
8
CONTEXT FREE GRAMMAR
A grammar, G = (V,T,S,P), is context free if all productions are on the form
A → α for
A∈V
α ∈ (V ∪ T)*
A gramma is linear if it is context free and the right hand side contains at most one
non-terminal
A → uBv | u, for u,v ∈ T*
9
CHOMSKY HIERARCHY
Type 3: regular languages
A → wA | w
Type 2: context-free
A→α
Type 1: context-sensitive
αAβ → αγβ
Type 0: restriction-free
α→β
10
EXERCISE, REGULAR EXPRESSIONS
Write a grammar for the language of regular expressions over Σ = {a, b} and derive
aab*b
11
EXERCISE
Which language does the following grammar define?
S → aSa
S → bSb
S→λ
12
EXAMPLE
Consider the grammar
S → aSb
S → SS
S→λ
13
EXERCISE
Show that L = { w : w ∈ T*, |w|a = |w|b, u ∈ prefix(w) . |u|a ≥ |u|b } is not regular
14
DEFINITION
A language L is regular if and only if there exists a regular grammar that defines it
equivalently, there exists a DFA, NFA or RE
A language L is context free if and only if there exists a context-free grammar that
defines it
15
AMBIGUITY derivation order
16
DERIVATIONS, REVISITED
Derivation step – application of production rule to sentential form to produce new
sentential form or sentence
17
DERIVATIONS
Each step in a derivation contains information about
which production rule was used, and
where it was used
18
DERIVATION ORDER
For context-free grammas in general, there can be a choice
S → AB
A → aaA | λ
B → Bb | λ
19
AMBIGUITY
A grammar is ambiguous if there exists fundamentally different derivations of the
same string
not the same thing as different derivations, due to derivation order
Is G ambiguous?
21
EXERCISE
Consider G given by
E→E+E|E*E|(E)|n∈ℕ
Is G ambiguous?
22
WHAT IS THE DIFFERENCE?
S → AB E→E+E|E*E|(E)|n∈ℕ
A → aaA | λ
B → Bb | λ
23
DERIVATION TREES
24
DERIVATION TREES
How can we abstract away from the derivation order?
25
EXERCISE, DERIVATION TREES
For G given by
S → AB
A → aaA | λ
B → Bb | λ
26
DERIVATION TREES
Derivation trees abstract the derivation S
order
S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab
S ⇒ AB ⇒ ABb ⇒ Ab ⇒ aaAb ⇒ aab A B
a a A B b
27
EXERCISE, DERIVATION TREES
For G given by
E→E+E|E*E|(E)|n∈ℕ
E⇒E+E⇒1+E⇒1+E*E⇒1+2*E⇒1+2*3
E⇒E*E⇒E+E*E⇒1+E*E⇒1+2*E⇒1+2*3
28
LINEAR GRAMMARS, REVISITED
Why are linear grammars called linear?
29
AMBIGUITY of context free grammars
30
DEFINITION, AMBIGUITY
A context-free grammar G is ambiguous, when there exists a string, w ∈ L(G) with
two (or more) different derivation trees, or equivalently
two (or more) different left-most derivation, or equivalently
two (or more) different right-most derivations
31
WHY DO WE CARE ABOUT AMBIGUITY?
As for regular expressions – it depends on the application
32
PUSHDOWN AUTOMATA (PDA)
33
LIMITATIONS OF DFAS
What is the intuition for why no DFA can match L = { anbn : n ≥ 0 }
34
PUSHDOWN AUTOMATON, PDA
Input String
Stack
States
35
PDA
DFA + stack = DPDA
NFA + stack = NPDA q1 a, b / c q2
No random access – only top of stack
via push and pop
36
q1 a, / q2
input
a a
stack
b top b
h No Change
h
e e
$ $
37
q1 a , / c q2
input
a a
stack c
b top b
h Push
h
e e
$ $
38
q1 a,b / q2
input
a a
stack
b top
h Pop
h
e e
$ $
39
q1 a, b / c q2
input
a a
stack
b top c
h Replace
h
e e
$ $
(An alternative is to start and finish with empty stack)
40
EXAMPLE
Create a PDA for { anbn : n ≥ 0 }
41
Time 0
( ( ( ) ) )
Input Stack
(, / ( ), ( /
start ), (/ q end
s
42
Time 1
Input
( ( ( ) ) ) (
Stack
(, / ( ), ( /
start ), (/ q end
s
43
Time 2
Input (
( ( ( ) ) ) (
Stack
(, / ( ), ( /
start ), (/ q end
s
44
Time 3
Input
(
( ( ( ) ) ) (
(
Stack
(, / ( ), ( /
start ), (/ q end
s
45
Time 4
Input
( ( ( ) ) ) (
(
(
(, / ( ), ( / Stack
start ), (/ q end
s
46
Time 5
Input
(
( ( ( ) ) )
(
Stack
(, / ( ), ( /
start ), (/ q end
s
47
Time 6
Input
( ( ( ) ) ) (
Stack
(, / ( ), ( /
start ), (/ q end
s
48
Time 7
Input
( ( ( ) ) )
Stack
(, / ( ), ( /
start ), (/ q end
s
49
EXERCISE
Create a DPDA that recognizes the language L = { ucuR : u ∈ {a,b}* }
50