Clarifications from last time. This Lecture. Last Lecture. CMSC 330: Organization of Programming Languages. Finite Automata.

Similar documents
CMSC 330: Organization of Programming Languages. Theory of Regular Expressions Finite Automata

How do regular expressions work? CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Regular Expressions and Finite Automata

Nondeterministic Finite Automata

COM364 Automata Theory Lecture Note 2 - Nondeterminism

CS 154, Lecture 2: Finite Automata, Closure Properties Nondeterminism,

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

CS 455/555: Finite automata

CS243, Logic and Computation Nondeterministic finite automata

Deterministic Finite Automata. Non deterministic finite automata. Non-Deterministic Finite Automata (NFA) Non-Deterministic Finite Automata (NFA)

Chapter Five: Nondeterministic Finite Automata

Closure under the Regular Operations

Languages. Non deterministic finite automata with ε transitions. First there was the DFA. Finite Automata. Non-Deterministic Finite Automata (NFA)

CS21 Decidability and Tractability

CISC 4090 Theory of Computation

Sri vidya college of engineering and technology

Computer Sciences Department

CS 154. Finite Automata, Nondeterminism, Regular Expressions

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Deterministic Finite Automaton (DFA)

CS 154, Lecture 3: DFA NFA, Regular Expressions

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

September 11, Second Part of Regular Expressions Equivalence with Finite Aut

Theory of Computation (I) Yijia Chen Fudan University

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CS 322 D: Formal languages and automata theory

Automata: a short introduction

Computational Models - Lecture 1 1

CSE 311: Foundations of Computing. Lecture 23: Finite State Machine Minimization & NFAs

Lecture 17: Language Recognition

Course 4 Finite Automata/Finite State Machines

Computational Models Lecture 2 1

Nondeterministic Finite Automata and Regular Expressions

Intro to Theory of Computation

Nondeterminism. September 7, Nondeterminism

CS21 Decidability and Tractability

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) September,

CSE 105 Theory of Computation Professor Jeanne Ferrante

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 35

Theory of Languages and Automata

CMPSCI 250: Introduction to Computation. Lecture #22: From λ-nfa s to NFA s to DFA s David Mix Barrington 22 April 2013

HKN CS/ECE 374 Midterm 1 Review. Nathan Bleier and Mahir Morshed

Finite Automata. Seungjin Choi

Finite Automata Part Two

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

CSE 105 Homework 1 Due: Monday October 9, Instructions. should be on each page of the submission.

Lecture 3: Nondeterministic Finite Automata

CSE 311 Lecture 23: Finite State Machines. Emina Torlak and Kevin Zatloukal

CMP 309: Automata Theory, Computability and Formal Languages. Adapted from the work of Andrej Bogdanov

CS 530: Theory of Computation Based on Sipser (second edition): Notes on regular languages(version 1.1)

Computational Models Lecture 2 1

T (s, xa) = T (T (s, x), a). The language recognized by M, denoted L(M), is the set of strings accepted by M. That is,

Computational Models #1

Equivalence of DFAs and NFAs

Computational Theory

What we have done so far

Recap DFA,NFA, DTM. Slides by Prof. Debasis Mitra, FIT.

Decision, Computation and Language

Automata Theory. Lecture on Discussion Course of CS120. Runzhe SJTU ACM CLASS

Closure under the Regular Operations

Inf2A: Converting from NFAs to DFAs and Closure Properties

CSE 105 THEORY OF COMPUTATION

Author: Vivek Kulkarni ( )

Recap from Last Time

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Nondeterminism and Epsilon Transitions

UNIT-I. Strings, Alphabets, Language and Operations

Classes and conversions

CPS 220 Theory of Computation REGULAR LANGUAGES

UNIT-II. NONDETERMINISTIC FINITE AUTOMATA WITH ε TRANSITIONS: SIGNIFICANCE. Use of ε-transitions. s t a r t. ε r. e g u l a r

September 7, Formal Definition of a Nondeterministic Finite Automaton

Lecture 1: Finite State Automaton

Deterministic Finite Automata (DFAs)

CHAPTER 1 Regular Languages. Contents

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

Finite State Machines 2

Proofs, Strings, and Finite Automata. CS154 Chris Pollett Feb 5, 2007.

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 43

Deterministic Finite Automata (DFAs)

acs-04: Regular Languages Regular Languages Andreas Karwath & Malte Helmert Informatik Theorie II (A) WS2009/10

Nondeterministic finite automata

Great Theoretical Ideas in Computer Science. Lecture 4: Deterministic Finite Automaton (DFA), Part 2

Theory Bridge Exam Example Questions

Algorithms for NLP

Lecture 23 : Nondeterministic Finite Automata DRAFT Connection between Regular Expressions and Finite Automata

CSE 105 THEORY OF COMPUTATION

Lecture 4 Nondeterministic Finite Accepters

Finite Automata and Regular Languages

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Theory of computation: initial remarks (Chapter 11)

Nondeterministic Finite Automata

Outline. Nondetermistic Finite Automata. Transition diagrams. A finite automaton is a 5-tuple (Q, Σ,δ,q 0,F)

CS 154. Finite Automata vs Regular Expressions, Non-Regular Languages

Automata and Languages

Finite Automata. Finite Automata

Languages, regular languages, finite automata

(Refer Slide Time: 0:21)

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Transcription:

CMSC 330: Organization of Programming Languages Last Lecture Languages Sets of strings Operations on languages Finite Automata Regular expressions Constants Operators Precedence CMSC 330 2 Clarifications from last time This Lecture Regular expression: (0 1... 9)*330(0 1... 9)* When talking about formal definition of REs, describe a set of strings exactly When used in Ruby (or other languages) are often used for substring matching Finite automata States Transitions Examples Implementation of Res Parse from left to right Keep track of whether current symbol is part of RE This brings us to today s lecture Types Deterministic (DFA) Non-deterministic (NFA) CMSC 330 3 CMSC 330 4 1

Implementing Regular Expressions Finite Automata Transition on 1 We can implement a regular expression by turning it into a finite automaton A machine for recognizing a regular language Start state Final state es No CMSC 330 5 States Machine starts in start or initial state Repeat until the end of the string is reached Scan the next symbol s of the string Take transition edge labeled with s String is accepted if automaton is in final state when end of string reached CMSC 330 6 Finite Automata: States Finite Automaton: Example 1 Start state State with incoming transition from no other state Can have only 1 start state Final state State with double circle Can have 0 or more final states S1 S2 0 0 1 0 1 1 accepted CMSC 330 7 CMSC 330 8 2

Finite Automaton: Example 2 What Language is This? 0 0 1 0 1 0 not accepted All strings over {0, 1} that end in 1 What is a regular expression for this language? (0 1)*1 CMSC 330 9 CMSC 330 10 Finite Automaton: Example 3 Finite Automaton: Example 3 (cont.) string aabcc state at end S2 accepts? What language does this FA accept? acc bbc S2 S2 a*b*c* aabbb S1 aa ε acba S0 S0 S3 N S3 is a dead state a nonfinal state with no transition to another state (a,b,c notation shorthand for three self loops) CMSC 330 11 CMSC 330 12 3

Dead State: Shorthand Notation What Lang. Does This FA Accept? If a transition is omitted, assume it goes to a dead state that is not shown is short for Language? Strings over {0,1,2,3} with alternating even and odd digits, beginning with odd digit a*b*c* again, so DFAs are not unique CMSC 330 13 CMSC 330 14 Practice Finite Automaton: Example 4 Give the English descriptions for the FA or regular expression of the following languages: ((0 1)(0 1)(0 1)(0 1)(0 1))* All strings with length a multiple of 5 (01)* (10)* (01)*0 (10)*1 All alternating binary strings All binary strings containing the substring 11 Language? (a b)*abb Description for each state S0 = Haven t seen anything yet OR seen zero or more b s OR Last symbol seen was a b S1 = Last symbol seen was an a S2 = Last two symbols seen were ab S3 = Last three symbols seen were abb CMSC 330 15 CMSC 330 16 4

Practice Types of Finite Automata Give the regular expressions and finite automata for the following languages ou and your neighbors names All protein-coding DNA strings (including only ATCG and appearing in multiples of 3) All binary strings containing exactly two 1 s All binary strings that start and end with the same number Deterministic Finite Automata (DFA) Exactly one sequence of steps for each string All examples so far Nondeterministic Finite Automata (NFA) May have many sequences of steps for each string More compact CMSC 330 17 CMSC 330 18 Formal Definition Formal Definition: Example A deterministic finite automaton (DFA) is a 5-tuple (Σ, Q, q 0, F, δ) where Σ is an alphabet the strings recognized by the DFA are over this set Q is a nonempty set of states q 0 Q is the start state F Q is the set of final states How many can there be? δ : Q x Σ Q specifies the DFA's transitions What's this definition saying that δ is? A DFA accepts s if it stops at a final state on s Σ = {0, 1} Q = {S0, S1} q 0 = S0 F = {S1} δ 0 1 S0 S0 S1 S1 S0 S1 CMSC 330 19 CMSC 330 20 5

DFA Requirements Can not have more than one transition leaving a state on the same symbol I.e., transition function must be a valid function a a Can not have transitions with empty labels Transitions must be labeled by alphabet symbols ε ε-transition NFAs do not have these requirements! DFA is a special case of NFA CMSC 330 21 Nondeterministic Finite Automata (NFA) An NFA is a 5-tuple (Σ, Q, q 0, F, δ) where Σ is an alphabet Q is a nonempty set of states q 0 Q is the start state F Q is the set of final states δ Q x (Σ {ε}) x Q specifies the NFA's transitions Transitions on ε are allowed can optionally take these transitions without consuming any input Can have more than one transition for a given state and symbol An NFA accepts s if there is at least one path from its start to final state on s CMSC 330 22 NFA for (a b)*abb Another example DFA ba Has paths to either S0 or S1 Neither is final, so rejected babaabb Has paths to different states One leads to S3, so accepted Language? (ab aba)* CMSC 330 23 CMSC 330 24 6

NFA for (ab aba)* Relating REs to DFAs and NFAs Regular expressions, NFAs, and DFAs accept the same languages! can transform DFA NFA aba Has paths to states S0, S1 can transform can transform ababa Has paths to S0, S1 Need to use ε-transition RE CMSC 330 25 CMSC 330 26 Reducing Regular Expressions to NFAs Reduction (cont.) Goal: Given regular expression e, construct NFA: <e> = (Σ, Q, q 0, F, δ) Remember regular expressions are defined recursively from primitive RE languages Invariant: F = 1 in our NFAs Recall F = set of final states Base case: ε <ε> = (ε, {S0}, S0, {S0}, ) Base case: Base case: a <a> = ({a}, {S0, S1}, S0, {S1}, {(S0, a, S1)} ) < > = (, {S0, S1}, S0, {S1}, ) CMSC 330 27 CMSC 330 28 7

Reduction: Concatenation Reduction: Concatenation (cont.) Induction: AB Induction: AB <A> <B> <A> <B> <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) <AB> = (Σ A Σ B, Q A Q B, q A, {f B }, δ A δ B {(f A, ε, q B )} ) CMSC 330 29 CMSC 330 30 Reduction: Union Reduction: Union (cont.) Induction: (A B) Induction: (A B) <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) <(A B)> = (Σ A Σ B, Q A Q B {S0,S1}, S0, {S1}, δ A δ B {(S0,ε,q A ), (S0,ε,q B ), (f A,ε,S1), (f B,ε,S1)}) CMSC 330 31 CMSC 330 32 8

Reduction: Closure Induction: A* Reduction: Closure (cont.) Induction: A* <A> = (Σ A, Q A, q A, {f A }, δ A ) <A*> = (Σ A, Q A {S0,S1}, S0, {S1}, δ A {(f A,ε,S1), (S0,ε,q A ), (S0,ε,S1), (S1,ε,S0)}) CMSC 330 33 CMSC 330 34 Reduction Complexity Practice Given a regular expression A of size n... Size = # of symbols + # of operations How many states does <A> have? 2 added for each, 2 added for each * O(n) That s pretty good! Draw NFAs for the following regular expressions and languages (0 1)*110* 101* 111 all binary strings ending in 1 (odd numbers) (ab*c d*a ab)d CMSC 330 35 CMSC 330 36 9

Summary Finite automata Deterministic (DFA) Non-deterministic (NFA) Questions How are DFAs and NFAs different? When does an NFA accept a string? How to convert regular expression to an NFA? CMSC 330 37 10