LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG.

Slides:



Advertisements
Similar presentations
CSA2050: DCG I1 CSA2050 Introduction to Computational Linguistics Lecture 8 Definite Clause Grammars.
Advertisements

LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 13: 10/9.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 8: 9/29.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 10: 9/27.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 12: 10/4.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21.
LING 388: Language and Computers Sandiway Fong Lecture 12: 10/5.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 8: 9/18.
LING 388 Language and Computers Lecture 14 10/16/03 Sandiway FONG.
LING 388 Language and Computers Lecture 3 9/09/03 Sandiway FONG.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/25.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7.
LING 388 Language and Computers Lecture 4 9/11/03 Sandiway FONG.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 11: 10/3.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/13.
LING 388: Language and Computers Sandiway Fong Lecture 11: 10/3.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
LING 388 Language and Computers Take-Home Final Examination 12/9/03 Sandiway FONG.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 12: 10/5.
LING 388: Language and Computers Sandiway Fong Lecture 17: 10/25.
LING 388 Language and Computers Lecture 11 10/7/03 Sandiway FONG.
LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.
LING 388 Language and Computers Lecture 7 9/23/03 Sandiway FONG.
LING 388: Language and Computers Sandiway Fong Lecture 4: 9/1.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 5: 9/5.
LING 388 Language and Computers Lecture 9 9/30/03 Sandiway FONG.
How to Convert a Context-Free Grammar to Greibach Normal Form
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
LING 388: Language and Computers Sandiway Fong Lecture 13: 10/10.
Finite State Machines Data Structures and Algorithms for Information Processing 1.
LING 388 Language and Computers Lecture 6 9/18/03 Sandiway FONG.
Languages and Grammars MSU CSE 260. Outline Introduction: E xample Phrase-Structure Grammars: Terminology, Definition, Derivation, Language of a Grammar,
LING 388: Language and Computers Sandiway Fong Lecture 8.
LING 388: Language and Computers Sandiway Fong 10/4 Lecture 12.
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
LING 388: Language and Computers Sandiway Fong Lecture 7.
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong.
Regular Grammars Chapter 7. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
Regular Grammars Chapter 7 1. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
CPS 506 Comparative Programming Languages Syntax Specification.
LING 388: Language and Computers Sandiway Fong 9/27 Lecture 10.
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
1 Week 5 Questions / Concerns What’s due: Lab2 part a due on Sunday Lab1 check-off by appointment Test#1 HW#5 next Thursday Coming up: Lab3 Posted. Discuss.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
1 Chapter 6 Simplification of CFGs and Normal Forms.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Three Basic Concepts Languages Grammars Automata.
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
LING/C SC/PSYC 438/538 Lecture 16 Sandiway Fong. SWI Prolog Grammar rules are translated when the program is loaded into Prolog rules. Solves the mystery.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong. Last Time Talked about: – 1. Declarative (logical) reading of grammar rules – 2. Prolog query: s(String,[]).
Context free grammar.
PARSE TREES.
Regular grammars Module 04.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
Chapter 7 Regular Grammars
Intro to Data Structures
LING/C SC/PSYC 438/538 Lecture 21 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 22 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 20 Sandiway Fong.
Presentation transcript:

LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG

Administrivia Last Lecture Last Lecture  Homework 2  Exercise 1: 2 pts, Exercise 2: 3pts, Exercise 3: 1pt This Lecture This Lecture  Homework 2 contd…  Exercise 4: 1pt, Exercise 5: 3 pts Next Lecture Next Lecture  Back in classroom  Franklin 220

Administrivia Revised TA Office Hours Revised TA Office Hours  Mondays 11am - 12pm  Haury 317  Other times by appointment 

Homework 1 Exercise 2 Review Homework Question Homework Question  Use both built-ins atom_chars/2 and append/3 to write a general rule  addNT/2 “add n’t” defined as follows:  addNT(X,Y) converts between a modal or auxiliary verb X and its contracted negative counterpart Y  Examples: could couldn’t, is isn’t  Make sure it (A) rejects may mayn’t(A) rejects may mayn’t (B) handles irregular forms can can`t, shall shan`t, will won`t(B) handles irregular forms can can`t, shall shan`t, will won`t

Introduction Last time… Last time…  We saw two ways of encoding FSA using Prolog rules This lecture… This lecture…  We look at how Prolog’s Definite Clause Grammar (DCG) notation can be used to encode regular grammars

Let’s revisit L = {a + b + } sx y a a b b

Regular Grammars From Lecture 6… From Lecture 6…  Right-recursive regular grammar G ab  S -> aB  B -> aB  B -> bC  B -> b  C -> bC  C -> b  L(G ab ) = {a + b + }

Regular Grammars Equivalent DCG for G ab : Equivalent DCG for G ab :  s --> [a], b.  b --> [a], b.  b --> [b], c.  b --> [b].  c --> [b], c.  c --> [b]. Notes: Notes:  --> = “rewrites”  Terminal symbols are enclosed in square brackets  Commas separate symbols on the right  Every rule ends with a period

Regular Grammars DCG rules and Prolog clauses DCG rules and Prolog clauses  Each DCG rule defines a clause with name = LHS of rule  Example: DCG rule s --> [a], b.s --> [a], b. is “syntactic sugar” in SWI-Prolog for a clause for predicate s/2: s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3). where ‘C’(L1,a,L2) calls a SWI-Prolog built-in that holds if the constant a is the difference between L1 and L2  DCG rule is automatically converted into the underlying Prolog clause  Use query ?- listing. to inspect…

Regular Grammars DCG rules and Prolog clauses contd. DCG rules and Prolog clauses contd.  The corresponding Prolog clause has arity 2  Example: s --> [a], b.s --> [a], b. s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).  Each non-terminal takes two lists as arguments  The difference between the two lists represents the string covered by the non-terminal or terminal in the case of ‘C’  Example: s(L1,L3)L1 – L3s(L1,L3)L1 – L3 ‘C’(L1,a,L2)L1 – L2 = a‘C’(L1,a,L2)L1 – L2 = a b(L2,L3)L2 – L3b(L2,L3)L2 – L3

Difference Lists Examples of well-formed difference lists: Examples of well-formed difference lists:  [a,a,b,b] – [] = [a,a,b,b]  [a,a,b,b] – [b] = [a,a,b]  [a,a,b,b] – [a,b,b] = [a]  [a,a,b,b] – [a,a,b,b] = [] Notes: Notes:  In L1 – L2, L2 must be a suffix of L1, otherwise L1 – L2 is not a well-formed difference list  Examples of ill-formed difference lists:  [a,a,b,b] – [a,a]  [a,a,b,b] – [a,a,a,b,b,b]

Difference Lists We can visualize difference lists as follows: We can visualize difference lists as follows:  s --> [a], b.  s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3). Alternatively, given a non-terminal x and difference list L-L’ Alternatively, given a non-terminal x and difference list L-L’  L represents the input list to be parsed  L’ represents the remainder of the input list after rule x has been parsed L1 L2 L3

Exercise 4: Regular Grammars Consult the DCG for G ab Consult the DCG for G ab Use ?- listing. to see the underlying DCG rule conversion Use ?- listing. to see the underlying DCG rule conversion Run essentially the same Prolog queries used for the FSA in Lecture 7 Run essentially the same Prolog queries used for the FSA in Lecture 7  ?- s([a,a,b,b],[]).  ?- s([a,b,a],[]).  ?- s([a,X],[]). Confirm for yourselves that the DCG returns the same results as the FSA

Exercise 4: Regular Grammars Example of Derivation: Example of Derivation:  ?- s([a,a,b,b],[]).S -> [a], b 'C'([a, a, b, b], a, L) L = [a, b, b] 'C'([a, a, b, b], a, L) L = [a, b, b] b([a, b, b], [])b -> [a], b b([a, b, b], [])b -> [a], b 'C'([a, b, b], a,L)L = [b, b] 'C'([a, b, b], a,L)L = [b, b] b([b, b], [])b -> [a], b b([b, b], [])b -> [a], b  'C'([b, b], a,L)FAIL  b([b, b], []) b -> [b], c 'C'([b, b], b,L)L = [b] 'C'([b, b], b,L)L = [b] c([b], [])c -> [b], c c([b], [])c -> [b], c 'C'([b], b, L) L = [] 'C'([b], b, L) L = [] c([], [])c -> [b], c c([], [])c -> [b], c  'C'([], b, L)FAIL  c([b], [])c -> [b]

Exercise 4: Regular Grammars Note:  We can access any non-terminal, not just the start symbol s Run Prolog queries for non-terminal c Run Prolog queries for non-terminal c  ?- c([b,b,b],[]).  ?- c([a,b],[]).

Exercise 4: Regular Grammars Homework Question: Homework Question:  What language does the sub-grammar starting with non-terminal b accept?  Give the answer in the form of a regular expression

Beyond Regular Grammars Recall from Lecture 6… Recall from Lecture 6…  For regular grammars, can’t have both left and right recursive rules in the same grammar A -> aBA -> aB A -> BaA -> Ba  Would change the expressive power of the grammar formalism and lose the correspondence with FSA and regular expressions

Beyond Regular Grammars DCG rules have no such limitations DCG rules have no such limitations  For example:  We can have both left and right recursive rules  We can have rules with more than one non- terminal and terminal symbol on the right side  We can even have complex non-terminals that contain variables and values…later

Left and Right Recursive Rules Recall from Lecture 6… Recall from Lecture 6…  The following grammar G n with starting non- terminal A generates the non-regular language L = {a n b n | n>=0 } A -> A ->  A -> aBA -> aB B -> AbB -> Ab B -> bB -> b  Can’t build a FSA to handle G n

Left and Right Recursive Rules Equivalent DCG for G n : Equivalent DCG for G n :  a --> []. a(L,L).  a --> [a], b. a(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).  b --> a, [b]. b(L1,L3) :- a(L1,L2), ‘C’(L2,b,L3).  b --> [b]. b([b|L],L).

Exercise 5: Left and Right Recursive Rules Consult DCG for G n Consult DCG for G n Check DCG conversion using ?- listing. Check DCG conversion using ?- listing. Run Prolog queries: Run Prolog queries:  ?- a([],[]).  ?- a([a,b],[]).  ?- a([a,a,a,b,b,b],[]).  Check also that…  ?- a([a,a,a,b,b],[]).#a > #b  ?- a([a,a,b,b,b],[]).#a < #b  ?- a([a],[]).No b all fail

Exercise 5: Left and Right Recursive Rules Homework Question (A) Homework Question (A)  What does the query ?- a(X,[]). return?  Note: Use  ?- set_prolog_flag(toplevel_print_options,[max_depth(0)]).  to see the entire list Homework Question (B) Homework Question (B)  What happens to the query ?- a(X,[]). if we switch the order of the clauses for non-terminal a?  Explain why