CS 3240 - Chapter 2. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.

Slides:



Advertisements
Similar presentations
CSE 311 Foundations of Computing I
Advertisements

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)
Finite Automata CPSC 388 Ellen Walker Hiram College.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture2: Non Deterministic Finite Automata Prof. Amos Israeli.
Introduction to Computability Theory
CS 3240 – Chapter 3.  How would you delete all C++ files from a directory from the command line?  How about all PowerPoint files that start with the.
CS5371 Theory of Computation
CS 310 – Fall 2006 Pacific University CS310 Finite Automata Sections:1.1 page 44 September 8, 2006.
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
Cohen, Chapter 61 Introduction to Computational Theory Chapter 6.
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.
CS5371 Theory of Computation Lecture 4: Automata Theory II (DFA = NFA, Regular Language)
FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such.
Automating Construction of Lexers. Example in javacc TOKEN: { ( | | "_")* > | ( )* > | } SKIP: { " " | "\n" | "\t" } --> get automatically generated code.
CSC 361Finite Automata1. CSC 361Finite Automata2 Formal Specification of Languages Generators Grammars Context-free Regular Regular Expressions Recognizers.
Costas Busch - LSU1 Non-Deterministic Finite Automata.
Foundations of (Theoretical) Computer Science Chapter 1 Lecture Notes (Section 1.2: NFA’s) David Martin With some modifications.
CSE 311: Foundations of Computing Fall 2014 Lecture 23: State Minimization, NFAs.
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,
NFA ε - NFA - DFA equivalence. What is an NFA An NFA is an automaton that its states might have none, one or more outgoing arrows under a specific symbol.
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
REGULAR LANGUAGES.
Lecture 05: Theory of Automata:08 Kleene’s Theorem and NFA.
Lexical Analysis Constructing a Scanner from Regular Expressions.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
Lexical Analysis III : NFA to DFA DFA Minimization Lecture 5 CS 4318/5331 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper.
Transition Diagrams Lecture 3 Wed, Jan 21, Building Transition Diagrams from Regular Expressions A regular expression consists of symbols a, b,
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
CS 3240 – Chapter 4.  Closure Properties  Algorithms for Elementary Questions:  Is a given word, w, in L?  Is L empty, finite or infinite?  Are L.
CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.
CS 3813: Introduction to Formal Languages and Automata Chapter 2 Deterministic finite automata These class notes are based on material from our textbook,
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
Lecture 04: Theory of Automata:08 Transition Graphs.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007.
using Deterministic Finite Automata & Nondeterministic Finite Automata
Algorithms for hard problems Automata and tree automata Juris Viksna, 2015.
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:
Regular Languages Chapter 1 Giorgi Japaridze Theory of Computability.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2006.
Theory of Computation Automata Theory Dr. Ayman Srour.
Deterministic Finite Automata Nondeterministic Finite Automata.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
June 13, 2016 Prof. Abdelaziz Khamis 1 Chapter 2 Scanning – Part 2.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Non Deterministic Automata
Chapter 2 Finite Automata
Theory of Computation Lecture # 9-10.
Nondeterministic Finite Automata
Two issues in lexical analysis
Chapter 2 FINITE AUTOMATA.
Hierarchy of languages
Principles of Computing – UFCFA3-30-1
Non Deterministic Automata
Finite Automata.
NFAs and Transition Graphs
Presentation transcript:

CS Chapter 2

LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar Recursively Enumerable Turing MachineUnrestricted Phrase- Structure Grammar 2CS Introduction

 2.1: Deterministic Finite Automata  2.2: Non-deterministic Finite Automata  2.3: Equivalence of DFAs and NFAs  2.4: State Minimization  Removing redundant states CS Finite Automata3

 A Finite State Machine  Determinism: Always traverses the same path and yields the same result for the same input  Consists of:  A finite set of states ▪ Exactly one “start state” ▪ One or more final (“accepting”) states  An input alphabet  A transition function CS Finite Automata4

 A Quintuple:  1) A set of states, Q  2) An input alphabet, Σ  3) A transition function, δ: Q x Σ -> Q  4) An initial state, q 0  5) A set of final states, F ⊆ Q CS Finite Automata5

 M = ({q 0,q 1,q 2 }, {0,1}, δ, q 0, {q 1 })  δ is defined as: δ(q 0,0) = q 0 δ(q 0,1) = q 1 δ(q 1,0) = q 0 δ(q 1,1) = q 2 δ(q 2,0) = q 2 δ(q 2,1) = q 1 CS Finite Automata6

01 -q 0 q0q0 q1q1 +q 1 q0q0 q2q2 q2q2 q2q2 q1q1 CS Finite Automata7

8 What language is this? Each state has exactly 2 out-edges (1 for each letter)

 Strings that have an even number of a’s  Strings that end in ab  Strings that contain aba  Strings over {0, 1} that end in 001 CS Finite Automata9

 Each state is a case in a switch statement  Each state’s code examines a character and changes state accordingly  All of this is in a read-loop  See fa1.cpp  Advantage:  Easy to code  Disadvantage:  Hard-codes the machine CS Finite Automata10

 Transition table is stored in a 2-d array  See fa2.cpp  Advantage:  Even easier to code  Disadvantage:  Hard-codes the table CS Finite Automata11

 Transition table is read at runtime  See fa3.py (with input file fa3.dat)  Advantage:  Can process any DFA  Disadvantage:  Hard to code in a static language  Not so bad in Python, etc. CS Finite Automata12

 Sometimes a state can never be exited  A “black hole” :-) CS Finite Automata13 What language is this?

 If we have a DFA for a language, L, how can we form a DFA for its complement?  Σ * - L  Think of the roles of final states in recognizing strings… CS Finite Automata14

 Find a DFA for the set of all strings except those that contain “001” as a substring  First build a DFA that accepts strings containing “001”  Then invert the “acceptability” of each state  Now all other strings will be accepted CS Finite Automata15

CS Finite Automata16 Note that the empty string (λ) is accepted

 A regular language is one that has a DFA that accepts it  We just proved that the complement of a regular language is also regular!  To show that a language is regular, we find a DFA for it  If it isn’t regular, well, that’s another story  Wait for Section 4.3 CS Finite Automata17

 Build a DFA that accepts all strings over {a, b} that start and end in the letter a (but not just “a”)  This one needs a jail  Build a DFA for the language over {a, b} containing an even number of both a’s and b’s CS Finite Automata18

CS Finite Automata19

CS Finite Automata20

CS Finite Automata21

 Consider binary numbers as input strings:  Construct a DFA where each state represents the remainder of the number mod 2 ▪ 2 states representing 0 and 1, respectively ▪ Making the 0–state final accepts even numbers ▪ Making the 1–state final accepts odd numbers  Pretty easy ▪ the last digit read determines the remainder CS Finite Automata22

 Consider binary numbers as input strings:  Construct a DFA where each state represents the remainder of the number mod 3 ▪ Need 3 states representing 0, 1 and 2, respectively ▪ Making the 0–state final accepts numbers ≡ 0 mod 3 ▪ Making the 1–state final accepts numbers ≡ 1 mod 3 ▪ Making the 2–state final accepts numbers ≡ 2 mod 3  Must consider that reading the next bit, b, forms the number 2n+b, where n is the numeric value of the string processed so far before reading b ▪ Remember: n ≡ m mod 3 ⇒ n = 3k+m, 0≤m<3 CS Finite Automata23

 Design a DFA for all strings over {0, 1} that have a 1 in the third position from the end:  For example, 00100, 110, … CS Finite Automata24

 A Non-Deterministic Finite Automaton (NFA) differs from a DFA in that it may have: 1. Zero or more out-edges from a state for the same character ▪ A “Choice” (multiple edges or even leave them out) 2. A move between states can consume no input ▪ Moves “on a whim” (“λ-transitions”)  As long as there exists at least one path to a final state, the corresponding string is accepted CS Finite Automata25

CS Finite Automata26 Note: no out-edges. Not required in an NFA. Any subsequent input crashes the system. Note: 2 out-edges for a

 It is easier to design solutions  They can be converted to an equivalent DFA!  Rabin-Scott algorithm CS Finite Automata27

 A Quintuple:  1) A set of states, Q  2) An input alphabet, Σ  3) A transition function, δ: Q x (Σ ∪ {λ}) -> 2 Q  4) An initial state, q 0  5) A set of final states, F ⊆ Q  Note: DFAs are a special case of NFAs CS Finite Automata28

CS Finite Automata29 “Free ride” from 1 to 2

 Strings that contain aa  Strings that contain aa or bb  Strings that begin and end with the same letter  (ab + aba)* CS Finite Automata30

 Start with the start state  Could be a composite ▪ Out-going λ-transitions give a “free ride”!  See where each character takes you  May be a set of states (we track all simultaneously)  May be nowhere (this will be the jail in the DFA)  Repeat until there’s no place to go!  I find it easier to use transition tables  vs. transition graphs CS Finite Automata31

 The lambda closure of a state in a NFA is the set of states containing:  The state itself  All states reachable from the state by a lambda transition  When you enter a state in an NFA, you can clearly reach any state in its lambda closure CS Finite Automata32

abc 0{0,1,2} ∅∅ CS Finite Automata33

CS Finite Automata34 abc 0{0,1,2} ∅∅ 2{1,2}

abc 0{0,1,2} ∅∅ CS Finite Automata35 abc 0{0,1,2} ∅∅ 2{1,2} 2 ∅ 2 ∅ ∅ 2

abc 0{0,1,2} ∅∅ CS Finite Automata36 abc - 0{0,1,2} ∅∅ + {0,1,2}{0,1,2}2{1,2} + 2 ∅ 2 ∅ + {1,2} ∅ 2{1,2}

 Begin with the lambda closure of the original start state as the new start state  This is your initial “current state”  For each state in the current (composite) state:  For each original transition leaving the current original state, add the states in the lambda closure of the corresponding target state to the result state  Continue until no more new (composite) states emerge  Any transitions that go “nowhere” go to the jail state  Final states are those with any original final state CS Finite Automata37

CS Finite Automata38 We’ll do this “by hand”…

 NFAs leave out states and edges that don’t contribute to the language  When taking a complement, you need those missing things!  The non-determinism complicates matters as well  Only DFAs can be complemented by inverting the acceptability of each state  So convert the NFA to a DFA first CS Finite Automata39

CS Finite Automata40

 A DFA can have redundant states  Often happens when converting from an NFA  Sometimes it’s easy to remove/combine them by inspection  Sometimes it’s not!  There is an algorithm to determine whether states are distinguishable CS Finite Automata41

CS Finite Automata42

CS Finite Automata43

 States p and q are indistinguishable if, starting in p and q, every string leads to the same state of “finality” (i.e., the strings fail or succeed together)  δ*(p,w) ∈ F ➯ δ*(q,w) ∈ F, and  δ*(p,w) ∉ F ➯ δ*(q,w) ∉ F  for all strings w  So we start with strings of length 0  then length 1, length 2…  We stop if any string shows p and q distinguishable CS Finite Automata44

 Mark all final states distinguishable from non- final states (strings of length 0 distinguish these states, obviously)  Repeat until no new unmarked pairs are marked distinguishable:  For all unmarked pairs of states, (p,q):  For each letter, c, of the alphabet Σ: ▪ If δ(p,c) and δ(q,c) are distinguishable, mark p and q distinguishable  Combine each group of remaining mutually indistinguishable states into a single state CS Finite Automata45

CS Finite Automata46

 Start by grouping final vs. non-final states:  {q 2, q 4 } vs. {q 0, q 1, q 3 }  Mark all 6 pairings between these groups distinguishable: CS Finite Automata47 q0q0 q1q1 q2q2 q3q3 q4q4 q0q0 xx q1q1 xx q2q2 x q3q3 x q4q4

 Check remaining unmarked pairs:  (q 2,q 4 ): δ(q 2,0) = q 1, δ(q 4,0) = q 4, => distinguishable  (q 0,q 1 ): δ(q 0,0) = q 1, δ(q 1,0) = q 2, => distinguishable  (q 0,q 3 ): δ(q 0,0) = q 1, δ(q 3,0) = q 2, => distinguishable  (q 1,q 3 ): δ(q 1,0) = δ(q 3,0) and δ(q 1,1) = δ(q 3,1), => indistinguishable CS Finite Automata48 q0q0 q1q1 q2q2 q3q3 q4q4 q0q0 xxxx q1q1 xx q2q2 xx q3q3 x q4q4

CS Finite Automata49

 Minimize the machine on slide 44  don’t combine the jails ahead of time, just for fun CS Finite Automata50

 What if two states, p and q, say, are indistinguishable, and also states q and r are indistinguishable? CS Finite Automata51