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

Slides:



Advertisements
Similar presentations
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
Advertisements

LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong. Administrivia Homework 3 graded.
1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong. Administrivia We'll postpone Homework 4 review until next week …
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/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/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.
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 438/538 Computational Linguistics Sandiway Fong Lecture 11: 10/3.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
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.
Normal forms for Context-Free Grammars
LING/C SC/PSYC 438/538 Midterm 10/11. Instructions It is recommended that you attempt all questions –Submit your answers in one file by to
LING 388 Language and Computers Lecture 9 9/30/03 Sandiway FONG.
CS 3240 – Chapter 8.  Is a n b n c n context-free? CS Properties of Context-Free Languages2.
MA/CSSE 474 Theory of Computation
Finite State Machines Data Structures and Algorithms for Information Processing 1.
LING 388 Language and Computers Lecture 6 9/18/03 Sandiway FONG.
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.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
LING/C SC/PSYC 438/538 Lecture 14 Sandiway Fong. Administrivia Midterm – This Wednesday – A bit like doing a homework in real time – Bring your laptop.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/15.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.
LING/C SC/PSYC 438/538 Lecture 14 Sandiway Fong. Administrivia Homework 6 graded.
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?
Syntax Analyzer (Parser)
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.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Lecture 15 Ambiguous Grammars Topics: Context Free Grammars Language generated by a grammar Proofs with L(G) Ambiguous grammars October 20, 2008 CSCE 355.
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.
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Language and Grammar classes
The chomsky hierarchy Module 03.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Context free grammar.
LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong.
PARSE TREES.
MA/CSSE 474 Decision Problems about Regular Languages
LING/C SC/PSYC 438/538 Lecture 21 Sandiway Fong.
Syntax Analysis Sections :.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
Context-Free Languages
Context-Free Grammars (CFG’s)
Intro to Data Structures
LING/C SC/PSYC 438/538 Lecture 19 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 22 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 18 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.
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Pushdown automata Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

LING/C SC/PSYC 438/538 Lecture 21 Sandiway Fong

Today's Topics Homework 8 Review Optional Homework 9 (make up on Homework 7)

Homework 8 Review Question1: write a Prolog regular grammar for the following language: Σ = {0, 1} L = strings from Σ* with an odd number of 0's and an even number of 1's assume 0 is an even number Hint: start with a FSA How many grammar rules do you have? submit your program and sample runs Examples: *[] (empty string) 0 000 00000 *1 *10 *01 *11 011 101 110

Homework 8 Review # 0's can be odd or even only Four possible states (abbreviated): e0e1: even 0 even 1 o0e1: odd 0 even 1 e0o1: even 0 odd 1 o0o1: odd 0 odd 1 Start state: e0e1 empty string: zero 0's and 1's Accept state: o0e1 e0e1 --> [0], o0e1. e0e1 --> [1], e0o1. o0e1 --> []. o0e1 --> [0], e0e1. o0e1 --> [1], o0o1. o0o1 --> [0], e0o1. o0o1 --> [1], o0e1. e0o1 --> [0], o0o1. e0o1 --> [1], e0e1.

Homework 8 Review ?- [hw8]. true. ?- e0e1([0],[]). true ; false. ?- e0e1([0,0,0],[]). ?- e0e1([0,0],[]). ?- e0e1([0,0,0,0],[]). ?- e0e1([],[]). ?- e0e1([1],[]). ?- e0e1([1,1],[]). ?- e0e1([1,1,1],[]). ?- e0e1([0,1,1],[]). ?- e0e1([1,0,1],[]). ?- e0e1([1,1,0],[]). ?- e0e1([1,0],[]). ?- e0e1([0,1],[]).

Homework 8 Review Recall Σ* Prolog code?

Homework 8 Review e0e1 --> [0]. e0e1 --> [0], o0e1. o0e1 --> [0], e0e1. o0e1 --> [1], o0o1. o0o1 --> [0], e0o1. o0o1 --> [1]. o0o1 --> [1], o0e1. e0o1 --> [0], o0o1. e0o1 --> [1], e0e1. e0e1 --> [0], o0e1. e0e1 --> [1], e0o1. o0e1 --> []. o0e1 --> [0], e0e1. o0e1 --> [1], o0o1. o0o1 --> [0], e0o1. o0o1 --> [1], o0e1. e0o1 --> [0], o0o1. e0o1 --> [1], e0e1. right recursive canonical format for regular grammars

Homework 8 Review (D)FSA: Optional Homework 9 (make-up opportunity for Homework 7) Give a regex for this DFSA Implement it in Perl or Python and show it works correctly Due next Monday

Homework 8 Review What happens when you run the following Prolog query on your grammar? s(String,[]).

Homework 8 Review ?- sigmas(L), e0e1(L,[]). L = [0] ; L = [0, 0, 0] ; L = [1, 1, 0] ; L = [1, 0, 1] ; L = [0, 1, 1] ; L = [0, 0, 0, 0, 0] ; L = [1, 1, 0, 0, 0] ; L = [1, 0, 1, 0, 0] ; L = [0, 1, 1, 0, 0] ; L = [1, 0, 0, 1, 0] ; L = [0, 1, 0, 1, 0] ; L = [0, 0, 1, 1, 0] ; L = [1, 1, 1, 1, 0] ; L = [1, 0, 0, 0, 1] ; L = [0, 1, 0, 0, 1] ; L = [0, 0, 1, 0, 1] ; L = [1, 1, 1, 0, 1] ; L = [0, 0, 0, 1, 1] ; L = [1, 1, 0, 1, 1] ; L = [1, 0, 1, 1, 1] ; L = [0, 1, 1, 1, 1] ; L = [0, 0, 0, 0, 0, 0, 0] ; L = [1, 1, 0, 0, 0, 0, 0] ; L = [1, 0, 1, 0, 0, 0, 0]

Homework 8 Review Use iterative deepening (material beyond this introductory course): ?- [id_meta, hw8]. String = [0, 0, 1, 1, 0] ; String = [1, 1, 1, 1, 0] ; true. String = [0, 1, 0, 0, 1] ; String = [0, 0, 0, 0, 0, 0, 0] ; String = [0, 1, 0, 1, 0] ; String = [0, 0, 0, 0, 0, 1, 1] ; ?- id(e0e1(String,[])). String = [0, 1, 1, 0, 0] ; String = [0, 0, 0, 0, 1, 0, 1] ; String = [0] ; String = [0, 1, 1, 1, 1] ; String = [0, 0, 0, 0, 1, 1, 0] ; String = [0, 0, 0] ; String = [1, 0, 0, 0, 1] ; String = [0, 0, 0, 1, 0, 0, 1] ; String = [0, 1, 1] ; String = [1, 0, 0, 1, 0] ; String = [0, 0, 0, 1, 0, 1, 0] ; String = [1, 0, 1] ; String = [1, 0, 1, 0, 0] ; String = [0, 0, 0, 1, 1, 0, 0]  String = [1, 1, 0] ; String = [1, 0, 1, 1, 1] ; String = [0, 0, 0, 0, 0] ; String = [1, 1, 0, 0, 0] ; String = [0, 0, 0, 1, 1] ; String = [1, 1, 0, 1, 1] ; String = [0, 0, 1, 0, 1] ; String = [1, 1, 1, 0, 1] ;

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

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

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

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

Left Recursion and Set Enumeration left recursive regular grammar: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. 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. 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 derivation tree for ?- s(L,[]). [Powerpoint animation] left recursive regular grammar: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. 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 L = [b,a,a,!] L = [b,a,a|..] L = [b,a|..] L = L = [b|..] Choice point s s a ! a ! ba a a a and so on b a ba a b a b b

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

So far … We've talked about: 1. Declarative (logical) reading of grammar rules 2. Prolog query: s(String,[]). Case 1. String is known: Is String ∈ L(G)? Case 2. String is unknown: enumerate L(G) 3. Different search strategies Prolog's (left-to-right) depth-first search Iterative deepening

Beyond Regular Languages anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n≥1 is not a regular language That means no FSA, RE or RG can be built for this language We only have a finite number of states to play with … We’re only allowed simple free iteration (looping) Pumping Lemma proof Perl regex:

Beyond Regular Languages Example: Language anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1 A regular grammar extended to allow both left and right recursive rules can accept/generate it: anbn.pl a --> [a], b. b --> [b]. b --> a, [b]. Set membership Set enumeration

Beyond Regular Languages anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1 A regular grammar extended to allow both left and right recursive rules can accept/generate it: a --> [a], b. b --> [b]. b --> a, [b]. Intuition: grammar implements the stacking of partial trees balanced for a’s and b’s: B A b a

Beyond Regular Languages anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1 A regular grammar extended to allow both left and right recursive rules can accept/generate it: a --> [a], b. b --> [b]. b --> a, [b]. A type-2 or context-free grammar (CFG) has no restrictions on what can go on the RHS of a grammar rule Note: CFGs still have a single nonterminal limit for the LHS of a rule Example: s --> [a], [b]. s --> [a], s, [b].

Extra Argument: Parse Tree Recovering a parse tree when want Prolog to return more than just true/false answers in case of true, we can compute a syntax tree representation of the parse by adding an extra argument to nonterminals applies to all grammar rules (not just regular grammars) Example sheeptalk again DCG (non-regular, context-free): s --> [b], [a], a, [!]. a --> [a]. (base case) a --> [a], a. (recursive case) s a ! b

Extra Argument: Parse Tree ! b Prolog term data structure: hierarchical allows sequencing of arguments functor(arg1,..,argn) each argi could be another term or simple atom Tree: s(b,a,a(a,a(a)),!)

Extra Arguments: Parse Tree DCG s --> [b],[a], a, [!]. a --> [a]. (base case) a --> [a], a. (right recursive case) s a ! b base case a --> [a]. a(subtree) --> [a]. a(a(a)) --> [a]. s(b,a,a(a,a(a)),!) recursive case a --> [a], a. a(subtree) --> [a], a(subtree). a(a(a,A)) --> [a], a(A). Idea: for each nonterminal, add an argument to store its subtree

Extra Arguments: Parse Tree Prolog grammar s --> [b], [a], a, [!]. a --> [a]. (base case) a --> [a], a. (right recursive case) s a ! b base and recursive cases a(a(a)) --> [a]. a(a(a,A)) --> [a], a(A). start symbol case s --> [b], [a], a, [!]. s(tree) --> [b], [a], a(subtree), [!]. s(s(b,a,A,!) ) --> [b], [a], a(A), [!]. s(b,a,a(a,a(a)),!)

Extra Arguments: Parse Tree Prolog grammar s --> [b], [a], a, [!]. a --> [a]. (base case) a --> [a], a. (right recursive case) Equivalent Prolog grammar computing a parse s(s(b,a,A,!)) --> [b], [a], a(A), [!]. a(a(a)) --> [a]. a(a(a,A)) --> [a], a(A).

Quick Homework 10 Modify this grammar to build parse trees for strings in the language anbn, n≥1. anbn.pl a --> [a], b. b --> [b]. b --> a, [b]. Show your code and give example runs. Due next Monday night