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

Similar documents
CMPSC 470: Compiler Construction

ECE 468/573 Midterm 1 September 28, 2012

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

CS 430 Spring Mike Lam, Professor. Parsing

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

CMSC 331 First Midterm Exam

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing

CSCE 531, Spring 2017, Midterm Exam Answer Key

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications.

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

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CSE 401 Midterm Exam 11/5/10 Sample Solution

Definition of Regular Expression

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins.

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

Theory of Computation CSE 105

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

Today. Search Problems. Uninformed Search Methods. Depth-First Search Breadth-First Search Uniform-Cost Search

Compilers Spring 2013 PRACTICE Midterm Exam

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

CS 321 Programming Languages and Compilers. Bottom Up Parsing

CSCI 446: Artificial Intelligence

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Assignment 4. Due 09/18/17

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata

Context-Free Grammars

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

Basics of Logic Design Arithmetic Logic Unit (ALU)

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

ASTs, Regex, Parsing, and Pretty Printing

CSEP 573 Artificial Intelligence Winter 2016

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

Midterm 2 Sample solution

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Topic 2: Lexing and Flexing

Midterm I Solutions CS164, Spring 2006

CS 241 Week 4 Tutorial Solutions

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES

Fall 2017 Midterm Exam 1 October 19, You may not use any books, notes, or electronic devices during this exam.

Context-Free Grammars

Eliminating left recursion grammar transformation. The transformed expression grammar

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

Fall 2018 Midterm 2 November 15, 2018

Context-Free Grammars

6.2 Volumes of Revolution: The Disk Method

Scanner Termination. Multi Character Lookahead

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

TO REGULAR EXPRESSIONS

Functor (1A) Young Won Lim 8/2/17

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

Functor (1A) Young Won Lim 10/5/17

Product of polynomials. Introduction to Programming (in C++) Numerical algorithms. Product of polynomials. Product of polynomials

CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1)

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. General Tree Search. Uniform Cost. Lecture 3: A* Search 9/4/2007

COMP 423 lecture 11 Jan. 28, 2008

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Digital Design. Chapter 1: Introduction. Digital Design. Copyright 2006 Frank Vahid

Fig.25: the Role of LEX

Today s Lecture. Basics of Logic Design: Boolean Algebra, Logic Gates. Recursive Example. Review: The C / C++ code. Recursive Example (Continued)

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

CS201 Discussion 10 DRAWTREE + TRIES

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

MIPS I/O and Interrupt

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

LEX5: Regexps to NFA. Lexical Analysis. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

MATH 25 CLASS 5 NOTES, SEP

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

MTH 146 Conics Supplement

Math 142, Exam 1 Information.

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

Section 3.1: Sequences and Series

AI Adjacent Fields. This slide deck courtesy of Dan Klein at UC Berkeley

Lecture T4: Pattern Matching

Unit #9 : Definite Integral Properties, Fundamental Theorem of Calculus

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

10.5 Graphing Quadratic Functions

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

12-B FRACTIONS AND DECIMALS

Dr. D.M. Akbar Hussain

a < a+ x < a+2 x < < a+n x = b, n A i n f(x i ) x. i=1 i=1

EECS 281: Homework #4 Due: Thursday, October 7, 2004

Physics 208: Electricity and Magnetism Exam 1, Secs Feb IMPORTANT. Read these directions carefully:

Topic: Software Model Checking via Counter-Example Guided Abstraction Refinement. Having a BLAST with SLAM. Combining Strengths. SLAM Overview SLAM

Spring 2018 Midterm Exam 1 March 1, You may not use any books, notes, or electronic devices during this exam.

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

1 Quad-Edge Construction Operators

CS 268: IP Multicast Routing

Uninformed Search. Hal Daumé III. Computer Science University of Maryland CS 421: Introduction to Artificial Intelligence 31 Jan 2012

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

ECEN 468 Advanced Logic Design Lecture 36: RTL Optimization

Transcription:

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

Tody's Topics Homework 8 Review Optionl Homework 9 (mke up on Homework 7)

Homework 8 Review Question1: write Prolog regulr grmmr for the following lnguge: Σ = {0, 1} L = strings from Σ* with n odd number of 0's nd n even number of 1's ssume 0 is n even number Hint: strt with FSA How mny grmmr rules do you hve? submit your progrm nd smple runs Exmples: *[] 0 000 00000 *1 *10 *01 *11 011 101 110 (empty string)

Homework 8 Review # 0's cn be odd or even only # 1's cn be odd or even only Four possible sttes (bbrevited): e0e1: even 0 even 1 o0e1: odd 0 even 1 e0o1: even 0 odd 1 o0o1: odd 0 odd 1 Strt stte: e0e1 empty string: zero 0's nd 1's Accept stte: o0e1 1. e0e1 --> [0], o0e1. 2. e0e1 --> [1], e0o1. 3. o0e1 --> []. 4. o0e1 --> [0], e0e1. 5. o0e1 --> [1], o0o1. 6. o0o1 --> [0], e0o1. 7. o0o1 --> [1], o0e1. 8. e0o1 --> [0], o0o1. 9. e0o1 --> [1], e0e1.

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

Homework 8 Review Recll Σ * Prolog code?

Homework 8 Review 1. e0e1 --> [0], o0e1. 2. e0e1 --> [1], e0o1. 3. o0e1 --> []. 4. o0e1 --> [0], e0e1. 5. o0e1 --> [1], o0o1. 6. o0o1 --> [0], e0o1. 7. o0o1 --> [1], o0e1. 8. e0o1 --> [0], o0o1. 9. e0o1 --> [1], e0e1. 1. e0e1 --> [0]. 2. e0e1 --> [0], o0e1. 3. e0e1 --> [1], e0o1. 4. o0e1 --> [0], e0e1. 5. o0e1 --> [1], o0o1. 6. o0o1 --> [0], e0o1. 7. o0o1 --> [1]. 8. o0o1 --> [1], o0e1. 9. e0o1 --> [0], o0o1. 10.e0o1 --> [1], e0e1. right recursive cnonicl formt for regulr grmmrs

Homework 8 Review (D)FSA: Optionl Homework 9 (mke-up opportunity for Homework 7) Give regex for this DFSA Implement it in Perl or Python nd show it works correctly Due next Mondy

Homework 8 Review Wht hppens when you run the following Prolog query on your grmmr? s(string,[]).

Homework 8 Review?- sigms(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 itertive deepening (mteril beyond this introductory course):?- [id_met, hw8]. true.?- id(e0e1(string,[])). String = [0] ; String = [0, 0, 0] ; String = [0, 1, 1] ; String = [1, 0, 1] ; String = [1, 1, 0] ; String = [0, 0, 0, 0, 0] ; String = [0, 0, 0, 1, 1] ; String = [0, 0, 1, 0, 1] ; String = [0, 0, 1, 1, 0] ; String = [0, 1, 0, 0, 1] ; String = [0, 1, 0, 1, 0] ; String = [0, 1, 1, 0, 0] ; String = [0, 1, 1, 1, 1] ; String = [1, 0, 0, 0, 1] ; String = [1, 0, 0, 1, 0] ; String = [1, 0, 1, 0, 0] ; String = [1, 0, 1, 1, 1] ; String = [1, 1, 0, 0, 0] ; String = [1, 1, 0, 1, 1] ; String = [1, 1, 1, 0, 1] ; String = [1, 1, 1, 1, 0] ; String = [0, 0, 0, 0, 0, 0, 0] ; String = [0, 0, 0, 0, 0, 1, 1] ; String = [0, 0, 0, 0, 1, 0, 1] ; String = [0, 0, 0, 0, 1, 1, 0] ; String = [0, 0, 0, 1, 0, 0, 1] ; String = [0, 0, 0, 1, 0, 1, 0] ; String = [0, 0, 0, 1, 1, 0, 0]

Left Recursion nd Set Enumertion Exmple: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Grmmr is: regulr grmmr left recursive (nonterminl on left) Question Wht is the lnguge of this grmmr? Answer: Sheeptlk! b..! (# 's > 1) Sententil forms: s! b! b! b! Underscoring used here to indicte nonterminl 12

Left Recursion nd Set Enumertion Exmple: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Prolog query:?- s([b,,,!],[]). true?- s([b,,,,,!],[]). true But it doesn t hlt when fced with string not in the lnguge?- s([b,,!],[]). ERROR: Out of locl stck 13

Left Recursion nd Set Enumertion Exmple: s -->, [!]. --> b, []. -->, []. b --> b, []. b --> [b]. In fct 14

Left Recursion nd Set Enumertion Exmple: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Why??- s([b,,!],[]). ERROR: Out of locl stck 15

Left Recursion nd Set Enumertion left recursive regulr grmmr: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Behvior hlts when presented with string tht is in the lnguge doesn t hlt when fced with string not in the lnguge unble to decide the lnguge membership question Surprisingly, the query:?- s(l,[]). enumertes the strings in the lnguge just fine.?- s(l,[]). L = [b,,,!] ; L = [b,,,,!] ; L = [b,,,,,!] ; L = [b,,,,,,!] ; L = [b,,,,,,,!] ; L = [b,,,,,,,,!] ; L = [b,,,,,,,,...] w L = [b,,,,,,,,,!]

Left Recursion nd Set Enumertion left recursive regulr grmmr: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Choice point derivtion tree for?- s(l,[]). L = [b..] [b,..] [b,,..] [b,,,!] s! [Powerpoint nimtion] s! Behvior hlts when presented with string tht is in the lnguge doesn t hlt when fced with string not in the lnguge b b b b b nd so on b

Left Recursion nd Set Enumertion However, this slightly re-ordered left recursive regulr grmmr: 1. s -->, [!]. 2. -->, []. 3. --> b, []. 4. b --> b, []. 5. b --> [b]. Why? s [Powerpoint nimtion] (rules 2 nd 3 swpped) won t hlt when enumerting... descends infinitely using rule #2

So fr We've tlked bout: 1. Declrtive (logicl) reding of grmmr rules 2. Prolog query: s(string,[]). Cse 1. String is known: Is String L(G)? Cse 2. String is unknown: enumerte L(G) 3. Different serch strtegies Prolog's (left-to-right) depth-first serch Itertive deepening

Beyond Regulr Lnguges Beyond regulr lnguges n b n = {b, bb, bbb, bbbb,... } n 1 is not regulr lnguge Tht mens no FSA, RE or RG cn be built for this lnguge 1. We only hve finite number of sttes to ply with 2. We re only llowed simple free itertion (looping) 3. Pumping Lemm proof Perl regex:

Beyond Regulr Lnguges Lnguge n b n = {b, bb, bbb, bbbb,... } n>=1 Exmple: Set membership A regulr grmmr extended to llow both left nd right recursive rules cn ccept/generte it: nbn.pl 1. --> [], b. 2. b --> [b]. 3. b -->, [b]. Set enumertion

Beyond Regulr Lnguges Lnguge n b n = {b, bb, bbb, bbbb,... } n>=1 A regulr grmmr extended to llow both left nd right recursive rules cn ccept/generte it: 1. --> [], b. 2. b --> [b]. 3. b -->, [b]. Intuition: grmmr implements the stcking of prtil trees blnced for s nd b s: A B b A A B b

Beyond Regulr Lnguges Lnguge n b n = {b, bb, bbb, bbbb,... } n>=1 A regulr grmmr extended to llow both left nd right recursive rules cn ccept/generte it: 1. --> [], b. 2. b --> [b]. 3. b -->, [b]. A type-2 or context-free grmmr (CFG) hs no restrictions on wht cn go on the RHS of grmmr rule Note: CFGs still hve single nonterminl limit for the LHS of rule Exmple: 1. s --> [], [b]. 2. s --> [], s, [b].

Extr Argument: Prse Tree Recovering prse tree when wnt Prolog to return more thn just true/flse nswers in cse of true, we cn compute syntx tree representtion of the prse by dding n extr rgument to nonterminls pplies to ll grmmr rules (not just regulr grmmrs) Exmple s sheeptlk gin DCG (non-regulr, context-free): b! s --> [b], [],, [!]. --> []. (bse cse) --> [],. (recursive cse)

Extr Argument: Prse Tree Tree: b s! Prolog term dt structure: hierrchicl llows sequencing of rguments functor(rg 1,..,rg n ) ech rg i could be nother term or simple tom s(b,,(,()),!)

Extr Arguments: Prse Tree DCG s --> [b],[],, [!]. --> []. --> [],. bse cse --> []. (subtree) --> []. (()) --> []. (bse cse) (right recursive cse) recursive cse --> [],. (subtree) --> [], (subtree). ((,A)) --> [], (A). b s! s(b,,(,()),!) Ide: for ech nonterminl, dd n rgument to store its subtree

Extr Arguments: Prse Tree Prolog grmmr s --> [b], [],, [!]. --> []. (bse cse) --> [],. (right recursive cse) bse nd recursive cses (()) --> []. ((,A)) --> [], (A). b s! strt symbol cse s --> [b], [],, [!]. s(tree) --> [b], [], (subtree), [!]. s(s(b,,a,!) ) --> [b], [], (A), [!]. s(b,,(,()),!)

Extr Arguments: Prse Tree Prolog grmmr s --> [b], [],, [!]. --> []. --> [],. (bse cse) (right recursive cse) Equivlent Prolog grmmr computing prse s(s(b,,a,!)) --> [b], [], (A), [!]. (()) --> []. ((,A)) --> [], (A).

Quick Homework 10 Modify this grmmr to build prse trees for strings in the lnguge n b n, n 1. nbn.pl 1. --> [], b. 2. b --> [b]. 3. b -->, [b]. Show your code nd give exmple runs. Due next Mondy night