Chapter 4: Regular Expressions

Similar documents
CHAPTER TWO LANGUAGES. Dr Zalmiyah Zakaria

Notes for Comp 454 Week 2

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

Glynda, the good witch of the North

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

ITEC2620 Introduction to Data Structures

Welcome! CSC445 Models Of Computation Dr. Lutz Hamel Tyler 251

Strings, Languages, and Regular Expressions

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Proof Techniques Alphabets, Strings, and Languages. Foundations of Computer Science Theory

KHALID PERVEZ (MBA+MCS) CHICHAWATNI

Assignment No.4 solution. Pumping Lemma Version I and II. Where m = n! (n-factorial) and n = 1, 2, 3

Finite Automata Part Three

Chapter Seven: Regular Expressions

CS402 - Theory of Automata FAQs By

Data Compression Fundamentals

CS 3100 Models of Computation Fall 2011 This assignment is worth 8% of the total points for assignments 100 points total.

Regular Languages. Regular Language. Regular Expression. Finite State Machine. Accepts

Compiler Construction

Finite Automata Part Three

Lesson 18: Counting Problems

Regular Expressions & Automata

Multiple Choice Questions

CMSC 132: Object-Oriented Programming II

Lec-5-HW-1, TM basics

CSE 431S Scanning. Washington University Spring 2013

The Kuratowski Closure-Complement Theorem

Key to Homework #8. (a) S aa A bs bbb (b) S AB aaab A aab aaaaaaab B bba bbbb

UNIT I PART A PART B

Finite Automata Part Three

JNTUWORLD. Code No: R

1. Which of the following regular expressions over {0, 1} denotes the set of all strings not containing 100 as a sub-string?

1.3 Functions and Equivalence Relations 1.4 Languages

Chapter 18: Decidability

CS375: Logic and Theory of Computing

Formal Grammars and Abstract Machines. Sahar Al Seesi

On the Parikh-de-Bruijn grid

UNION-FREE DECOMPOSITION OF REGULAR LANGUAGES

Come & Join Us at VUSTUDENTS.net

Alphabets, strings and formal. An introduction to information representation

Introduction to Variables, Terms, and Expressions

DVA337 HT17 - LECTURE 4. Languages and regular expressions

CMPSCI 250: Introduction to Computation. Lecture #36: State Elimination David Mix Barrington 23 April 2014

Languages and Strings. Chapter 2

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

Automata Theory CS S-FR Final Review

Chapter Seven: Regular Expressions. Formal Language, chapter 7, slide 1

Dr. D.M. Akbar Hussain

QUESTION BANK. Unit 1. Introduction to Finite Automata

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

Formal Languages. Grammar. Ryan Stansifer. Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901

Regular Expressions. Regular Expressions. Regular Languages. Specifying Languages. Regular Expressions. Kleene Star Operation

Tilings of the Sphere by Edge Congruent Pentagons

Formal Languages and Automata

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

Bayesian Networks and Decision Graphs

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

THE INTERNATIONAL JOURNAL OF SCIENCE & TECHNOLEDGE

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 2: Lexical Analysis 23 Jan 08

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Regular Expressions. Chapter 6

Section 1.7 Sequences, Summations Cardinality of Infinite Sets

Lexical Analysis - 1. A. Overview A.a) Role of Lexical Analyzer

Weighted Finite Automatas using Spectral Methods for Computer Vision

Section 2.4 Sequences and Summations

Slides for Faculty Oxford University Press All rights reserved.

2010: Compilers REVIEW: REGULAR EXPRESSIONS HOW TO USE REGULAR EXPRESSIONS

Turing Machine Languages

Regular Languages and Regular Expressions

Languages and Finite Automata

AUTOMATA THEORY AND COMPUTABILITY

Learn Smart and Grow with world

AUBER (Models of Computation, Languages and Automata) EXERCISES

Finite-State Transducers in Language and Speech Processing

WFST: Weighted Finite State Transducer. September 12, 2017 Prof. Marie Meteer

Problem Set 2 Solutions

CS 314 Principles of Programming Languages

Context-Free Grammars

A Formal Study of Practical Regular Expressions

Models and Algorithms for Complex Networks

I BSc(Computer Science)[ ] Semester - II Allied:DISCRETE MATHEMATICS - 207D Multiple Choice Questions.

HKN CS 374 Midterm 1 Review. Tim Klem Noah Mathes Mahir Morshed

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

Regular Expressions 1

CMPSCI 250: Introduction to Computation. Lecture #7: Quantifiers and Languages 6 February 2012

Name: Finite Automata

Regular Languages. MACM 300 Formal Languages and Automata. Formal Languages: Recap. Regular Languages

MA/CSSE 474. Today's Agenda

TOPIC PAGE NO. UNIT-I FINITE AUTOMATA

2. λ is a regular expression and denotes the set {λ} 4. If r and s are regular expressions denoting the languages R and S, respectively

Converting a DFA to a Regular Expression JP

Don t Let Uncle Sam Pick Your Pocket Again. Plan Now for next year. Call for a free half-hour consultation.

Phil 320 Chapter 1: Sets, Functions and Enumerability I. Sets Informally: a set is a collection of objects. The objects are called members or

8 ε. Figure 1: An NFA-ǫ

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

LECTURE NOTES THEORY OF COMPUTATION

Transcription:

CSI 3104 /Winter 2011: Introduction to Formal Languages What are the languages with a finite representation? We start with a simple and interesting class of such languages. Dr. Nejib Zaguia CSI3104-W11 1 A new method to define languages alphabet language S = {x} S* = {Λ, x, xx, xxx, } or directly {x}* = {Λ, x, xx, xxx, } language language S = {xx, xxx} S* = {Λ, xx, xxx, xxxx, } or directly {xx, xxx}* = {Λ, xx, xxx, xxxx, } letter language x* (written in bold) language(x*) = {Λ, x, xx, xxx, } or informally x* = {Λ, x, xx, xxx, } Dr. Nejib Zaguia CSI3104-W11 2 L1 = {a, ab, abb, abbb, } or simply (ab*) L2 = {Λ, ab, abab, ababab, } or simply (ab)* Several ways to express the same language {x, xx, xxx, xxxx, } xx* x + xx*x* x*xx* (x + )x* x*(x + ) x*x*xx* L3= {Λ, a, b, aa, ab, bb, aaa, aab, abb, bbb, aaaa, } or simply (a*b*) (a s before b s) Remark: language(a*b*) language((ab)*) Dr. Nejib Zaguia CSI3104-W11 3 1

Example: S-ODD Rule 1: x S-ODD Rule 2: If w is in S-ODD then xxw is in S-ODD S-ODD = language(x(xx)*) S-ODD = language((xx)*x) But not: S-ODD = language(x*xx*) xx x x Dr. Nejib Zaguia CSI3104-W11 4 A useful symbol to simplify the writing: x + y choose either x or y Example: S = {a, b, c} T = {a, c, ab, cb, abb, cbb, abbb, cbbb, } T = language((a+c)b*) (defines the language whose words are constructed from either a or c followed by some b s) Dr. Nejib Zaguia CSI3104-W11 5 L = {aaa, aab, aba, abb, baa, bab, bba, bbb} all words of exactly three letters from the alphabet {a, b} L = (a+b)(a+b)(a+b) (a+b)* all words formed from alphabet {a,b} a(a+b)* =? a(a+b)*b =? Dr. Nejib Zaguia CSI3104-W11 6 2

Definition: Given an alphabet S, the set of regular expressions is defined by the following rules. 1. For every letter in S, the letter written in bold is a regular expression. Λ is a regular expression. 2. If r 1 and r 2 are regular expressions, then so are: 1. (r 1 ) 2. r 1 r 2 3. r 1 +r 2 4. r 1 * 3. Nothing else is a regular expression. Dr. Nejib Zaguia CSI3104-W11 7 Remark: Notice that r 1+ = r 1 r 1 * r 1 =r 2 if and only if language(r 1 ) = language(r 2 ) Example: (a+b)*a(a+b)* All words that have at least one a. abbaab: (Λ)a(bbaab) (abb)a(ab) (abba)a(b) Words with no a s? b* All words formed from {a,b}? (a+b)*a(a+b)* + b* Thus: (a+b)* = (a+b)*a(a+b)* + b* Dr. Nejib Zaguia CSI3104-W11 8 Example: The language of all words that have at least two a s. (a+b)*a(a+b)*a(a+b)* = b*ab*a(a+b)* = (a+b)*ab*ab* = b*a(a+b)*ab* Example: The language of all words that have exactly two a s. b*ab*ab* Dr. Nejib Zaguia CSI3104-W11 9 3

Another Example: At least one a and one b? First solution: (a+b)*a(a+b)*b(a+b)* + (a+b)*b(a+b)*a(a+b)* But (a+b)*a(a+b)*b(a+b)* expresses all words except words of the form some b s (at least one) followed by some a s (at least one). bb*aa* Second solution: (a+b)*a(a+b)*b(a+b)* + bb*aa* Thus: (a+b)*a(a+b)*b(a+b)* + (a+b)*b(a+b)*a(a+b)* = (a+b)*a(a+b)*b(a+b)* + bb*aa* Dr. Nejib Zaguia CSI3104-W11 10 The only words that do not contain both an a and b in them are the words formed from all a s or all b s: a*+b* Thus: (a+b)* = (a+b)*a(a+b)*b(a+b)* + bb*aa* + a* + b* Dr. Nejib Zaguia CSI3104-W11 11 Example: The language of all words formed from some b s (possibly 0) and all words where an a is followed by some b s (possibly 0): {Λ, a, b, ab, bb, abb, bbb, abbb, bbbb, } b* + ab* (Λ + a)b* In general: concatenation is distributive over the + operation. r 1 (r 2 + r 3 ) = r 1 r 2 + r 1 r 3 (r 1 + r 2 ) r 3 = r 1 r 3 + r 2 r 3 Dr. Nejib Zaguia CSI3104-W11 12 4

Example of the distributivity rule: (a+c)b* = ab*+cb* 2 operations: language(s) language If S and T are two languages from the same alphabet S, 1. S+T: the union of languages S and T defined as S T 2. ST: the product set is the set of words x written vw with v a word in S and w a word in T. Example: S = {a, bb} T = {a, ab} ST = {aa, aab, bba, bbab} Dr. Nejib Zaguia CSI3104-W11 13 Language associated with a regular expression is defined by the following rules. 1. The language associated with a regular expression that is just a single letter is that one-letter word alone. The language associated with Λ is {Λ}. 2. If L 1 is the language associated with the regular expression r 1 et L 2 is the language associated with the regular expression r 2 : (i) The product L 1 L 2 is the language associated with the regular expression r 1 r 2, that is: language(r 1 r 2 ) = L 1 L 2 (ii) The union L 1 +L 2 is the language associated with the regular expression r 1 +r 2, that is: language(r +r 2 ) = L 1 +L 2 (iii) The Kleene closure of L 1, written L 1 *, is the language associated with the regular expression r 1 *, that is language(r 1 *) = L 1 * Dr. Nejib Zaguia CSI3104-W11 14 Remark: For all regular expressions, there is some language associated with it. Finite Languages are Regular Let L be a finite language. There is a regular expression that defines it. Algorithm (and proof) Write each letter in L in bold, and write a + between regular expressions Dr. Nejib Zaguia CSI3104-W11 15 5

Example: L = {baa, abbba, bababa} baa + abbba + bababa The regular expression that is defined by this algorithm is not necessarily unique. Example: L = {aa, ab, ba, bb} aa + ab + ba + bb or (a+b)(a+b) Remark: This algorithm does not work for infinite languages. Regular expressions must be finite, even if the language defined is infinite. Dr. Nejib Zaguia CSI3104-W11 16 Kleene star applied to a subexpression with a star (a+b*)* (aa+ab*)* (a+b*)* = (a+b)* (aa+ab*)* (aa+ab)* abb abb (a*b*)* The letter a and the letter b are in language(a*b*). (a*b*)* = (a+b)* Is it possible to determine if two regular expressions are equivalent? With a set of algebraic rules? Unknown. With an algorithm? Yes. Dr. Nejib Zaguia CSI3104-W11 17 Examples Words with a double letter: (a+b)*(aa+bb)(a+b)* Words without a double letter: (ab)* But not words that begin with b or end with a: (Λ+b)(ab)*(Λ+a) (a+b)*(aa+bb)(a+b)* + (Λ+b)(ab)*(Λ +a) Dr. Nejib Zaguia CSI3104-W11 18 6

Language EVEN-EVEN defined by the expression: [aa + bb + (ab + ba)(aa+bb)*(ab + ba)]* Every word in EVEN-EVEN has an even number of a s and b s. Every word that contains an even number of a s and b s is a member of EVEN-EVEN. Dr. Nejib Zaguia CSI3104-W11 19 7