Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong.

Similar presentations


Presentation on theme: "LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong."— Presentation transcript:

1 LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong

2 Administrivia Homework 3 out today – Usual rules, due next Monday (11 th October)

3 XKCD Acknowledgement: Erwin Chan

4 Let’s write a regular grammar Example from Lecture 10 Regular expression: b+(ab+)* | ε 1 1 2 2 3 3 4 4 > b ab b b ε FSA:

5 Let’s write a regular grammar Test cases (* denotes string not in the language): – *ab *ba [a,b][b,a] – bab [b,a,b] – λ (empty string) [] – bb [b,b] – *baba [b,a,b,a] – babab [b,a,b,a,b]

6 Let’s write a regular grammar Regular Grammar in Prolog notation: –s --> []. ( s = ”start state”) –s --> [b], b. ( b = ”seen a b ”) –s --> [b], s. –b --> [a], c. ( c = ”expect a b ”) –c --> [b]. –c --> [b], b. –c --> [b], c.

7 Let’s write a regular grammar Compare the FSA with our Regular Grammar (RG) –s --> []. ( s = ”start state”) –s --> [b], b. ( b = ”seen a b ”) –s --> [b], s. –b --> [a], c. ( c = ”expect a b ”) –c --> [b]. –c --> [b], b. –c --> [b], c. 1 1 2 2 3 3 4 4 > b ab b b ε There is a straightforward correspondence between right recursive RGs and FSA

8 RG to FSA Informally, we can convert RG to a FSA – by treating – non-terminals as states – and introducing (new) states for rules of the form x --> [a]. s s > b c c a b b b e e b b b 1.s --> []. 2.s --> [b], b. 3.s --> [b], s. 4.b --> [a], c. 5.c --> [b]. 6.c --> [b], b. 7.c --> [b], c. 1.s --> []. 2.s --> [b], b. 3.s --> [b], s. 4.b --> [a], c. 5.c --> [b]. 6.c --> [b], b. 7.c --> [b], c. [Powerpoint animation] in order of the RG rules

9 Let’s write a regular grammar Test cases (* denotes string not in the language): – *ab *ba [a,b][b,a] – bab [b,a,b] – λ (empty string) [] – bb [b,b] – *baba [b,a,b,a] – babab [b,a,b,a,b] Output: Licensed to SP4arizona.edu [g]. % compiling /Users/sandiway/Desktop/g.pl... % compiled /Users/sandiway/Desktop/g.pl in module user, 0 msec -16 bytes yes | ?- s([a,b],[]). no | ?- s([b,a],[]). no | ?- s([b,a,b],[]). yes | ?- s([],[]). yes | ?- s([b,b],[]). yes | ?- s([b,a,b,a],[]). no | ?- s([b,a,b,a,b],[]). yes

10 Set Enumeration using Prolog Regular Grammar 1.s --> []. 2.s --> [b], b. 3.s --> [b], s. 4.b --> [a], c. 5.c --> [b]. 6.c --> [b], b. 7.c --> [b], c. Normally, we ask the set membership question when posing a Prolog query: – e.g. ?- s([a,b],[]). no Prolog enumeration: ?- s(X,[]). –X is a Prolog variable – asks the question for what values of X is s(X,[]) true? –; is disjunction (look for alternative answers) why? Prolog matches rules in the order in which they’re written

11 Set Enumeration using Prolog Let’s swap rules 2 and 3 Regular Grammar 1.s --> []. 2.s --> [b], s. 3.s --> [b], b. 4.b --> [a], c. 5.c --> [b]. 6.c --> [b], b. 7.c --> [b], c. Prolog enumeration: ?- s(X,[]).

12 Set Enumeration using Prolog Similarly, if we swap rules 6 and 7 Regular Grammar –s --> []. –s --> [b], b. –s --> [b], s. –b --> [a], c. –c --> [b]. –c --> [b], c. –c --> [b], b. Prolog enumeration: ?- s(X,[]).

13 Set Enumeration using Prolog Regular Grammar 1.s --> []. 2.s --> [b], b. 3.s --> [b], s. 4.b --> [a], c. 5.c --> [b]. 6.c --> [b], b. 7.c --> [b], c. Unfortunately, the default Prolog grammar rule matching convention does not permit us to properly enumerate the set (language). Enumeration (in order of size): – Length: 0 –[] – Length: 1 –[b] – Length: 2 –[b,b] – Length: 3 –[b,b,b][b,a,b] – Length: 4 –[b,b,b,b][b,b,a,b] –[b,a,b,b] – Length: 5 –[b,b,b,b,b] [b,b,b,a,b] –[b,b,a,b,b] [b,a,b,b,b] –[b,a,b,a,b] – etc.

14 Converting FSA to REs Example: – Give a RE for the FSA: State by-pass method: – Delete one state at a time – Calculate the possible paths passing through the deleted state – E.g. eliminate state 3 then 2… 1 1 2 2 3 3 > 1 0 1 1 1 1 0

15 Converting FSA to REs eliminate state 3 eliminate state 2 1 1 > 2 2 1 0 1 1 1 > 1 1+01+0 1+11+1 0(1 + 0|1)*1 + 1 1 1 > 0(1 + 0|1)*1 + 1 | 1 [Powerpoint animation] (0(1 + 0|1)*1 + 1 | 1)*

16 Homework 3 Consider the language L given by – L = a*b*  (ab)* We can express this as: – L = L 1  L 2 – L 1 = a*b* – L 2 = (ab)* Question 1 (438/538) 15 points Part 1 – Give a regular grammar for L 1 in Prolog Part 2 – Give a regular grammar for L 2 in Prolog Part 3 – Give a regular grammar for L (=L 1  L 2 ) in Prolog Instruction: – In each case show your program and sample output

17 Homework 3 Consider the language L given by – L = a*b*  (ab)* We can express this as: – L = L 1  L 2 – L 1 = a*b* – L 2 = (ab)* Question 2 (438/538) 15 points Give a deterministic FSA for L – Draw a diagram – Implement it in Perl – show your program and sample output

18 Homework 3 Question 3 (538 obligatory, 438 extra credit) 15 points – Give a FSA that accepts strings of a’s and b’s (in any order) such that the total number of a’s in the string must be even and the total number of b’s in string must be odd e.g. – aab baababa – *b *ab *aabbbb *aa (* indicates string not in the language)


Download ppt "LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong."

Similar presentations


Ads by Google