COMP Logic for Computer Scientists. Lecture 25

Similar documents
Theory of Programming Languages COMP360

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

ECS 120 Lesson 7 Regular Expressions, Pt. 1

CSE 105 THEORY OF COMPUTATION

1. [5 points each] True or False. If the question is currently open, write O or Open.

Languages and Finite Automata

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Regular Languages. MACM 300 Formal Languages and Automata. Formal Languages: Recap. Regular Languages

(Refer Slide Time: 0:19)

COMP Logic for Computer Scientists. Lecture 23

8 ε. Figure 1: An NFA-ǫ

Regular Languages and Regular Expressions

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Chapter Seven: Regular Expressions. Formal Language, chapter 7, slide 1

Languages and Models of Computation

2010: Compilers REVIEW: REGULAR EXPRESSIONS HOW TO USE REGULAR EXPRESSIONS

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

Theory Bridge Exam Example Questions Version of June 6, 2008

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CS402 - Theory of Automata Glossary By

Automating Construction of Lexers

Automata & languages. A primer on the Theory of Computation. The imitation game (2014) Benedict Cumberbatch Alan Turing ( ) Laurent Vanbever

CSE 105 THEORY OF COMPUTATION

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

Outline. Language Hierarchy

Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

Multiple Choice Questions

We can create PDAs with multiple stacks. At each step we look at the current state, the current input symbol, and the top of each stack.

Finite Automata Part Three

Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Lecture 2 Finite Automata

To illustrate what is intended the following are three write ups by students. Diagonalization

Ambiguous Grammars and Compactification

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

Converting a DFA to a Regular Expression JP

CS52 - Assignment 10

UNIT 9C Randomness in Computation: Cellular Automata Principles of Computing, Carnegie Mellon University

Finite Automata Part Three

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Lexical Analysis. Prof. James L. Frankel Harvard University

Learn Smart and Grow with world

1 Finite Representations of Languages

CS103 Handout 13 Fall 2012 May 4, 2012 Problem Set 5

CMPSCI 250: Introduction to Computation. Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013


Dr. D.M. Akbar Hussain

CT32 COMPUTER NETWORKS DEC 2015

HKN CS 374 Midterm 1 Review. Tim Klem Noah Mathes Mahir Morshed

Chapter Seven: Regular Expressions

Turing Machines. A transducer is a finite state machine (FST) whose output is a string and not just accept or reject.

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

Chapter 2 - Programming Language Syntax. September 20, 2017

Midterm I - Solution CS164, Spring 2014

Theory of Computations Spring 2016 Practice Final Exam Solutions

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

CSCI 2132 Software Development. Lecture 7: Wildcards and Regular Expressions

Decision, Computation and Language

Computer Sciences Department

Lecture 3: Lexical Analysis

Name: Finite Automata

Final Course Review. Reading: Chapters 1-9

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Lecture 4: Syntax Specification

Formal Languages and Compilers Lecture VI: Lexical Analysis

TOPIC PAGE NO. UNIT-I FINITE AUTOMATA

Formal Languages. Formal Languages

CMSC 132: Object-Oriented Programming II

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

LECTURE NOTES THEORY OF COMPUTATION

Quiz 1: Solutions J/18.400J: Automata, Computability and Complexity. Nati Srebro, Susan Hohenberger

Implementation of Lexical Analysis

Regular Expressions & Automata

CMSC 350: COMPILER DESIGN

CIT3130: Theory of Computation. Regular languages

Final Exam 1, CS154. April 21, 2010

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

LECTURE NOTES THEORY OF COMPUTATION

Chapter 11 :: Functional Languages

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Finite Automata Part Three

CS402 - Theory of Automata FAQs By

Midterm I (Solutions) CS164, Spring 2002

Lexical Analysis. Lecture 3-4

Theory of Languages and Automata

Languages and Compilers

1 Parsing (25 pts, 5 each)

CS103 Handout 14 Winter February 8, 2013 Problem Set 5

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

CSE 105 THEORY OF COMPUTATION

Course Project 2 Regular Expressions

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

Formal Grammars and Abstract Machines. Sahar Al Seesi

Formal Languages and Automata

Transcription:

COMP 1002 Logic for Computer Scientists Lecture 25 B 5 2 J

Admin stuff Assignment 4 is posted. Due March 23 rd. Monday March 20 th office hours From 2:30pm to 3:30pm I need to attend something 2-2:30pm.

Regular expressions A regular expression is a standard tool for pattern matching in Python, Perl, Ruby, grep, shell scripts... a b* matches either a letter a, or 0 or more repetitions of b. So a regular expression defines a set of strings that it matches: a regular language. Recursive definition of regular expressions (as a set of strings): Base:, λλ (empty string), all letters in alphabet Recursive step: Given two regular expressions R and S, the following are regular expressions: Union RR SS (sometimes written RR SS ) Often drop parentheses when no ambiguity Concatenation RR SS = xxxx xx RR aaaaaa yy SS } (sometimes written RS) A Kleene star RR = xx 1 xx 2 xx kk kk N ii {0,, kk} xx ii RR } k=0 ok; so zero or more strings from R concatenated together. Restriction: no other strings are in the set.

Examples of regular expressions aaaa Strings of one or more a s. 0 1 00 Binary strings ending in 00. COMP(1000 1001 1002 2001) Matches COMP1000, COMP1001, COMP1002 and COMP2001. COMP(1 2)00(0 1 2) COMP1000,COMP2000,COMP1001, COMP2001, COMP1002,COMP2002 Does not match anything: zero strings in the language λλ Matches the empty string: one string in the language

Permanent link to this comic: https://xkcd.com/208/

Pattern matching Suppose we have a DNA string: AAGATTCATTAATAAATACGCTTACA And a gene string ATAC How do we check if the string contains the match? AAGATTCATATAATAAATACGCTTACA ATAC Could just move along checking each letter, and if mismatch, shifting by 1 character... There is a faster way: finite state machines.

Matching with finite state machines Faster matching idea: AAGATTCATATAATAAATACGCTTACA ATAC If mismatch T instead of C, know that shifting by 2 would be good enough; no need to re-match ATA ` Start match C,G,T A C,G ` 1st letter OK A T 2nd letter OK A T 3rd letter OK C Last letter OK C,G,T G A

Finite state machine Metrobus door: wave to open. Only works when bus has stopped. Description of the system: If bus is in motion then closed. If bus is stopped then if wave received, open. If bus is stopped and there is no wave, remain closed. Door Closed Bus stopped and wave Door open Bus started moving

Finite state machine Finite state machine: States Including start state s, possibly finish states Inputs An input alphabet Transitions from SSSSSSSSSSSS IIIIIIIIIIII SSSSSSSSSSSS Sometimes also have outputs: Then include output alphabet Transitions to SSSSSSSSSSSS OOOOOOOOOOOOOO In the bus example Two states: closed and open. Looks like closed is the start state. In real life, probably more states needed. Input alphabet Bus moving/stopping, wave. Transitions: If closed and stopping and sensed a wave, go to open If open and started moving, go to closed. Bus stopped and wave Bus started moving

Finite automata Finite state machines with no output. Take an input string, accept if finish in an accepting state Example: accept strings with even number of 1s. States ss 1, ss 2 ss 1 is a start state Arrow ss 1 is an accepting state Double circle Input alphabet is {0,1} Transitions: ss 1, 0 ss 1 ss 1, 1 ss 2 ss 2, 0 ss 2 0 1 ss 11 ss 1 ss 2 ss 22 ss 2 ss 1 ss 1 0 1 1 ss 2 0 ss 2, 1 ss 1 If exactly one transition for each pair (state, symbol) Then called deterministic finite automata (DFA) Otherwise, non-deterministic finite automata (NFA) No transition: stop and reject. Multiple: if some choice eventually leads to accept, accept. Everything an NFA can do, a DFA can do. But might need a much bigger DFA.

Regular expressions Recursive definition of regular expressions (as a set of strings): Base:, λλ (empty string), all letters in alphabet Recursive step: Given two regular expressions R and S, the following are regular expressions: Union RR SS (sometimes written RR SS ) Often drop parentheses when no ambiguity Concatenation RR SS = xxxx xx RR aaaaaa yy SS } (sometimes written RS) A Kleene star RR = xx 1 xx 2 xx kk kk N ii {0,, kk} xx ii RR } k=0 ok; so zero or more strings from R concatenated together. Restriction: no other strings are in the set.

Finite automata compute what regular expressions match Each regular expression can be computed by a finite automaton (in particular, NFA). Proof (structural induction) Base case: Compute the empty language: Accept just the empty string: Accept just the string with one symbol a: Recursion step: take NFAs for R and for S. Kleene star RR : loop back to start (make start accepting) Union RR SS: done with ambiguity (combine starts) Concatenation RR SS: accept states of R become start of S. a

Turing machines Like finite automata with external memory. Church-Turing thesis: Turing machines can compute anything computable In particular, anything a human can compute.

Turing machine A Turing machine has an (unlimited) memory, visualized as a tape Or a stack of paper And takes very simple instructions: Read a symbol Write a symbol Move one step left or right on the tape Change internal state.

Church-Turing thesis Everything we can call computable is computable by a Turing machine.

Conway s game of life Converge to a still pattern At every step of the game: Every live cell with less than 2 neighbours dies Every live cell with more than 3 neighbours dies A cell with exactly 3 neighbours becomes alive (is born ). Oscillate Create a moving pattern

Conway s game of life: what does it mean to compute? Rules of the Game of Life: At every step of the game: Every live cell with less than 2 neighbours dies Every live cell with more than 3 neighbours dies A cell with exactly 3 neighbours becomes alive (is born ). Start with a few cells lit up See if cells somewhere else light up Make it so they only light up if some condition holds Just like a Turing machine going into yes -state if some condition holds about its input

Game of life and Turing machines are equivalent A Turing machine can read a description of the initial configuration and keep applying the rules. Conway game of life can do a Turing machine using this picture:

Puzzle: chocolate squares Suppose you have a piece of chocolate like this: How many squares are in it? of all sizes, from single to the whole thing