1 / 18

Understanding Regular Grammar and Finite State Automata in Prolog

This lecture covers the fundamentals of regular grammars and finite state automata (FSA), including their correspondence and how to express languages using these concepts in Prolog. Students will learn to write regular grammars based on specified languages, analyze test cases, and convert FSAs into regular expressions. The lecture also includes practical assignments to enhance student understanding through implementation in Prolog and Perl. Homework will be assigned to apply these concepts and demonstrate proficiency.

taipa
Download Presentation

Understanding Regular Grammar and Finite State Automata in Prolog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 (11th October)

  3. XKCD Acknowledgement: Erwin Chan

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

  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. b a b b ε > 1 2 3 4 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], b. • s --> [b], s. • b --> [a], c. • c --> [b]. • c --> [b], b. • c --> [b], c. a b [Powerpoint animation] in order of the RG rules b b b > s b c e b

  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 • s --> []. • s --> [b], b. • s --> [b], s. • b --> [a], c. • c --> [b]. • c --> [b], b. • 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 • s --> []. • s --> [b], s. • s --> [b], b. • b --> [a], c. • c --> [b]. • c --> [b], b. • 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 • s --> []. • s --> [b], b. • s --> [b], s. • b --> [a], c. • c --> [b]. • c --> [b], b. • 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 > 0 1 2 1 1 0 3 1

  15. Converting FSA to REs • eliminate state 3 • eliminate state 2 [Powerpoint animation] 1 1 1 > 1 > 0 1 2 0(1+0|1)*1+1 1+1 1+0 0(1+0|1)*1+1 | 1 > 1 (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 = L1∪ L2 • L1 = a*b* • L2 = (ab)* • Question 1 (438/538) 15 points • Part 1 • Give a regular grammar for L1 in Prolog • Part 2 • Give a regular grammar for L2 in Prolog • Part 3 • Give a regular grammar for L (=L1∪ L2) 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 = L1∪ L2 • L1 = a*b* • L2 = (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. • aabbaababa • *b*ab*aabbbb*aa (* indicates string not in the language)

More Related