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.

Slides:



Advertisements
Similar presentations
Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –
Advertisements

LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong. Administrivia Homework 3 graded.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong. Administrivia We'll postpone Homework 4 review until next week …
LING 388: Language and Computers Sandiway Fong Lecture 9: 9/27.
LING 388: Language and Computers Sandiway Fong 9/29 Lecture 11.
CS466(Prasad)L7Parse1 Parsing Recognition of strings in a language.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
LING 438/538 Computational Linguistics Sandiway Fong Lecture 8: 9/29.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
CS 3240 – Chapter 3.  How would you delete all C++ files from a directory from the command line?  How about all PowerPoint files that start with the.
CS5371 Theory of Computation
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 10: 9/27.
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/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/11.
LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
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/13.
LING 388: Language and Computers Sandiway Fong Lecture 11: 10/3.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.
LING 388 Language and Computers Lecture 7 9/23/03 Sandiway FONG.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 5: 9/5.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
How to Convert a Context-Free Grammar to Greibach Normal Form
CS5371 Theory of Computation Lecture 12: Computability III (Decidable Languages relating to DFA, NFA, and CFG)
Finite State Machines Data Structures and Algorithms for Information Processing 1.
LING 388 Language and Computers Lecture 6 9/18/03 Sandiway FONG.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
LING 388: Language and Computers Sandiway Fong Lecture 7.
1 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the strings of a language. Notational Example.
1 Chapter Construction Techniques. 2 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
1 Language Definitions Lecture # 2. Defining Languages The languages can be defined in different ways, such as Descriptive definition, Recursive definition,
1 Chapter 1 Introduction to the Theory of Computation.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
LING/C SC/PSYC 438/538 Lecture 7 9/15 Sandiway Fong.
Module 2 How to design Computer Language Huma Ayub Software Construction Lecture 8.
LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Regular Expressions and Languages A regular expression is a notation to represent languages, i.e. a set of strings, where the set is either finite or contains.
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.
LING/C SC/PSYC 438/538 Lecture 14 Sandiway Fong. Administrivia Homework 6 graded.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Top-Down Parsing.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
CS 154 Formal Languages and Computability February 11 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
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,[]).
Lecture 17: Theory of Automata:2014 Context Free Grammars.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 19 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 21 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 26 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 20 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
Presentation transcript:

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 why we have to type two arguments with the nonterminal at the command prompt Recall list notation: – [1|[2,3,4]] = [1,2,3,4] 1. s --> [a],b. 2. b --> [a],b. 3. b --> [b],c. 4. b --> [b]. 5. c --> [b],c. 6. c --> [b]. 1.s([a|A], B) :- b(A, B). 2. b([a|A], B) :- b(A, B). 3. b([b|A], B) :- c(A, B). 4. b([b|A], A). 5. c([b|A], B) :- c(A, B). 6. c([b|A], A).

Regex, FSA and a Regular Grammar Textbook Exercise: find a RE for Examples (* denotes string not in the language): *ab *ba babbab λ (empty string) bb *baba*baba bababbabab

Regex, FSA and a Regular Grammar Draw a FSA and convert it to a RE: > b ab b b ε b* ab+ [Powerpoint Animation] = b*|b(ab+)+ | b Ungraded exercise : Could use the technique shown in a previous class: eliminate one state at a time

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 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 ab b 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 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. [Powerpoint animation] in order of the RG rules

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 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:

Regex, FSA and a Regular Grammar DCG to Prolog conversion: note the function of the two list arguments

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 writes out list in full

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,[]).

Set Enumeration using Prolog Similarly, if we swap rules 6 and 7 Regular Grammar 1.s --> []. 2.s --> [b], b. 3.s --> [b], s. 4.b --> [a], c. 5.c --> [b]. 6.c --> [b], c. 7.c --> [b], b. Prolog enumeration: ?- s(X,[]). Prolog's left-to-right depth-first search strategy cannot handle this. Need a breadth-first strategy or (equivalently) an iterative deepening strategy.

Set Enumeration using Prolog Iterative deepening: – run Prolog depth-first strategy repeatedly allowing maximum depth of expansion at 1, 2, 3, 4, 5, and so on… – inefficient but simple – can build a meta- interpreter for this (beyond the scope of this course) –id_meta.pl –id(Goal) tries to prove Goal using iterative deepening Example:

Left Recursion and Set Enumeration Example: 1.s --> a, [!]. 2.a --> ba, [a]. 3.a --> a, [a]. 4.ba --> b, [a]. 5.b --> [b]. Grammar is: – a regular grammar – left recursive Question – What is the language of this grammar? Answer: Sheeptalk! ba..a!(# a's > 1) Sentential forms: s a! baa! Underscoring used here to indicate a nonterminal Underscoring used here to indicate a nonterminal 16

Left Recursion and Set Enumeration Example: 1.s --> a, [!]. 2.a --> ba, [a]. 3.a --> a, [a]. 4.ba --> b, [a]. 5.b --> [b]. Prolog query: ?- s([b,a,a,!],[]). true ?- s([b,a,a,a,a,!],[]). true But it doesn’t halt when faced with a string not in the language ?- s([b,a,!],[]). ERROR: Out of local stack 17

Left Recursion and Set Enumeration Example: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. In fact… 18

Left Recursion and Set Enumeration Example: 1.s --> a, [!]. 2.a --> ba, [a]. 3.a --> a, [a]. 4.ba --> b, [a]. 5.b --> [b]. ?- s([b,a,!],[]). ERROR: Out of local stack Why? 19

Left Recursion and Set Enumeration left recursive regular grammar: 1.s --> a, [!]. 2.a --> ba, [a]. 3.a --> a, [a]. 4.ba --> b, [a]. 5.b --> [b]. Behavior – halts when presented with a string that is in the language – doesn’t halt when faced with a string not in the language – unable to decide the language membership question Surprisingly, the query: ?- s(L,[]). enumerates the strings in the language just fine. ?- s(L,[]). L = [b, a, a, !] ; L = [b, a, a, a, !] ; L = [b, a, a, a, a, !] ; L = [b, a, a, a, a, a, !] ; L = [b, a, a, a, a, a, a, !] ; L = [b, a, a, a, a, a, a, a, !] ; L = [b, a, a, a, a, a, a, a, a|...] w L = [b, a, a, a, a, a, a, a, a, !]

Left Recursion and Set Enumeration left recursive regular grammar: 1.s --> a, [!]. 2.a --> ba, [a]. 3.a --> a, [a]. 4.ba --> b, [a]. 5.b --> [b]. Behavior – halts when presented with a string that is in the language – doesn’t halt when faced with a string not in the language derivation tree for ?- s(L,[]). s a ba b b a a ! s a a b b a a a ! and so on L =L = [b|..] L = [b,a|..] L = [b,a,a|..]L = [b,a,a,!] [Powerpoint animation] Choice point

Left Recursion and Set Enumeration However, this slightly re- ordered left recursive regular grammar: 1.s --> a, [!]. 2.a --> a, [a]. 3.a --> ba, [a]. 4.ba --> b, [a]. 5.b --> [b]. (rules 2 and 3 swapped) won’t halt when enumerating Why? s a a a a... descends infinitely using rule #2 [Powerpoint animation]