Formal Languages and Automata

Similar documents
Regular Languages and Regular Expressions

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

Decidable Problems. We examine the problems for which there is an algorithm.

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

Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

Dr. D.M. Akbar Hussain

Chapter Seven: Regular Expressions

DVA337 HT17 - LECTURE 4. Languages and regular expressions

Multiple Choice Questions

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

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

Compiler Construction

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

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

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


ECS 120 Lesson 7 Regular Expressions, Pt. 1

CT32 COMPUTER NETWORKS DEC 2015

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

QUESTION BANK. Unit 1. Introduction to Finite Automata

UNIT I PART A PART B

Learn Smart and Grow with world

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

Regular Languages (14 points) Solution: Problem 1 (6 points) Minimize the following automaton M. Show that the resulting DFA is minimal.

TOPIC PAGE NO. UNIT-I FINITE AUTOMATA

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

Ambiguous Grammars and Compactification

CSE 105 THEORY OF COMPUTATION

Name: Finite Automata

UNION-FREE DECOMPOSITION OF REGULAR LANGUAGES

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

JNTUWORLD. Code No: R

8 ε. Figure 1: An NFA-ǫ

LECTURE NOTES THEORY OF COMPUTATION

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

LECTURE NOTES THEORY OF COMPUTATION

AUTOMATA THEORY AND COMPUTABILITY

Automating Construction of Lexers

Decision Properties for Context-free Languages

Finite Automata Part Three

Lexical Analysis. Prof. James L. Frankel Harvard University

9.5 Equivalence Relations

Converting a DFA to a Regular Expression JP

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

(Refer Slide Time: 0:19)

Languages and Compilers

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Theory Bridge Exam Example Questions Version of June 6, 2008

3 rd Year - Computer Science and Engineering,

Zhizheng Zhang. Southeast University

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

Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Actually talking about Turing machines this time

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

Automata & languages. A primer on the Theory of Computation. The imitation game (2014) Benedict Cumberbatch Alan Turing ( ) Laurent Vanbever

Outline. 1 Scanning Tokens. 2 Regular Expresssions. 3 Finite State Automata

CMPSCI 250: Introduction to Computation. Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.


Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Lexical Analysis 1 / 52

Finite Automata Part Three

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

1.3 Functions and Equivalence Relations 1.4 Languages

CSE 105 THEORY OF COMPUTATION

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Automata Theory CS S-FR Final Review

Formal Languages and Compilers Lecture VI: Lexical Analysis

CSE 431S Scanning. Washington University Spring 2013

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

Formal Grammars and Abstract Machines. Sahar Al Seesi

CS402 - Theory of Automata FAQs By

Lexical Analysis. Implementation: Finite Automata

CHAPTER 8. Copyright Cengage Learning. All rights reserved.

CIT3130: Theory of Computation. Regular languages

2. Lexical Analysis! Prof. O. Nierstrasz!

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

Integers and Mathematical Induction

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

Alphabets, strings and formal. An introduction to information representation

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

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

Recursively Defined Functions

Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form

T.E. (Computer Engineering) (Semester I) Examination, 2013 THEORY OF COMPUTATION (2008 Course)

Regular Expressions. Chapter 6

UNIT -2 LEXICAL ANALYSIS

where is the transition function, Pushdown Automata is the set of accept states. is the start state, and is the set of states, is the input alphabet,

FAdo: Interactive Tools for Learning Formal Computational Models

14.1 Encoding for different models of computation

Implementation of Lexical Analysis

lec3:nondeterministic finite state automata

Midterm I (Solutions) CS164, Spring 2002

Languages and Finite Automata

Front End: Lexical Analysis. The Structure of a Compiler

Universal Turing Machine Chomsky Hierarchy Decidability Reducibility Uncomputable Functions Rice s Theorem Decidability Continued

A Note on the Succinctness of Descriptions of Deterministic Languages

Transcription:

Mobile Computing and Software Engineering p. 1/3 Formal Languages and Automata Chapter 3 Regular languages and Regular Grammars Chuan-Ming Liu cmliu@csie.ntut.edu.tw Department of Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN

Mobile Computing and Software Engineering p. 2/3 Contents Regular Expressions Connection between Regular Expressions and Regular Languages Regular Grammars

Mobile Computing and Software Engineering p. 3/3 Regular Expressions Let Σ be a given alphabet. Then, 1, λ, and a Σ are all regular expressions. These are called primitive regular expression. 2 if r 1 and r 2 are regular expression, so are r 1 + r 2,r 1 r 2,r 1, and (r 1). 3 A string is a regular expression if and only if it can be derived from the primitive regular expressions by a finite number of applications of the rules in 2. Note that we use + to denote union, for concatenation, and for star-closure.

Mobile Computing and Software Engineering p. 4/3 Example 1 For Σ = {a, b, c}, the string is a regular expression. (a + b c) (c + )

Regular Expression Languages Let r be a regular expression. Then, the language L(r) is defined by the following rules: 1. is a regular expression denoting the empty set, 2. λ is a regular expression denoting {λ}, 3. For every a Σ, a is a regular expression denoting {a}. If r 1 and r 2 are regular expressions, then 4. L(r 1 + r 2 ) = L(r 1 ) L(r 2 ), 5. L(r 1 L 2 ) = L(r 1 )L(r 2 ), 6. L((r 1 )) = L(r 1 ), 7. L(r 1) = (L(r 1 )). Mobile Computing and Software Engineering p. 5/3

Mobile Computing and Software Engineering p. 6/3 Example 2 Exhibit the language L(a (a + b)) in set notation. L(a (a + b)) = L(a )L(a + b) = (L(a)) (L(a) L(b)) = {λ,a,aa,aaa,...}{a,b} = {a,aa,aaa,...,b,ab,aab,...}.

Mobile Computing and Software Engineering p. 7/3 Example 3 Problem: Find the language generated by the following regular expression where Σ = {a,b}. r = (a + b) (a + ab) Solution: It denotes the language L(r) = {a,bb,aa,abb,ba, bbb,...}.

Mobile Computing and Software Engineering p. 8/3 Example 4 Problem: Describe the language which has the following regular expression r = (aa) (bb) b. Solution: It denotes the set of all strings with an even number of a s followed by an odd number of b s; that is, L(r) = {a 2n b 2m+1 : n 0,m 0}.

Mobile Computing and Software Engineering p. 9/3 Example 5 Problem: For Σ = {0, 1}, give a regular expression r such that L(r) = {w Σ : w has at least one pair of Solution: consecutive zeros}. r = (0 + 1) 00(0 + 1).

Mobile Computing and Software Engineering p. 10/3 Example 6 Problem: Find a regular expression for the language L = {w {0, 1} : w has no pair of consecutive zeros}. Solution: r = (1 011 ) (0 + λ) + 1 (0 + λ). An alternative shorter expression r = (1 + 01) (0 + λ). Note: Two regular expressions are equivalent if they denote the same language.

Mobile Computing and Software Engineering p. 11/3 Contents Regular Expressions Connection between Regular Expressions and Regular Languages Regular Grammars

Mobile Computing and Software Engineering p. 12/3 Regular Expressions and Languages The two concepts of regular expression and regular language are essentially the same: for every regular language there is a regular expression. for every regular expression there is a regular language. Note: a regular language a dfa accepts it an nfa accepts it

Mobile Computing and Software Engineering p. 13/3 Theorem Let r be a regular expression. Then there exists some nondeterministic finite automaton that accepts L(r). Consequently, L(r) is a regular language. Proof: To show L(r) is a regular language, we need to find an nfa which accepts L(r). We will use the definition of the languages associated with regular expressions. Note: The basic idea is to construct an nfa which mimics the process of producing L(r) for a given regular expression.

Mobile Computing and Software Engineering p. 14/3 Constructing an Nfa (1) (1) Consider the rules for the termination conditions of the recursion on the regular expression. For each of the simple regular expressions, λ, and a Σ, we can easily construct an nfa which accepts it. : λ: a:

Mobile Computing and Software Engineering p. 15/3 Constructing an Nfa (2) (2) We now consider the recursion part of the definition for L(r). Let r 1 and r 2 be two arbitrary regular expressions and suppose automata M(r 1 ) and M(r 2 ) accept L(r 1 ) and L(r 2 ), respectively. Then, using the state diagrams, we can have the following conceptual state diagrams:

Mobile Computing and Software Engineering p. 16/3 Constructing an Nfa (3) (3) Construct the automata for the regular expressions r 1 + r 2, r 1 r 2, and r 1: For L(r 1 + r 2 ): M(r 1 ) M(r 2 ) Note: The original final states are dismissed.

Mobile Computing and Software Engineering p. 17/3 Constructing an Nfa (4) For L(r 1 r 2 ):

Mobile Computing and Software Engineering p. 18/3 Constructing an Nfa (5) For L(r 1): Using these fundamental components, we can build automata for arbitrary complex regular expressions.

Example 7 Problem: Find an nfa that accepts L(r), where Solution: r = (a + bb) (ba + λ) L(a + bb) L(ba + λ) = L((a + bb) (ba + λ)) Mobile Computing and Software Engineering p. 19/3

Mobile Computing and Software Engineering p. 20/3 Generalized Transition Graphs We now consider that for every regular language, there exists a corresponding regular expression. Definition: A generalized transition graph (GTG) is a transition graph whose edges are labeled with regular expressions; otherwise, it is the same as the usual transition graph. Note: The strings of the labels are subsets of the language accepted by the GTG.

Mobile Computing and Software Engineering p. 21/3 Example GTG L(a + a (a + b)c )

Mobile Computing and Software Engineering p. 22/3 Complete GTG s Observation: The state diagram of an nfa is a GTG. = for every regular language, there exists a GTG that accepts it. Note: The language accepted by an nfa is regular. Hence, the observation says, in some sense, that the language accepted by a GTG is regular. Definition: A complete GTG is a graph in which all edges are present.

Mobile Computing and Software Engineering p. 23/3 Example Complete GTG (a) A GTG (b) The complete GTG of (a)

Mobile Computing and Software Engineering p. 24/3 Two State Complete GTG s The regular expression r = r 1 r 2 (r 4 + r 3 r 1r 2 ) covers all possible paths and so is the correct regular expression associated with the graph.

Mobile Computing and Software Engineering p. 25/3 Example 8 Note: When a GTG has more than two states, we can find an equivalent GTG by renaming one state at a time. =

Mobile Computing and Software Engineering p. 26/3 Example 9 Problem:Find a regular expression for the language L = {w {a,b} : n a (w) is even and n b (w) is odd}, where n a (w) and n b (w) are the number of occurrences of a and b in w respectively.

Mobile Computing and Software Engineering p. 27/3 Theorem Let L be a regular language. Then there exists a regular expression r such that L = L(r). Proof: L is regular an nfa accepting it a GTG to present it can find a regular expression for it.

Mobile Computing and Software Engineering p. 28/3 Contents Regular Expressions Connection between Regular Expressions and Regular Languages Regular Grammars

Mobile Computing and Software Engineering p. 29/3 Regular Grammars We have learned that a regular language can be represented by an nfa (or a dfa) a regular expression We now discuss a third way to describe regular languages grammars

Mobile Computing and Software Engineering p. 30/3 Definitions A grammar G = (V,T,s,P) is said to be right-linear (left-linear) if all productions are of the form A xb (A Bx) or A x, where A,B V and x T. A regular grammar (or type-3 grammar) is one that is either right-linear or left-linear. Note: At most one variable appears on the right side of any production.

Mobile Computing and Software Engineering p. 31/3 Example 10 and 11 1. The grammar G 1 = ({S}, {a,b},s,p 1 ) with P 1 given as S abs a is right-linear. 2. The grammar G 2 = ({S,S 1,S 2 }, {a,b},s,p 2 ) with productions is left-linear. S S 1 ab, S 1 S 1 ab S 2, S 2 a,

Mobile Computing and Software Engineering p. 32/3 Example 12 The grammar G = ({S,A,B}, {a,b},s,p) with productions is not regular. S A, A ab λ, B Ab, Note: The grammar is an instance of a linear grammar.

Mobile Computing and Software Engineering p. 33/3 Linear Grammar Definition: A linear grammar is a grammar in which at most one variable can occur on the right side of any production, without restriction on the position of this variable. Note: A regular grammar is always linear, but not all linear grammars are regular.

Mobile Computing and Software Engineering p. 34/3 Theorem Let G = (V, T, S, P) be a right-linear grammar. Then L(G) is a regular language. proof: To prove this, we consider to construct an nfa M that mimics the derivations of a right-linear grammar: Let M = (Q, Σ,δ,q 0,F) where Σ = T, q 0 = S, 1. Q= the set of variables in all productions, plus an additional state A; { {S,A} if S λ exists; 2. F =, {A} O.W. 3. δ(b,a) contains all C s if B ac and A δ(b,a) if B C.

Mobile Computing and Software Engineering p. 35/3 Example 13 Problem: Construct a finite automaton that accepts the language generated by the following grammar Solution: V 0 = a 1 V 1 V 1 = abv 0 b

Mobile Computing and Software Engineering p. 36/3 Theorem If L is a regular language on the alphabet Σ, then there exists a right-linear grammar G = (V, Σ, S, P). proof: W.L.O.G., let M = (Q, Σ,δ,q 0,F) be a dfa that accepts L. We construct a right-linear grammar G = (Q, Σ,q 0,P) as follows. 1. B ac P if δ(b,a) = C, 2. B C P if δ(b,a) = C and C F. We then need to show that (1) T(M) L(G) and (2) L(G) T(M).

Mobile Computing and Software Engineering p. 37/3 Example 14 Problem: Construct a right-linear grammar for L(aab a). Solution: G = ({q 0,q 1,q 2,q 3 }, {a,b},q 0,P ) where P includes: 1. q 0 aq 1, 2. q 1 aq 2, 3. q 2 bq 2,

Mobile Computing and Software Engineering p. 38/3 Theorems L is regular there exists a left-linear grammar G such that L = L(G). L is regular there exists a regular grammar G such that L = L(G).