Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Today's Topics Homework 7 Review We will start to write grammars today
Quick Homework 8: install SWI-Prolog for next time (no need to submit anything)

3 Homework 8: Install SWI Prolog
Install on your laptop (Mac or PC0: prolog.org/build/Debian.html sudo apt-add-repository ppa:swi-prolog/stable sudo apt-get update sudo apt-get install swi-prolog

4 Homework 8: Install SWI Prolog
Mac problems: option-click on application

5 Homework 8: Install SWI Prolog
Alternative to the OSX application port install swi-prolog Yet another one is Homebrew

6 Homework 7 Review Question 1: convert this 4-state NDFSA into a deterministic FSA How many states does the DFSA have? Σ = {0, 1} double circle = final state initial state = A comma = disjunction

7 Homework 7 Review Question 1: this 4-state NDFSA = an 8-state deterministic FSA

8 Homework 7 Review Question 2: Find a regex for this machine
Step 1: eliminate state D

9 Homework 7 Review (01)*01?([01]1*01)*([01]1*01(01)*01?([01]1*01)*)*
Step 2a: eliminate state B: (01)*01?([01]1*01)*([01]1*01(01)*01?([01]1*01)*)* Step 2b: eliminate state C: (0|0[01]1*0)(1[01]1*0)*(1(0|0[01]1*0)(1[01]1*0)*)*

10 Homework 7 Review Question 3: implement the DFSA in Perl or Python

11 Homework 7 Review

12 Homework 7 Review 1: : : : : : : : : : : : : : : : : : : : : : : 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: ling538-18$ python3 hw7-3.py

13 Homework 7 Review Question 4: implement the regex in Perl or Python
NO_B NO_C

14 Homework 7 Review

15 Homework 7 Review

16 Homework 7 Review for i in sigma/sigma[0-2][0-9].txt; do python3 hw7-3f.py $i; done for i in sigma/sigma[0-2][0-9].txt; do perl hw7- 4f.perl $i; done 2: 1 16384: 1450 4: 1 32768: 2578 8: 2 65536: 4585 16: 5 131072: 8155 32: 8 262144: 14503 64: 14 524288: 25793 128: 26 : 45873 256: 46 : 81584 512: 81 : 1024: 145 : 2048: 258 : 4096: 458 : 8192: 815 2: 1 16384: 1450 4: 1 32768: 2578 8: 2 65536: 4585 16: 5 131072: 8155 32: 8 262144: 14503 64: 14 524288: 25793 128: 26 : 45873 256: 46 : 81584 512: 81 : 1024: 145 : 2048: 258 : 4096: 458 : 8192: 815 real 2m50.567s user 2m47.246s sys 0m3.196s real 2m54.513s user 2m52.142s sys 0m2.204s

17 regexper.com

18 Homework 7 Review Question 5: based on your testing, can you succinctly describe the language accepted in English? Begins with a zero No more than three zeros in a row

19 Control-D (EOF) to terminate.
SWI Prolog every command ends in a period case sensitive: variables begin with an uppercase letter Control-D (EOF) to terminate. halt.

20 SWI Prolog Cheatsheet At the prompt ?- Anytime Everything typed at the
prompt must end in a period. At the prompt ?- halt. listing. listing(name). more useful [filename]. loads filename.pl trace (step through derivation, hit return) notrace. debug. nodebug. turn off debugger spy(name). spy on predicate name pwd print working directory working_directory(_,Y). switch directories to Y Anytime ^C (then a(bort) or h(elp) for other options)

21 Example Learn Prolog Now! Notation: Data structures: \+ negation
, conjunction ; disjunction :- if Facts: predicate(arguments). Rules: p(Args) :- q(Args) ,.., r(Args). Data structures: list: [a,..b] empty list: [] head/tail: [h|List] Atom: name, number Term: functor(arguments) arguments: comma-separated terms/atoms Learn Prolog Now! P. Blackburn, J. Bos & K. Striegnitz

22 Prolog Recursion Example (factorial): In Prolog:
0! = 1 n! = n * (n-1)! for n>0 In Prolog: factorial(0,1). factorial(N,NF) :- M is N-1, factorial(M,MF), NF is N * MF. Problem: infinite loop Fix: 2nd case only applies to numbers > 0 factorial(N,NF) :- N>0, M is N-1, factorial(M,MF), NF is N * MF. Prolog built-in: X is <math expression> Run ?- factorial(5,X). (hit ; for more answers)

23 Prolog Recursion Example: Σ={0,1}, enumerate Σ* sigma(0). sigma(1).
Example: Σ={0,1}, enumerate Σ* sigma(0). sigma(1). sigmas([]). sigmas([X|L]) :- sigmas(L), sigma(X). Run (hit ; for more answers) ?- sigmas(L).


Download ppt "LING/C SC/PSYC 438/538 Lecture 19 Sandiway Fong."

Similar presentations


Ads by Google