CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.

Slides:



Advertisements
Similar presentations
Non-Deterministic Finite Automata
Advertisements

4b Lexical analysis Finite Automata
CSC 361NFA vs. DFA1. CSC 361NFA vs. DFA2 NFAs vs. DFAs NFAs can be constructed from DFAs using transitions: Called NFA- Suppose M 1 accepts L 1, M 2 accepts.
Complexity and Computability Theory I Lecture #4 Rina Zviel-Girshin Leah Epstein Winter
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
Regular Expressions and DFAs COP 3402 (Summer 2014)
Finite Automata CPSC 388 Ellen Walker Hiram College.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Compiler Construction
Finite Automata Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture 20Oct 28, 2010Carnegie Mellon University.
Introduction to Computability Theory
Lecture 3UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 3.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
CS5371 Theory of Computation
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
1 The scanning process Goal: automate the process Idea: –Start with an RE –Build a DFA How? –We can build a non-deterministic finite automaton (Thompson's.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
Introduction to Finite Automata Adapted from the slides of Stanford CS154.
CS Chapter 2. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Fall 2004COMP 3351 Another NFA Example. Fall 2004COMP 3352 Language accepted (redundant state)
Costas Busch - LSU1 Non-Deterministic Finite Automata.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
1 Non-Deterministic Finite Automata. 2 Alphabet = Nondeterministic Finite Automaton (NFA)
Formal Language Finite set of alphabets Σ: e.g., {0, 1}, {a, b, c}, { ‘{‘, ‘}’ } Language L is a subset of strings on Σ, e.g., {00, 110, 01} a finite language,
Topic #3: Lexical Analysis
Basics of automata theory
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
CS311: Computational Theory
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
REGULAR LANGUAGES.
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
By: Er. Sukhwinder kaur.  What is Automata Theory? What is Automata Theory?  Alphabet and Strings Alphabet and Strings  Empty String Empty String 
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
CHAPTER 1 Regular Languages
CMSC 330: Organization of Programming Languages Theory of Regular Expressions NFAs  DFAs.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
Deterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.2)
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Finite Automata Chapter 1. Automatic Door Example Top View.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
Lecture Notes 
CS 154 Formal Languages and Computability February 9 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
Chapter 5 Finite Automata Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
Finite Automata A simple model of computation. 2 Finite Automata2 Outline Deterministic finite automata (DFA) –How a DFA works.
1 Section 11.2 Finite Automata Can a machine(i.e., algorithm) recognize a regular language? Yes! Deterministic Finite Automata A deterministic finite automaton.
Deterministic Finite Automata Nondeterministic Finite Automata.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
June 13, 2016 Prof. Abdelaziz Khamis 1 Chapter 2 Scanning – Part 2.
Topic 3: Automata Theory 1. OutlineOutline Finite state machine, Regular expressions, DFA, NDFA, and their equivalence, Grammars and Chomsky hierarchy.
Lexical analysis Finite Automata
Non Deterministic Automata
Chapter 2 Finite Automata
Two issues in lexical analysis
Recognizer for a Language
Review: NFA Definition NFA is non-deterministic in what sense?
CS 154, Lecture 3: DFANFA, Regular Expressions.
Principles of Computing – UFCFA3-30-1
COSC 3340: Introduction to Theory of Computation
CSE322 Definition and description of finite Automata
Non Deterministic Automata
CS 350 — Fall 2018 gilray.org/classes/fall2018/cs350/
Finite Automata Reading: Chapter 2.
4b Lexical analysis Finite Automata
4b Lexical analysis Finite Automata
Chapter 1 Regular Language
Presentation transcript:

CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs

CMSC 3302 Review How are DFAs and NFAs different? When does an NFA accept a string? How do we convert from a regular expression to an NFA? What is the  -closure of a state?

CMSC 3303 Relating REs to DFAs and NFAs DFANFA RE can transform can transform can transform (we’ll discuss this next)

CMSC 3304 Reduction Complexity Regular expression to NFA reduction: –O(n) NFA to DFA reduction –Intuition: Build DFA where each DFA state represents a set of NFA states –How many states could there be in the DFA? –Given NFA with n states, DFA may have 2 n states –This is not so good, but we can implement DFAs more easily

CMSC 3305 NFA  DFA reduction Example: {A} {B,D} a b {C,D}

CMSC 3306 NFA  DFA reduction Algorithm Let r 0 =  -closure(q 0 ), R = {r 0 },  R = ∅ While there is an unmarked state r i in R –Mark r i –For each a   Let S = {s | {q, a, s}  and q  r i } Let E =  -closure(S) If E  R, then R = R  E  R =  R  {r i, a, E} Let F R = {r i |  s  r i with s  q f } Notes: Let ( , Q, q 0, F Q,  ) be the NFA and ( , R, r 0, F R,  R ) be the DFA. All new states in R are unmarked at creation.

CMSC 3307 NFA  DFA example {A,E} R = {{A,E}, } {B,D,E} a {B,D,E}, {C,D} {C,D}, b b{E} a b a Language? All strings that have exactly 1 b and end in b or the string a Regular expression? a*b|a

CMSC 3308 Practice Convert the NFA to a DFA:

CMSC 3309 Equivalence of DFAs and NFAs Any string from A to either D or CD in the DFA represents a path from A to D in the original NFA.

CMSC Relating RE's to DFAs and NFAs Regular expressions, NFAs, and DFAs accept the same languages! DFANFA RE can transform can transform can transform High-level idea next

CMSC Converting from DFAs to REs General idea: –Remove states one by one, labeling transitions with regular expressions –When two states are left (start and final), the transition label is the regular expression for the DFA

CMSC Relating REs to DFAs and NFAs Why do we want to convert between these? –Easier to express ideas (regexes/NFAs) –Easier to implement (DFAs)

CMSC Implementing DFAs cur_state = 0; while (1) { symbol = getchar(); switch (cur_state) { case 0: switch (symbol) { case '0': cur_state = 0; break; case '1': cur_state = 1; break; case '\n': printf("rejected\n"); return 0; default: printf("rejected\n"); return 0; } break; case 1: switch (symbol) { case '0': cur_state = 0; break; case '1': cur_state = 1; break; case '\n': printf("accepted\n"); return 1; default: printf("rejected\n"); return 0; } break; default: printf("unknown state; I'm confused\n"); break; } It's easy to build a program which mimics a DFA

CMSC Implementing DFAs (Alternative) Alternatively, use generic table-driven DFA –q is just an integer –Represent δ using 2d array or nested hash tables –Represent F as a set given components (Σ, Q, q 0, F, δ) of a DFA: let q = q 0 while (there exists another symbol s of the input string) q := δ(q, s); if q ∊  F then accept else reject

CMSC Run Time of Algorithm Given a string s, how long does algorithm take to decide whether s is accepted? –Assume we can compute δ(q0, c) in constant time –Time per string s to determine acceptance is O(|s|) –Can’t get much faster! But recall that constructing the DFA from the regular expression A may take O(2 |A| ) time –But this is usually not the case in practice So there’s the initial overhead, but then accepting strings is fast

CMSC Regular Expressions in Practice Regular expressions are typically “compiled” into tables for the generic algorithm –Can think of this as a simple byte code interpreter –But really just a representation of (Σ, Q A, q A, {f A }, δ A ), the components of the DFA produced from the RE

CMSC Complement of DFA Given a DFA accepting language L, how can we create a DFA accepting its complement? (the alphabet = {a,b})

CMSC Complement Steps Add implicit transitions to a dead state Change every accepting state to a non- accepting state and every non-accepting state to an accepting state Note: this only works with DFAs - Why?

CMSC Practice Make the DFA which accepts the complement of the language accepted by the DFA below. S3

Review and Practice CMSC 330

21 Deterministic Finite Automaton (DFA) A DFA 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 Σ → Q specifies the DFA's transitions

CMSC Nondeterministic Finite Automaton (NFA) A 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 (Σ  {ε}) → Q specifies the NFA's transitions

CMSC Practice Give the English descriptions and the DFA or regular expression of the following languages: ((0|1)(0|1)(0|1))* (01)*|(10)*|(01)*0|(10)*1

CMSC Practice Give the DFAs for the following languages: All valid strings including only “A”, “C”, “G”, “T” and appearing in multiples of 3 All binary strings containing an even, non-zero length substring of all 1’s All binary strings containing exactly two 1’s All binary strings that start and end with the same number

CMSC Practice Make the DFA which accepts all strings with a substring of 330 Take the complement of this DFA

CMSC Practice 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 Example Find the  -closure for each of the states in this NFA:

CMSC Practice Convert to a DFA: Convert to an NFA and then to a DFA: (0|1)*11|0* aba*|(ba|b)