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

Similar documents
6 NFA and Regular Expressions

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

Dr. D.M. Akbar Hussain

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

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

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

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

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

Formal Definition of Computation. Formal Definition of Computation p.1/28

Lexical Analysis - 2

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Theory Bridge Exam Example Questions Version of June 6, 2008

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

CSE 105 THEORY OF COMPUTATION

Languages and Compilers

Converting a DFA to a Regular Expression JP

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

Formal Languages and Compilers Lecture VI: Lexical Analysis

Structure of Programming Languages Lecture 3

Automating Construction of Lexers

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

Theory of Computations Spring 2016 Practice Final Exam Solutions

Learn Smart and Grow with world

CSE 105 THEORY OF COMPUTATION

CS 432 Fall Mike Lam, Professor. Finite Automata Conversions and Lexing

Midterm I (Solutions) CS164, Spring 2002

Lexical Analysis. Implementation: Finite Automata


JNTUWORLD. Code No: R

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

(Refer Slide Time: 0:19)

TOPIC PAGE NO. UNIT-I FINITE AUTOMATA

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Regular Languages and Regular Expressions

ECS 120 Lesson 7 Regular Expressions, Pt. 1

Formal Languages and Automata

Lexical Analysis. Lecture 3-4

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

Multiple Choice Questions

Name: Finite Automata

Front End: Lexical Analysis. The Structure of a Compiler

CMSC330 Summer 2009 Midterm 1: Ruby and Re~lu[ar Lan~ua~les

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

Implementation of Lexical Analysis. Lecture 4

Implementation of Lexical Analysis

Implementation of Lexical Analysis

LECTURE NOTES THEORY OF COMPUTATION

CSE450. Translation of Programming Languages. Automata, Simple Language Design Principles

Solutions to Homework 10

Implementation of Lexical Analysis

DVA337 HT17 - LECTURE 4. Languages and regular expressions

Chapter Seven: Regular Expressions

LECTURE NOTES THEORY OF COMPUTATION

Computer Sciences Department

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

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

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

CSE 431S Scanning. Washington University Spring 2013

Lexical Analysis. Prof. James L. Frankel Harvard University

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

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

lec3:nondeterministic finite state automata

Lecture 2 Finite Automata

Introduction to Lexical Analysis

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

CT32 COMPUTER NETWORKS DEC 2015

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

CS308 Compiler Principles Lexical Analyzer Li Jiang

UNIT I PART A PART B

Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Announcements! P1 part 1 due next Tuesday P1 part 2 due next Friday

Compiler Construction LECTURE # 3

Lexical Analysis. Chapter 2

Decision Properties for Context-free Languages

Theory of Computation

Regular Expression Constrained Sequence Alignment

Zhizheng Zhang. Southeast University

Lexical Analysis. Lecture 2-4

Finite Automata Part Three

Optimizing Finite Automata

Theory of Computations Spring 2016 Practice Final

CIT3130: Theory of Computation. Regular languages

Lexical Analysis. Lecture 3. January 10, 2018

A Characterization of the Chomsky Hierarchy by String Turing Machines

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

Midterm I review. Reading: Chapters 1-4

Question Bank. 10CS63:Compiler Design

1. [5 points each] True or False. If the question is currently open, write O or Open.

14.1 Encoding for different models of computation

COMP Logic for Computer Scientists. Lecture 25

CS 310: State Transition Diagrams

Lexical Analyzer Scanner

CS 181 B&C EXAM #1 NAME. You have 90 minutes to complete this exam. You may assume without proof any statement proved in class.

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


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

Lecture 5: Regular Expression and Finite Automata

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

Compiler Design. 2. Regular Expressions & Finite State Automata (FSA) Kanat Bolazar January 21, 2010

Transcription:

Last lecture CMSC330 Finite Automata Languages Sets of strings Operations on languages Regular expressions Constants Operators Precedence 1 2 Finite automata States Transitions Examples Types This lecture Deterministic (DFA) Non-deterministic (NFA) Implementing Regular Expressions We can implement a regular expression by turning it into a finite automaton A machine for recognizing a regular language 3 4 Finite Automata Finite Automata: States Start state State with incoming transition from no other state Can have only 1 start state Machine starts in start or initial state Repeat until the end of the string is reached Scan the next symbol s of the string Take transition edge labeled with s String is accepted if automaton is in final state when end of string reached Final state State with double circle Can have 0 or more final states 5 6 1

Finite Automaton: Example 1 Finite Automaton: Example 2 0 0 1 0 1 1 accepted 0 0 1 0 1 0 not accepted 7 8 What Language is This? All strings over that end in What is a regular expression for this language? 9 Finite Automaton: Example 3 string state at end accept s? aabcc S2 Y acc S2 Y bbc S2 Y aabbb Y aa S0 Y S0 Y acba S3 N (a,b,c notation shorthand for three self loops) 10 Last Lecture Regular Expression Theory Introduction to finite state machines This lecture Finite state machines continued 11 12 2

Finite Automaton: Example 3 string state at end accept s? aabcc S2 Y acc S2 Y bbc S2 Y aabbb Y aa S0 Y S0 Y acba S3 N (a,b,c notation shorthand for three self loops) 13 Finite Automaton: Example 3 (cont.) What language does this DFA accept? S3 is a dead state a nonfinal state with no transition to another state 14 Dead State: Shorthand Notation If a transition is omitted, assume it goes to a dead state that is not shown What Lang. Does This FA Accept? is short for Language? Strings over with alternating even and odd digits, beginning with odd digit 15 again, so DFAs are not unique 16 Practice Give the English descriptions and the DFA or regular expression of the following languages: All strings with length a multiple of 5 All alternating binary strings All binary strings containing the substring 11 17 Finite Automaton: Example 4 Language? Description for each state S0 = Haven t seen anything yet OR seen zero or more b s OR Last symbol seen was a b = Last symbol seen was an a S2 = Last two symbols seen were ab S3 = Last three symbols seen were abb 18 3

Practice Give the regular expressions and finite automata for the following languages You and your neighbors names All binary strings containing an even length substring of all 1 s All binary strings containing exactly two 1 s All binary strings that start and end with the same number Types of Finite Automata Deterministic Finite Automata (DFA) Exactly one sequence of steps for each string All examples so far Nondeterministic Finite Automata (NFA) May have many sequences of steps for each string More compact 19 20 Math Alert! Formal Definition A deterministic finite automaton (DFA) is a 5-tuple (, Q, q 0, F, ) where is an alphabet the strings recognized by the DFA are over this set Q is a nonempty set of states q 0 Q is the start state F Q is the set of final states How many can there be? : Q x x Q specifies the DFA's transitions A DFA accepts s if it stops at a final state on s (after consuming all chars in s) 21 22 Formal Definition: Example 5-tuple (, Q, q 0, F, ) S0 0 S0 S0 1 23 DFA Requirements Can not have more than one transition leaving a state on the same symbol I.e., transition function must be a valid function a a Can not have transitions with empty labels Transitions must be labeled by alphabet symbols ε-transition NFAs do not have these requirements! DFA is a special case of NFA 24 4

Nondeterministic Finite Automata (NFA) An NFA is a 5-tuple where is an alphabet Q is a nonempty set of states q 0 Q is the start state F Q is the set of final states Q x ( U {}) x Q specifies the NFA's transitions Transitions on are allowed can optionally take these transitions without consuming any input Can have more than one transition for a given state and symbol An NFA accepts s if there is at least one path from its start to final state on s NFA for Has paths to either S0 or Neither is final, so rejected Has paths to different states One leads to S3, so accepted 25 26 Another example DFA NFA for Language? 27 Has paths to states S0, Has paths to S0, Need to use -transition 28 Relating REs to DFAs and NFAs Reducing Regular Expressions to NFAs Regular expressions, NFAs, and DFAs accept the same languages! Goal: Given regular expression, construct NFA: DFA can transform NFA Remember regular expressions are defined recursively from primitive RE languages Invariant: in our NFAs Recall F = set of final states can transform can transform Base case: RE 29 30 5

Base case: Reduction (cont.) Reduction: Concatenation Induction: Base case: <A> <B> 31 32 Reduction: Concatenation (cont.) Induction: AB Induction: (A B) Reduction: Union <A> <B>!!! 33 34 Reduction: Union (cont.) Induction: (A B) Induction: A* Reduction: Closure! 35 36 6

Reduction: Closure (cont.) Reduction Complexity Induction: A* Given a regular expression A of size n... Size = # of symbols + # of operations!! 37 How many states does <A> have? 2 added for each, 2 added for each * O(n) That s pretty good! 38 Practice Summary Draw NFAs for the following regular expressions and languages all binary strings ending in 1 (odd numbers) all alphabetic strings which come after hello in alphabetic order " " 39 Finite automata Deterministic (DFA) Non-deterministic (NFA) Questions How are DFAs and NFAs different? When does an NFA accept a string? How to convert regular expression to an NFA? 40 7