Sunteți pe pagina 1din 50

DVA337 HT17 - LECTURE 9 Context-free languages,

ambiguity and derivation trees

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

Tells of a certain regular structure on infinite regular languages

All infinite regular languages have this structure

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

Push-Down Automata, PDA

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*

A grammar is left-linear if it is linear, and the non-terminal occurs in the leftmost


position (analogously for right-linear)
 A → Bu | u, for u ∈ 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
α→β

where w ∈ T*, α, β ∈ (V ∪ T)*, γ ∈ (V ∪ T)+

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→λ

Which type of grammar is it?

Which language does it define?

13
EXERCISE
Show that L = { w : w ∈ T*, |w|a = |w|b, u ∈ prefix(w) . |u|a ≥ |u|b } is not regular

What does the fact that L is not regular imply?

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

Derivation – sequence of derivation steps

Context-free grammars work by replacing one non-terminal in each step of


derivation

17
DERIVATIONS
Each step in a derivation contains information about
 which production rule was used, and
 where it was used

In linear grammars there is always only one non-terminal to replace

18
DERIVATION ORDER
For context-free grammas in general, there can be a choice
 S → AB
 A → aaA | λ
 B → Bb | λ

The derivations are equivalent, but the order differs


 left most, always the left non-terminal
 right most, always the right non-terminal

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

For linear grammars, there exists only one derivation order


 more than one derivation of one string implies that a linear grammar is ambiguous

For context-free grammars, there exists many different equivalent derivations


 more than one derivation of one string does not imply that a context-free grammar is ambiguous
20
EXERCISE
Consider G given by
 S → AB
 A → aaA | λ
 B → Bb | λ

Is the derivation of abb unique?

Is G ambiguous?

21
EXERCISE
Consider G given by
E→E+E|E*E|(E)|n∈ℕ

Is the derivation of 1 + 2 * 3 unique?

Is G ambiguous?

22
WHAT IS THE DIFFERENCE?
S → AB E→E+E|E*E|(E)|n∈ℕ
A → aaA | λ
B → Bb | λ

Derivations of aab Derivations of 1 + 2 * 3

 S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab E⇒E+E⇒1+E⇒1+E*E⇒1+2*E⇒1+2*3

 S ⇒ AB ⇒ ABb ⇒ Ab ⇒ aaAb ⇒ aab E⇒E*E⇒E+E*E⇒1+E*E⇒1+2*E⇒1+2*3

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

Write the following derivation as a tree


 S ⇒ AB ⇒ aaAB ⇒ aaABb ⇒ aaBb ⇒ aab

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∈ℕ

Draw the derivation trees corresponding to

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

Consider the derivation of aaabbb in


 S → aSb | λ

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

corresponding to the string.

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

Each transition adds interaction with


stack

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

S-ar putea să vă placă și