LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM:...

19
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong

Transcript of LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM:...

Page 1: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

LING/C SC/PSYC 438/538

Lecture 13Sandiway Fong

Page 2: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Administrivia

• Reading Homework– Chapter 3 of JM: Words and Transducers

Page 3: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Today Topics

• Left recursive regular grammars

• The limits of regular grammars (RG), FSA and Res

Page 4: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Left Recursion and Set Enumeration

• Example:s --> a, [!].a --> ba, [a]. a --> a, [a].ba --> b, [a].b --> [b].

• Grammar is:– a regular grammar– left recursive

• Question– What is the language

of this grammar?

• Answer: Sheeptalk !• Sentential forms:

• s• a!• baa!• baa!• baa!

Underscoring used here to indicate a nonterminal

Page 5: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Left Recursion and Set Enumeration

• Example:s --> a, [!].a --> ba, [a]. a --> a, [a].ba --> b, [a].b --> [b].

• Prolog query:?- s([b,a,a,!],[]).Yes?- s([b,a,a,a,a,!],[]).yes

• But it doesn’t halt when faced with a string not in the language

?- s([b,a,!],[]).! Resource error: insufficient

memory

Page 6: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Left Recursion and Set Enumeration

• Example:1. s --> a, [!].2. a --> ba, [a]. 3. a --> a, [a].4. ba --> b, [a].5. b --> [b].

– ?- s([b,a,!],[]).– ! Resource error: insufficient memory

• Why?

Page 7: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Left Recursion and Set Enumeration

• left recursive regular grammar:1. s --> a, [!].2. a --> ba, [a]. 3. a --> a, [a].4. ba --> b, [a].5. b --> [b].

• Behavior– halts when presented with a string that is in the language– doesn’t halt when faced with a string not in the language– unable to decide the language membership question

• Surprisingly, the query:?- s(L,[]).

• enumerates the strings in the language just fine.

Page 8: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Left Recursion and Set Enumeration

• left recursive regular grammar:1. s --> a, [!].2. a --> ba, [a]. 3. a --> a, [a].4. ba --> b, [a].5. b --> [b].

• Behavior– halts when presented with a string that is in the

language– doesn’t halt when faced with a string not in the language

• derivation tree for?- s(L,[]).

s

a

ba

b

b

a

a

!

s

a

a

ba

b

b

a

a

a

!

and so on

L = L = [b|..] L = [b,a|..] L = [b,a,a|..] L = [b,a,a,!]

[Powerpointanimation]

Choice point

Page 9: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Left Recursion and Set Enumeration

• However, this slightly re-ordered left recursive regular grammar:1. s --> a, [!].2. a --> a, [a].3. a --> ba, [a]. 4. ba --> b, [a].5. b --> [b].

• won’t halt when enumerating

• Why?s

a

a

a

a... descends

infinitely

using rule 2

[Powerpointanimation]

Page 10: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Beyond Regular Languages

• Beyond regular languages– anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n≥1– is not a regular language

• That means no FSA, RE or RG can be built for this set

• Informally, let’s think about a FSA implementation …

1. We only have a finite number of states to play with …2. We’re only allowed simple free iteration (looping)

Page 11: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Beyond Regular Languages

• Language– anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1

• A regular grammar extended to allow both left and right recursive rules can accept/generate it:1. a --> [a], b.2. b --> [b].3. b --> a, [b].

• Example:Set membership

Set enumeration

Page 12: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Beyond Regular Languages

• Language– anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1

• A regular grammar extended to allow both left and right recursive rules can accept/generate it:1. a --> [a], b.2. b --> [b].3. b --> a, [b].

• Intuition:– grammar implements the

stacking of partial trees balanced for a’s and b’s:

B

A

b

a

A

B

A

b

a

Page 13: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

Beyond Regular Languages

• Language– anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1

• A regular grammar extended to allow both left and right recursive rules can accept/generate it:1. a --> [a], b.2. b --> [b].3. b --> a, [b].

• A context-free grammar (CFG) (aka type-2) has no restrictions on what can go on the RHS of a grammar rule

• Note:– CFGs still have a single

nonterminal limit for the LHS of a rule

• Example:1. s --> [a], [b].2. s --> [a], s, [b].

Page 14: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

A Formal Tool: The Pumping Lemma

• A tool used to prove languages are NOT regular– based on the intuition that if a language is regular

and non-trivial (infinite), – it “pumps” (iterates freely)

– Recall: for any given # states, if the (accepted) input string is long enough, you’ll need to loop

• Note: – can’t be used to prove a language is regular– to show something is regular:

• proof by exhibition• describe a FSA

(or regular expression or regular grammar)

Page 15: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

A Formal Tool: The Pumping Lemma

[See also discussion in JM 16.2.1, pages 533–534]

• If a language L is regular, then there exists a number p > 0 – where p is a pumping length– (sometimes called a magic number)

such that every string w in L with |w| ≥ p can be written in the following form

w = xyz• with strings x, y and z such that |xy| ≤ p, |y| > 0 and

xy i z is in L • for every integer i ≥ 0.

Page 16: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

A Formal Tool: The Pumping Lemma

• Example:– show that anbn is not regular

• Proof (by contradiction):– pick a sufficiently long string in the language– e.g. a..aab..bb (#a’s = #b’s)– Partition it according to w = xyz– then show xy i z is not in L – i.e. string does not pump

Page 17: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

A Formal Tool: The Pumping Lemma

aaaa..aabbbb..bb

Case 1: w = xyz, y straddles the ab boundarywhat happens when we pump y?

yy y

Case 2: w = xyz, y is wholly within the a’swhat happens when we pump y?

Case 3: w = xyz, y is wholly within the b’swhat happens when we pump y?

Page 18: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

A Formal Tool: The Pumping Lemma

• Prime number testing (from lecture 9)• prime number testing using Perl’s so-called “regular

expressions”• Using unary notation, e.g. 5 = “11111”• /^(11+?)\1+$/ will match anything that’s greater than 1

that’s not prime • Key to making this work: backreference \1

L = {1n | n is prime} is not a regular language

Page 19: LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.

A Formal Tool: The Pumping Lemma

1n = 111..1111..11111 such that n is a prime number

yx z

Pump y such that i = length(x+z), giving yi

What is the length of string w=xyiz now?

The resulting length is non-prime since it can be factorized