220 likes | 312 Views
LING/C SC/PSYC 438/538. Lecture 14 Sandiway Fong. Administrivia. Has everyone been able to install SWI Prolog? Today’s Topic: We switch to our new grammar programming language Prolog. SWI Prolog Useful Commands. Everything typed at the prompt must end in a period. At the prompt ?-
E N D
LING/C SC/PSYC 438/538 Lecture 14 Sandiway Fong
Administrivia • Has everyone been able to install SWI Prolog? Today’s Topic: • We switch to our new grammar programming language Prolog
SWI Prolog Useful Commands 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 predicates name • pwd. print working directory • working_directory(_,Y).switch directories • Anytime • ^C (then a(bort) or h(elp) for other options)
Example Formal grammar Prolog format S aBs--> [a],b. BaBb--> [a],b. BbCb--> [b],c. B bb--> [b]. CbCc--> [b],c. Cbc--> [b]. Notes: Start symbol: S Non-terminals: {S,B,C} (uppercase letters) Terminals: {a,b} (lowercase letters) Prolog format: both terminals and non-terminal symbols begin with lowercase letters variables begin with an uppercase letter (or underscore) --> is the rewrite symbol terminals are enclosed in square brackets (list notation) nonterminals don’t have square brackets surrounding them the comma (,: and) represents the concatenation symbol a period (.) is required at the end of every DCG rule Definite Clause Grammars (DCG)
Regular Grammars • Regular or Chomsky hierarchy type-3grammars • are a class of formal grammars with a restricted RHS • LHS → RHS “LHS rewrites/expands to RHS” • Canonical Forms: x --> y, [t].x --> [t]. (left linear/ recursive) or x --> [t], y.x --> [t]. (right linear/recursive) • where x and y are non-terminal symbols and • t (enclosed in square brackets) represents a terminal symbol. • Note: • can’t mix these two forms (and still have a regular grammar)! • can’t have both left and right recursive rules in the same grammar
Regular Grammars • What language does our regular grammar generate? • One or more a’s followed by one or more b’s • by writing the grammar in Prolog, • we have a ready-made recognizer program • no need to write a separate grammar rule interpreter (in this case) • Example queries • ?- s([a,a,b,b,b],[]).true • ?- s([a,b,a],[]).false • Note: • Query uses the start symbol s with two arguments: • (1) sequence (as a list) to be recognized and • (2) the empty list [] 1. s --> [a],b. 2. b--> [a],b. 3. b--> [b],c. 4. b --> [b]. 5. c --> [b],c. 6. c --> [b]. Prolog lists: In square brackets, separated by commas e.g. [a] [a,b,c]
Prolog lists • Perl lists: • @list = (“a”, “b”, “c”); • @list = qw(a b c); • @list = (); • Prolog lists: • List = [a,b, c] (List is a variable) • List = [a|[b|[c|[]]]] (a = head, tail = [b|[c|[]]]) • List = [] Mixed notation: [a|[b,c]] [a,b|[c]]
Regular Grammars • Tree representation • Example • ?- s([a,a,b],[]). true There’s a choice of rules for nonterminal b: Prolog tries the first rule Through backtracking It can try other choices Derivation: s [a], b (rule 1) [a],[a],b (rule 2) [a],[a],[b] (rule 4) 1. s --> [a],b. 2. b--> [a],b. 3. b--> [b],c. 4. b --> [b]. 5. c --> [b],c. 6. c --> [b]. our a regular grammar all terminals, so we stop Using trace, we can observe the progress of the derivation…
Regular Grammars • Tree representation • Example • ?- s([a,a,b,b,b],[]). Derivation: s [a], b (rule 1) [a],[a],b (rule 2) [a],[a],[b],c (rule 3) [a],[a],[b],[b],c (rule 5) [a],[a],[b],[b],[b] (rule 6) 1. s --> [a],b. 2. b--> [a],b. 3. b--> [b],c. 4. b --> [b]. 5. c --> [b],c. 6. c --> [b].
Prolog Derivations • Prolog’s computation rule: • Try first matching rule in the database (remember others for backtracking) • Backtrack if matching rule leads to failure • undo and try next matching rule (or if asked for more solutions) • For grammars: • Top-down left to right derivations • left to right = expand leftmost nonterminal first • Leftmost expansion done recursively = depth-first
Prolog Derivations For a top-down derivation, logically, we have: • Choice • about which rule to use for nonterminals b and c • No choice • About which nonterminal to expand next 1. s --> [a],b. 2. b--> [a],b. 3. b--> [b],c. 4. b --> [b]. 5. c --> [b],c. 6. c --> [b]. • Bottom up derivation for [a,a,b,b] • [a],[a],[b],[b] • [a],[a],[b],c (rule 6) • [a],[a],b (rule 3) • [a],b(rule 2) • s (rule 1) Prolog doesn’t give you bottom-up derivations … you’d have to program it up
Regex, FSA and a Regular Grammar Textbook Exercise: find a RE for • Examples (* denotes string not in the language): • *ab*ba • bab • λ (empty string) • bb • *baba • babab
Regex, FSA and a Regular Grammar • Draw a FSA and convert it to a RE: b b [Powerpoint Animation] > 1 2 3 4 b a b ε Ungraded homework exercise: Use the technique shown in a previous class Eliminate one state at a time b* b ( )+ ab+ = b+(ab+)* | ε
Regex, FSA and a Regular Grammar • Regular Grammar in Prolog. • Let’s begin with something like: • s --> [b], b. (grammar generates bb+) • b --> [b]. • b --> [b], b. Add rule: • b --> [a], b. (grammar generates b+(a|b)*b) • Classroom Exercise: • How would you fix this grammar so it can handle the specified language?
Regex, FSA and 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]
Regex, FSA and 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.
Regex, FSA and 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
Regex, FSA and a Regular Grammar • 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
Regex, FSA and 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:
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) writes out list in full why? Prolog matches rules in the order in which they’re written
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,[]).
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,[]).