Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers."— Presentation transcript:

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

2 Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers

3 Today Topics Left recursive regular grammars The limits of regular grammars (RG), FSA and Res

4 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! Underscoring used here to indicate a nonterminal Underscoring used here to indicate a nonterminal

5 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

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

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

8 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 b b a a a ! and so on L =L = [b|..] L = [b,a|..] L = [b,a,a|..]L = [b,a,a,!] [Powerpoint animation] Choice point

9 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 descends infinitely using rule 2 [Powerpoint animation]

10 Beyond Regular Languages Beyond regular languages – a n b n = {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) 1.We only have a finite number of states to play with … 2.We’re only allowed simple free iteration (looping)

11 Beyond Regular Languages Language – a n b n = {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

12 Beyond Regular Languages Language – a n b n = {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

13 Beyond Regular Languages Language – a n b n = {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].

14 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)

15 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.

16 A Formal Tool: The Pumping Lemma Example: – show that a n b n 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

17 A Formal Tool: The Pumping Lemma aaaa..aabbbb..bb Case 1: w = xyz, y straddles the ab boundary what happens when we pump y? yyy Case 2: w = xyz, y is wholly within the a’s what happens when we pump y? Case 3: w = xyz, y is wholly within the b’s what happens when we pump y?

18 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 = {1 n | n is prime} is not a regular language

19 A Formal Tool: The Pumping Lemma 1 n = 111..1111..11111 such that n is a prime number yxz Pump y such that i = length(x+z), giving y i What is the length of string w=xy i z now? Pump y such that i = length(x+z), giving y i What is the length of string w=xy i z now? The resulting length is non-prime since it can be factorized


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

Similar presentations


Ads by Google