Regular expressions and Kleene s theorem

Similar documents
Regular expressions and Kleene s theorem

Regular expressions and Kleene s theorem

Closure under the Regular Operations

Deterministic Finite Automaton (DFA)

Kleene Algebra and Arden s Theorem. Anshul Kumar Inzemamul Haque

Constructions on Finite Automata

What Is a Language? Grammars, Languages, and Machines. Strings: the Building Blocks of Languages

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Regular Expressions. Definitions Equivalence to Finite Automata

Lecture 3: Nondeterministic Finite Automata

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 Lecture 2 1

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

Constructions on Finite Automata

Automata: a short introduction

The Pumping Lemma: limitations of regular languages

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

Computational Models Lecture 2 1

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

Regular Expressions and Language Properties

Lecture 2: Connecting the Three Models

GEETANJALI INSTITUTE OF TECHNICAL STUDIES, UDAIPUR I

Sri vidya college of engineering and technology

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

Nondeterministic Finite Automata. Nondeterminism Subset Construction

UNIT-III REGULAR LANGUAGES

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

1 Alphabets and Languages

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

UNIT II REGULAR LANGUAGES

CMSC 330: Organization of Programming Languages

COMP4141 Theory of Computation

CMPSCI 250: Introduction to Computation. Lecture #23: Finishing Kleene s Theorem David Mix Barrington 25 April 2013

Inf2A: Converting from NFAs to DFAs and Closure Properties

CS 455/555: Mathematical preliminaries

Foundations of

CS21 Decidability and Tractability

Regular Expressions (Pre Lecture)

Computational Models - Lecture 1 1

3515ICT: Theory of Computation. Regular languages

Closure Properties of Regular Languages. Union, Intersection, Difference, Concatenation, Kleene Closure, Reversal, Homomorphism, Inverse Homomorphism

CMPSCI 250: Introduction to Computation. Lecture #29: Proving Regular Language Identities David Mix Barrington 6 April 2012

Nondeterminism. September 7, Nondeterminism

Nondeterministic Finite Automata

Equivalence of Regular Expressions and FSMs

CS 121, Section 2. Week of September 16, 2013

Recap from Last Time

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

Formal Language and Automata Theory (CS21004)

Introduction to the Theory of Computation. Automata 1VO + 1PS. Lecturer: Dr. Ana Sokolova.

This Lecture will Cover...

Uses of finite automata

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

TAFL 1 (ECS-403) Unit- II. 2.1 Regular Expression: The Operators of Regular Expressions: Building Regular Expressions

Nondeterministic finite automata

Deterministic finite Automata

Introduction to the Theory of Computation. Automata 1VO + 1PS. Lecturer: Dr. Ana Sokolova.

CS311 Computational Structures Regular Languages and Regular Expressions. Lecture 4. Andrew P. Black Andrew Tolmach

CS 154, Lecture 3: DFA NFA, Regular Expressions

CS 455/555: Finite automata

Computational Models #1

Concatenation. The concatenation of two languages L 1 and L 2

Axioms of Kleene Algebra

Turing machines and linear bounded automata

Equivalence of DFAs and NFAs

Finite Automata and Formal Languages TMV026/DIT321 LP4 2012

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

Regular expressions. Regular expressions. Regular expressions. Regular expressions. Remark (FAs with initial set recognize the regular languages)

Non-Deterministic Finite Automata

Context Free Grammars

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

Formal Languages, Automata and Models of Computation

컴파일러입문 제 3 장 정규언어

Finite-State Machines (Automata) lecture 12

Lecture 7 Properties of regular languages

Regular Expression Unit 1 chapter 3. Unit 1: Chapter 3

Closure Properties of Context-Free Languages. Foundations of Computer Science Theory

Theory of Computation (I) Yijia Chen Fudan University

Decision, Computation and Language

CS 154. Finite Automata, Nondeterminism, Regular Expressions

CS21 Decidability and Tractability

Turing machines and linear bounded automata

Languages. A language is a set of strings. String: A sequence of letters. Examples: cat, dog, house, Defined over an alphabet:

Regular Expressions [1] Regular Expressions. Regular expressions can be seen as a system of notations for denoting ɛ-nfa

Turing machines and linear bounded automata

Chapter Five: Nondeterministic Finite Automata

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

CONCATENATION AND KLEENE STAR ON DETERMINISTIC FINITE AUTOMATA

Critical CS Questions

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

Kleene Algebras and Algebraic Path Problems

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Theory of computation: initial remarks (Chapter 11)

Non-deterministic Finite Automata (NFAs)

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

Harvard CS121 and CSCI E-121 Lecture 2: Mathematical Preliminaries

Formal Models in NLP

CMSC 330: Organization of Programming Languages

Transcription:

and Kleene s theorem Informatics 2A: Lecture 5 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 29 September 2016 1 / 21

1 More closure properties of regular languages Operations on languages ɛ-nfas Closure under concatenation and Kleene star 2 From regular expressions to regular languages 3 Kleene s theorem Kleene algebra From DFAs to regular expressions 2 / 21

Concatenation Operations on languages ɛ-nfas Closure under concatenation and Kleene star We write L 1.L 2 for the concatenation of languages L 1 and L 2, defined by: L 1.L 2 = {xy x L 1, y L 2 } For example, if L 1 = {aaa} and L 2 = {b, c} then L 1.L 2 is the language {aaab, aaac}. Later we will prove the following closure property. If L 1 and L 2 are regular languages then so is L 1.L 2. 3 / 21

Kleene star More closure properties of regular languages Operations on languages ɛ-nfas Closure under concatenation and Kleene star We write L for the Kleene star of the language L, defined by: L = {ɛ} L L.L L.L.L... For example, if L 3 = {aaa, b} then L 3 contains strings like aaaaaa, bbbbb, baaaaaabbaaa, etc. More precisely, L 3 contains all strings over {a, b} in which the letter a always appears in sequences of length some multiple of 3 Later we will prove the following closure property. If L is a regular language then so is L. 4 / 21

Exercise More closure properties of regular languages Operations on languages ɛ-nfas Closure under concatenation and Kleene star Consider the language over the alphabet {a, b, c} L = {x x starts with a and ends with c} Which of the following strings are valid for the language L.L? 1 abcabc 2 acacac 3 abcbcac 4 abcbacbc 5 / 21

Exercise More closure properties of regular languages Operations on languages ɛ-nfas Closure under concatenation and Kleene star Consider the language over the alphabet {a, b, c} L = {x x starts with a and ends with c} Which of the following strings are valid for the language L.L? 1 abcabc 2 acacac 3 abcbcac 4 abcbacbc Answer: 1,2,3 are valid, but 4 isn t. (To split the string into two L-strings, we d need c followed by a.) 5 / 21

Another exercise Operations on languages ɛ-nfas Closure under concatenation and Kleene star Consider the (same) language over the alphabet {a, b, c} L = {x x starts with a and ends with c} Which of the following strings are valid for the language L? 1 ɛ 2 acaca 3 abcbc 4 acacacacac 6 / 21

Another exercise Operations on languages ɛ-nfas Closure under concatenation and Kleene star Consider the (same) language over the alphabet {a, b, c} L = {x x starts with a and ends with c} Which of the following strings are valid for the language L? 1 ɛ 2 acaca 3 abcbc 4 acacacacac Answer: 1,3,4 are valid, but not 2. (In this particular case, it so happens that L = L + {ɛ}, but this won t be true in general.) 6 / 21

NFAs with ɛ-transitions Operations on languages ɛ-nfas Closure under concatenation and Kleene star We can vary the definition of NFA by also allowing transitions labelled with the special symbol ɛ (not a symbol in Σ). The automaton may (but doesn t have to) perform a spontaneous ɛ-transition at any time, without reading an input symbol. This is quite convenient: for instance, we can turn any NFA into an ɛ-nfa with just one start state and one accepting state: ε ε ε............... ε ε ε (Add ɛ-transitions from new start state to each state in S, and from each state in F to new accepting state.) 7 / 21

Equivalence to ordinary NFAs Operations on languages ɛ-nfas Closure under concatenation and Kleene star Allowing ɛ-transitions is just a convenience: it doesn t fundamentally change the power of NFAs. If N = (Q,, S, F ) is an ɛ-nfa, we can convert N to an ordinary NFA with the same associated language, by simply expanding and S to allow for silent ɛ-transitions. To achieve this, perform the following steps on N. For every pair of transitions q a q (where a Σ) and q ɛ q, add a new transition q a q. For every transition q ɛ q, where q is a start state, make q a start state too. Repeat the two steps above until no further new transitions or new start states can be added. Finally, remove all ɛ-transitions from the ɛ-nfa resulting from the above process. This produces the desired NFA. 8 / 21

Closure under concatenation Operations on languages ɛ-nfas Closure under concatenation and Kleene star We use ɛ-nfas to show, as promised, that regular languages are closed under the concatenation operation: L 1.L 2 = {xy x L 1, y L 2 } If L 1, L 2 are any regular languages, choose ɛ-nfas N 1, N 2 that define them. As noted earlier, we can pick N 1 and N 2 to have just one start state and one accepting state. Now hook up N 1 and N 2 like this: N1 ε N2 Clearly, this NFA corresponds to the language L 1.L 2. 9 / 21

Closure under Kleene star Operations on languages ɛ-nfas Closure under concatenation and Kleene star Similarly, we can now show that regular languages are closed under the Kleene star operation: L = {ɛ} L L.L L.L.L... For suppose L is represented by an ɛ-nfa N with one start state and one accepting state. Consider the following ɛ-nfa: ε N Clearly, this ɛ-nfa corresponds to the language L. ε 10 / 21

From regular expressions to regular languages We ve been looking at ways of specifying regular languages via machines (often presented as pictures). But it s very useful for applications to have more textual ways of defining languages. A regular expression is a written mathematical expression that defines a language over a given alphabet Σ. The basic regular expressions are ɛ a (for a Σ) From these, more complicated regular expressions can be built up by (repeatedly) applying the two binary operations +,. and the unary operation. Example: (a.b + ɛ) + a We use brackets to indicate precedence. In the absence of brackets, binds more tightly than., which itself binds more tightly than +. So a + b.a means a + (b.(a )) Also the dot is often omitted: ab means a.b 11 / 21

From regular expressions to regular languages How do regular expressions define languages? A regular expression is itself just a written expression. However, every regular expression α over Σ can be seen as defining an actual language L(α) Σ in the following way. L( ) =, L(ɛ) = {ɛ}, L(a) = {a}. L(α + β) = L(α) L(β) L(α.β) = L(α). L(β) L(α ) = L(α) Example: a + ba defines the language {a, b, ba, baa, baaa,...}. The languages defined by, ɛ, a are obviously regular. What s more, we ve seen that regular languages are closed under union, concatenation and Kleene star. This means every regular expression defines a regular language. (Formal proof by induction on the size of the regular expression.) 12 / 21

Exercises More closure properties of regular languages From regular expressions to regular languages Consider (again) the language {x {0, 1} x contains an even number of 0 s} Which of the following regular expressions define the above language? 1 (1 01 01 ) 2 (1 01 0) 1 3 1 (01 0) 1 4 (1 + 01 0) 13 / 21

Exercises More closure properties of regular languages From regular expressions to regular languages Consider (again) the language {x {0, 1} x contains an even number of 0 s} Which of the following regular expressions define the above language? 1 (1 01 01 ) 2 (1 01 0) 1 3 1 (01 0) 1 4 (1 + 01 0) Answer: 2 and 4 define the required language. 1 doesn t: e.g. 11 doesn t match the expression. 3 doesn t: e.g. 00100 doesn t match the expression. 13 / 21

Kleene s theorem Kleene s theorem Kleene algebra From DFAs to regular expressions We ve seen that every regular expression defines a regular language. Remarkably, the converse is also true: every regular language can be defined by a regular expression. The equivalence between regular languages and expressions is: Kleene s theorem DFAs and regular expressions give rise to exactly the same class of languages (the regular languages). (For proof, see Kozen, Lecture 9.) As we ve already seen, NFAs (with or without ɛ-transitions) also give rise to this class of languages. So the evidence is mounting that the class of regular languages is mathematically a very natural and well-behaved one. 14 / 21

Kleene algebra Kleene s theorem Kleene algebra From DFAs to regular expressions give a textual way of specifying regular languages. This is useful e.g. for communicating regular languages to a computer. Another benefit: regular expressions can be manipulated using algebraic laws (Kleene algebra). For example: α + (β + γ) = (α + β) + γ α + β = β + α α + = α α + α = α α(βγ) = (αβ)γ ɛα = αɛ = α α(β + γ) = αβ + αγ (α + β)γ = αγ + βγ α = α = ɛ + αα = ɛ + α α = α Often these can be used to simplify regular expressions down to more pleasant ones. 15 / 21

Other reasoning principles Kleene s theorem Kleene algebra From DFAs to regular expressions Let s write α β to mean L(α) L(β) (or equivalently α + β = β). Then αγ + β γ α β γ β + γα γ βα γ Arden s rule: Given an equation of the form X = αx + β, its smallest solution is X = α β. What s more, if ɛ L(α), this is the only solution. Beautiful fact: The rules on this slide and the last form a complete set of reasoning principles, in the sense that if L(α) = L(β), then α = β is provable using these rules. (Beyond scope of Inf2A.) 16 / 21

DFAs to regular expressions Kleene s theorem Kleene algebra From DFAs to regular expressions We use an example to show how to convert a DFA to an equivalent regular expression. 1 1 0 p q 0 For each state r, let the variable X r stand for the set of strings that take us from r to an accepting state. Then we can write some simultaneous equations: X p = 1X p + 0X q + ɛ X q = 1X q + 0X p 17 / 21

Where do the equations come from? Kleene s theorem Kleene algebra From DFAs to regular expressions Consider: X p = 1X p + 0X q + ɛ This asserts the following. Any string that takes us from p to an accepting state is: a 1 followed by a string that takes us from p to an accepting state; or a 0 followed by a string that takes us from q to an accepting state; or the empty string. Note that the empty string is included because p is an accepting state. 18 / 21

Solving the equations Kleene s theorem Kleene algebra From DFAs to regular expressions We solve the equations by eliminating one variable at a time: X q = 1 0X p by Arden s rule So X p = 1X p + 01 0X p + ɛ = (1 + 01 0)X p + ɛ So X p = (1 + 01 0) by Arden s rule Since the start state is p, the resulting regular expression for X p is the one we are seeking. Thus the language recognised by the automaton is: (1 + 01 0) The method we have illustrated here, in fact, works for arbitrary NFAs (without ɛ-transitions). 19 / 21

Theory of regular languages: overview Kleene s theorem Kleene algebra From DFAs to regular expressions 20 / 21

Reading More closure properties of regular languages Kleene s theorem Kleene algebra From DFAs to regular expressions Relevant reading: : Kozen chapters 7,8; J & M chapter 2.1. (Both texts actually discuss more general patterns see next lecture.) From regular expressions to NFAs: Kozen chapter 8; J & M chapter 2.3. Kleene algebra: Kozen chapter 9. From NFAs to regular expressions: Kozen chapter 9. Next two lectures: Some applications of all this theory. String and pattern matching Lexical analysis Model checking 21 / 21