Regular Expressions Chapter 6 1. Regular Languages Regular Language Regular Expression Finite State Machine L Accepts 2.

Slides:



Advertisements
Similar presentations
Theory Of Automata By Dr. MM Alam
Advertisements

Finite Automata CPSC 388 Ellen Walker Hiram College.
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Regular Languages and Regular Grammars Chapter 3.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY (For next time: Read Chapter 1.3 of the book)
CS 310 – Fall 2006 Pacific University CS310 Regular Expressions Sections:1.3 page 63 September 18, 2006 September 20, 2006.
CS5371 Theory of Computation Lecture 6: Automata Theory IV (Regular Expression = NFA = DFA)
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such.
Great Theoretical Ideas in Computer Science.
Language Recognizer Connecting Type 3 languages and Finite State Automata Copyright © – Curt Hill.
Regular Expressions. Notation to specify a language –Declarative –Sort of like a programming language. Fundamental in some languages like perl and applications.
Finite State Machines Chapter 5. Languages and Machines.
CMPS 3223 Theory of Computation
Deterministic Finite State Machines Chapter 5. Languages and Machines 2.
1 Unit 1: Automata Theory and Formal Languages Readings 1, 2.2, 2.3.
Regular Expressions and Finite State Automata  Themes  Finite State Automata (FSA)  Describing patterns with graphs  Programs that keep track of state.
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
Introduction to CS Theory Lecture 3 – Regular Languages Piotr Faliszewski
Automata, Computability, & Complexity by Elaine Rich ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Slides provided by author Slides edited for.
Lecture 05: Theory of Automata:08 Kleene’s Theorem and NFA.
Regular Grammars Chapter 7. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
MA/CSSE 474 Theory of Computation Kleene's Theorem Practical Regular Expressions.
Computability Review homework. Regular Operations. Nondeterministic machines. NFSM = FSM Homework: By hand, build FSM version of specific NFSM. Try to.
1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II:
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
MA/CSSE 474 Theory of Computation Regular Expressions Intro.
Regular Expressions and Languages A regular expression is a notation to represent languages, i.e. a set of strings, where the set is either finite or contains.
CHAPTER 1 Regular Languages
Copyright © Curt Hill Finite State Automata Again This Time No Output.
Regular Expressions Chapter 6.
Regular Expressions Chapter 6. Regular Languages Regular Language Regular Expression Finite State Machine Recognizes or Accepts Generates.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
Regular Expressions Chapter 6. Regular Languages Regular Language Regular Expression Finite State Machine L Accepts.
CS 203: Introduction to Formal Languages and Automata
Recursive Definations Regular Expressions Ch # 4 by Cohen
Chapter 3 Regular Expressions, Nondeterminism, and Kleene’s Theorem Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction.
MA/CSSE 474 Theory of Computation Minimizing DFSMs.
Modeling Computation: Finite State Machines without Output
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
Regular Expressions Section 1.3 (also 1.1, 1.2) CSC 4170 Theory of Computation.
using Deterministic Finite Automata & Nondeterministic Finite Automata
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
Conversions Regular Expression to FA FA to Regular Expression.
1 Chapter 3 Regular Languages.  2 3.1: Regular Expressions (1)   Regular Expression (RE):   E is a regular expression over  if E is one of:
 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if.
1 Introduction to the Theory of Computation Regular Expressions.
Nondeterministic Finite State Machines Chapter 5.
Finite State Machines Chapter 5. Languages and Machines.
Regular Languages Chapter 1 Giorgi Japaridze Theory of Computability.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
1 Section 11.2 Finite Automata Can a machine(i.e., algorithm) recognize a regular language? Yes! Deterministic Finite Automata A deterministic finite automaton.
Lecture 09: Theory of Automata:2014 Asif NawazUIIT, PMAS-Arid Agriclture University Rawalpindi. Kleene’s Theorem and NFA.
1 Closure E.g., we understand number systems partly by understanding closure properties: Naturals are closed under +, , but not -, . Integers are closed.
MA/CSSE 474 Theory of Computation How many regular/non-regular languages are there? Closure properties of Regular Languages (if there is time) Pumping.
MA/CSSE 474 Theory of Computation Regular Expressions Intro.
Kleene’s Theorem and NFA
Theory of Computation Lecture #
Practical Regular Expressions
Regular Expressions.
Properties of Regular Languages
Chapter 7 Regular Grammars
Kleene’s Theorem Muhammad Arif 12/6/2018.
MA/CSSE 474 Theory of Computation Nondeterminism NFSMs.
Lecture 5 Scanning.
Presentation transcript:

Regular Expressions Chapter 6 1

Regular Languages Regular Language Regular Expression Finite State Machine L Accepts 2

Regular Expressions The regular expressions over an alphabet  are all and only the strings that can be obtained as follows: 1.  is a regular expression. 2.  is a regular expression. 3. Every element of  is a regular expression. 4. If ,  are regular expressions, then so is . 5. If ,  are regular expressions, then so is . 6. If  is a regular expression, then so is  *. 7.  is a regular expression, then so is  If  is a regular expression, then so is (  ). 3

Regular Expression Examples If  = { a, b }, the following are regular expressions:   a ( a  b )* abba   4

Regular Expressions Define Languages Define L, a semantic interpretation function for regular expressions: 1. L(  ) = . 2. L(  ) = {  }. 3. L(c), where c   = {c}. 4. L(  ) = L(  ) L(  ). 5. L(    ) = L(  )  L(  ). 6. L(  *) = (L(  ))*. 7. L(  + ) = L(  *) = L(  ) (L(  ))*. If L(  ) is equal to , then L(  + ) is also equal to . Otherwise L(  + ) is the language that is formed by concatenating together one or more strings drawn from L(  ). 8. L((  )) = L(  ). 5

The Role of the Rules Rules 1, 3, 4, 5, and 6 give the language its power to define sets. Rule 8 has as its only role grouping other operators. Rules 2 and 7 appear to add functionality to the regular expression language, but they don’t. 2.  is a regular expression. 7.  is a regular expression, then so is  +. 6

Analyzing a Regular Expression L(( a  b )* b ) = L(( a  b )*) L( b ) = (L(( a  b )))* L( b ) = (L( a )  L( b ))* L( b ) = ({ a }  { b })* { b } = { a, b }* { b }. 7

Examples L( a * b * ) = L( ( a  b )* ) = L( ( a  b )* a * b * ) = L( ( a  b )* abba ( a  b )* ) = 8

Going the Other Way L = {w  { a, b }*: |w| is even} 9

Going the Other Way L = {w  { a, b }*: |w| is even} (( a  b ) ( a  b ))* ( aa  ab  ba  bb )* 10

Going the Other Way L = {w  { a, b }*: |w| is even} (( a  b ) ( a  b ))* ( aa  ab  ba  bb )* L = {w  { a, b }*: w contains an odd number of a ’s} 11

Going the Other Way L = {w  { a, b }*: |w| is even} (( a  b ) ( a  b ))* ( aa  ab  ba  bb )* L = {w  { a, b }*: w contains an odd number of a ’s} b * ( ab * ab *)* a b * b * a b * ( ab * ab *)* 12

More Regular Expression Examples L ( ( aa *)   ) = L ( ( a   )* ) = L = {w  { a, b }*: there is no more than one b in w} L = {w  { a, b }* : no two consecutive letters in w are the same} 13

Common Idioms (  )(  ) optional  ( a  b )*  *, where  = {a, b} 14

Operator Precedence in Regular Expressions RegularArithmeticExpressions HighestKleene starexponentiation concatenationmultiplication Lowestunionaddition a b *  c d *x y 2 + i j 2 15

The Details Matter a *  b *  ( a  b )* ( ab )*  a * b * 16

Kleene’s Theorem Finite state machines and regular expressions define the same class of languages. To prove this, we must show: Theorem: Any language that can be defined with a regular expression can be accepted by some FSM and so is regular. Theorem: Every regular language (i.e., every language that can be accepted by some DFSM) can be defined with a regular expression. To prove A = B, we have to prove: 1. A  B and 2. B  A 17

For Every Regular Expression There is a Corresponding FSM We’ll show this by construction. An FSM for:  : 18

For Every Regular Expression There is a Corresponding FSM We’ll show this by construction. An FSM for:  : 19

For Every Regular Expression There is a Corresponding FSM We’ll show this by construction. An FSM for:  : A single element c of  : 20

For Every Regular Expression There is a Corresponding FSM We’ll show this by construction. An FSM for:  : A single element c of  : 21

For Every Regular Expression There is a Corresponding FSM We’ll show this by construction. An FSM for:  : A single element c of  :  (  *): 22

For Every Regular Expression There is a Corresponding FSM We’ll show this by construction. An FSM for:  : A single element c of  :  (  *): 23

Union If  is the regular expression    and if both L(  ) and L(  ) are regular: 24

Union S3S3   25

Concatenation If  is the regular expression  and if both L(  ) and L(  ) are regular: 26

Concatenation   27

Kleene Star If  is the regular expression  * and if L(  ) is regular: 28

Kleene Star S2S2    29

An Example (b  ab )* An FSM for b An FSM for a An FSM for b An FSM for ab : 30

An Example ( b  ab )* An FSM for ( b  ab ): 31

An Example ( b  ab )* An FSM for ( b  ab )*: 32

The Algorithm regextofsm regextofsm(  : regular expression) = Beginning with the primitive subexpressions of  and working outwards until an FSM for all of  has been built do: Construct an FSM as described above. 33

For Every FSM There is a Corresponding Regular Expression We’ll show this by construction. The key idea is that we’ll allow arbitrary regular expressions to label the transitions of an FSM. 34

A Simple Example Let M be: Suppose we rip out state 2: 35

The Algorithm fsmtoregexheuristic fsmtoregexheuristic(M: FSM) = 1. Remove unreachable states from M. 2. If M has no accepting states then return . 3. If the start state of M is part of a loop, create a new start state s and connect s to M’s start state via an  -transition. 4. If there is more than one accepting state of M or there are any transitions out of any of them, create a new accepting state and connect each of M’s accepting states to it via an  -transition. The old accepting states no longer accept. 5. If M has only one state then return . 6. Until only the start state and the accepting state remain do: 6.1 Select rip (not s or an accepting state). 6.2 Remove rip from M. 6.3 *Modify the transitions among the remaining states so M accepts the same strings. 7. Return the regular expression that labels the one remaining transition from the start state to the accepting state. 36

An Example 1. Create a new initial state and a new, unique accepting state, neither of which is part of a loop. 37

An Example, Continued 2. Remove states and arcs and replace with arcs labelled with larger and larger regular expressions. It’s to create a source and a sink! 38

An Example, Continued Remove state 3: 39

An Example, Continued Remove state 2: 40

An Example, Continued Remove state 1: 41 The goal is to keep the source and the sink only!

It’s not always easy M = 42 Try removing state [2]!

Further Modifications to M Before We Start We require that, from every state other than the accepting state there must be exactly one transition to every state (including itself) except the start state. And into every state other than the start state there must be exactly one transition from every state (including itself) except the accepting state. 1. If there is more than one transition between states p and q, collapse them into a single transition: becomes: 43

Further Modifications to M Before We Start 2. If any of the required transitions are missing, add them: becomes: 44

Ripping Out States 3. Choose a state. Rip it out. Restore functionality. Suppose we rip state 2. 45

What Happens When We Rip? Consider any pair of states p and q. Once we remove rip, how can M get from p to q? ● It can still take the transition that went directly from p to q, or ● It can take the transition from p to rip. Then, it can take the transition from rip back to itself zero or more times. Then it can take the transition from rip to q. 46

Defining R(p, q) After removing rip, the new regular expression that should label the transition from p to q is: R(p, q) /* Go directly from p to q  /* or R(p, rip) /* Go from p to rip, then R(rip, rip)* /* Go from rip back to itself any number of times, then R(rip, q) /* Go from rip to q Without the comments, we have: R = R(p, q)  R(p, rip) R(rip, rip)* R(rip, q) 47

Returning to Our Example R = R(p, q)  R(p, rip) R(rip, rip)* R(p, rip) Let rip be state 2. Then: R (1, 3) = R(1, 3)  R(1, rip)R(rip, rip)*R(rip, 3) = R(1, 3)  R(1, 2)R(2, 2)*R(2, 3) =   a b * a = ab * a 48

Returning to Our Example 49 R (4, 4) = R(4, 3)  R(4, 2)R(2, 2)*R(2, 4) =   b b *  =  14 3  bb * a ab * a b R (4, 3) = R(4, 3)  R(4, 2)R(2, 2)*R(2, 3) =   b b * a = bb * a R (1, 4) = R(1, 4)  R(1, 2)R(2, 2)*R(2, 4) = b  a b *  = b Rip state 4: R (1, 3) = R(1, 3)  R(1, 4)R(4, 4)*R(4, 3) = ab * a  b  * bb * a = ab * a  b  bb * a = ab * a  bbb * a

The Algorithm fsmtoregex fsmtoregex(M: FSM) = 1. M = standardize(M: FSM). 2. Return buildregex(M). standardize(M: FSM) = 1. Remove unreachable states from M. 2. If necessary, create a new start state. 3. If necessary, create a new accepting state. 4. If there is more than one transition between states p and q, collapse them. 5. If any transitions are missing, create them with label . 50

The Algorithm fsmtoregex buildregex(M: FSM) = 1. If M has no accepting states then return . 2. If M has only one state, then return . 3. Until only the start and accepting states remain do: 3.1 Select some state rip of M. 3.2 For every transition from p to q, if both p and q are not rip then do Compute the new label R for the transition from p to q: R (p, q) = R(p, q)  R(p, rip) R(rip, rip)* R(rip, q) 3.3 Remove rip and all transitions into and out of it. 4. Return the regular expression that labels the transition from the start state to the accepting state. 51 The case of p = q should also be considered!

Regular Expression or FSM Kleene’s Theorem –Regular expression  FSM Q: when to use regular expression and when to use FSM to describe a regular language? Order –Regular expression: must specify the order in which a sequence of symbols must occur Phone number, address, etc. –FSM: order doesn’t matter Vending machine, parity checking, etc. Sometimes it’s easier to do it one way, sometimes the other. 52

Sometimes Writing Regular Expressions is Easy No two consecutive letters are the same –(b   )(ab)*(a   ) or (a   )(ba)*(b   ) Floating point number –(   +  -)D + (  .D + )(   (E(   +  -)D + )) where D = (0  1  2  3  4  5  6  7  8  9) 53

Sometimes Building a DFSM is Easy A Special Case of Pattern Matching Suppose that we want to match a pattern that is composed of a set of keywords. Then we can write a regular expression of the form: (  * (k 1  k 2  …  k n )  *) + We can use regextofsm to build an FSM. But … We can instead use buildkeywordFSM. 54

Recognize {cat, bat, cab} The single keyword cat: 55

{cat, bat, cab} Adding bat and cab: 56

{cat, bat, cab} Adding transitions to recover after a path dies: 57

Using Regular Expressions in the Real World Matching numbers Matching IP addresses Scanning valid address Determining legal password Finding doubled words (e.g., “the the” in word processor) Identifying spam 58

Using Substitution Building a chatbot: On input: is the chatbot will reply: Why is ? Example: The food there is awful Why is the food there awful? 59

Simplifying Regular Expressions Regular expression as sets: ● Union is commutative:    =    ● Union is associative: (    )   =   (    ) ●  is the identity for union:    =    =  ● Union is idempotent:    =  ● If B  A, A  B = A Concatenation: ● Concatenation is associative: (  )  =  (  ) ●  is the identity for concatenation:   =   =  ●  is a zero for concatenation:   =   =  Concatenation distributes over union: ● (    )  = (   )  (   ) ●  (    ) = (   )  (   ) 60

Simplifying Regular Expressions Kleene star: ●  * =  ●  * =  ● (  *)* =  * ●  *  * =  * ● If L(  *)  L(  *) then  *  * =  * ● (    )* = (  *  *)* ● If L(  )  L(  *) then (    )* =  * 61

Example ● ((a* U  )*  aa)(b  bb)* b* ((a  b)* b*  ab)* = ((a* )*  aa)(b  bb)* b* ((a  b)* b*  ab)* = (a*  aa)(b  bb)* b* ((a  b)* b*  ab)* = a* (b  bb)* b* ((a  b)* b*  ab)* = a* b* b* ((a  b)* b*  ab)* = a* b* ((a  b)* b*  ab)* = a* b* ((a  b)*  ab)* = a* b* (a  b)* = a* (a  b)* = (a  b)* 62