Regular Expressions & Automata

Similar documents
CMSC 132: Object-Oriented Programming II

Chapter Seven: Regular Expressions

Dr. D.M. Akbar Hussain

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

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

Regular Languages and Regular Expressions

CS 314 Principles of Programming Languages

Languages and Strings. Chapter 2

Structure of Programming Languages Lecture 3

Regular Expressions. Chapter 6

Applications of. Regular Expressions BY NIKHIL KUMAR KATTE

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

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

CS 314 Principles of Programming Languages. Lecture 3

Theory of Programming Languages COMP360

Understanding Regular Expressions, Special Characters, and Patterns

ECS 120 Lesson 7 Regular Expressions, Pt. 1

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

Regular Expressions. Steve Renals (based on original notes by Ewan Klein) ICL 12 October Outline Overview of REs REs in Python

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

CSE 105 THEORY OF COMPUTATION

CHAPTER TWO LANGUAGES. Dr Zalmiyah Zakaria

Compilers CS S-01 Compiler Basics & Lexical Analysis

Compilers CS S-01 Compiler Basics & Lexical Analysis

CSE 431S Scanning. Washington University Spring 2013

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

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

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Chapter 4: Regular Expressions

Finite Automata Part Three

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Rice s Theorem and Enumeration

Formal languages and computation models

Non-deterministic Finite Automata (NFA)

Lexical Analysis (ASU Ch 3, Fig 3.1)

Lec-5-HW-1, TM basics

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

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

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

CSE 105 THEORY OF COMPUTATION

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

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

Languages and Finite Automata

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

CS S-01 Compiler Basics & Lexical Analysis 1

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

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

Perl Regular Expressions. Perl Patterns. Character Class Shortcuts. Examples of Perl Patterns

Multiple Choice Questions

Week 2: Syntax Specification, Grammars

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

Formal Languages and Automata

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

Dr. Sarah Abraham University of Texas at Austin Computer Science Department. Regular Expressions. Elements of Graphics CS324e Spring 2017

CSE 105 THEORY OF COMPUTATION

Object-Oriented Software Engineering CS288

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

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

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Theory of Computations Spring 2016 Practice Final Exam Solutions

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

Optimizing Finite Automata

8 ε. Figure 1: An NFA-ǫ

Ambiguous Grammars and Compactification

Formal Languages and Compilers Lecture VI: Lexical Analysis

Lexical Analysis 1 / 52

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine

UNION-FREE DECOMPOSITION OF REGULAR LANGUAGES

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

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

CS402 - Theory of Automata FAQs By

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

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

DVA337 HT17 - LECTURE 4. Languages and regular expressions

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

Compiler Construction

Formal Grammars and Abstract Machines. Sahar Al Seesi

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1


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

JNTUWORLD. Code No: R

CSE528 Natural Language Processing Venue:ADB-405 Topic: Regular Expressions & Automata. www. l ea rn ersd esk.weeb l y. com

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Finite Automata Part Three

Languages and Models of Computation

Lexical Analysis. Chapter 2

Chapter 3: Lexing and Parsing

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions


Lecture 4: Syntax Specification

Regular Expressions. Upsorn Praphamontripong. CS 1111 Introduction to Programming Spring [Ref:

1. Forma9er class. String Stuff. Forma9er class cont. 2. Scanner class 10/19/10. Forma&er Dates G52APR

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

CSE302: Compiler Design

Notes for Comp 454 Week 2

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

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

CMSC 330: Organization of Programming Languages

Transcription:

Regular Expressions & Automata CMSC 132 Department of Computer Science University of Maryland, College Park

Regular expressions Notation Patterns Java support Automata Languages Finite State Machines Turing Machines Computability Overview

Regular Expression (RE) Notation for describing simple string patterns Very useful for text processing Finding / extracting pattern in text Manipulating strings Automatically generating web pages

Regular Expression Regular expression is composed of Symbols Operators Concatenation AB Union A B Closure A*

Alphabet Definitions Set of symbols Σ Examples {a, b}, {A, B, C}, {a-z,a-z,0-9} Strings Sequences of 0 or more symbols from alphabet Examples ε, a, bb, cat, caterpillar Languages Sets of strings empty string Examples, {ε}, { a }, { bb, cat }

More Formally Regular expression describes a language over an alphabet L(E) is language for regular expression E Set of strings generated from regular expression String in language if it matches pattern specified by regular expression

Regular Expression Construction Every symbol is a regular expression Example a REs can be constructed from other REs using Concatenation Union Closure *

Regular Expression Construction Concatenation A followed by B L(AB) = { ab a L(A) AND b L(B) } Example a { a } ab { ab }

Regular Expression Construction Union A or B L(A B) = { a a L(A) OR a L(B) } Example a b { a, b }

Regular Expression Construction Closure Zero or more A L(A*) = { a a = ε OR a L(A)L(A*) } Example a* {ε, a, aa, aaa, aaaa } (ab)*c { c, abc, ababc, abababc }

Regular Expressions in Java Java supports regular expressions In java.util.regex.* Applies to String class in Java 1.4 Introduces additional specification methods Simplifies specification Does not increase power of regular expressions Can simulate with concatenation, union, closure

Regular Expressions in Java Concatenation ab ab (ab)c abc Union ( bar or square brackets [ ] for chars) a b a, b [abc] a, b, c Closure (star *) (ab)* [ab]* ε, ab, abab, ababab ε, a, b, aa, ab, ba, bb

Regular Expressions in Java One or more (plus +) a+ One or more a s Range (dash ) [a z] Any lowercase letters [0 9] Any digit Complement (caret ^ at beginning of RE) [^a] Any symbol except a [^a z] Any symbol except lowercase letters

Regular Expressions in Java Precedence Higher precedence operators take effect first Precedence order Parentheses ( ) Closure a* b+ Concatenation ab Union a b Range [ ]

Regular Expressions in Java Examples ab+ (ab)+ ab cd a(b c)d [abc]d ab, abb, abbb, abbbb ab, abab, ababab, ab, cd abd, acd ad, bd, cd When in doubt, use parentheses

Regular Expressions in Java Predefined character classes [.] Any character except end of line [\d] Digit: [0-9] [\D] Non-digit: [^0-9] [\s] Whitespace character: [ \t\n\x0b\f\r] [\S] Non-whitespace character: [^\s] [\w] Word character: [a-za-z_0-9] [\W] Non-word character: [^\w]

Regular Expressions in Java Literals using backslash \ Need two backslash Java compiler will interpret 1 st backslash for String Examples \\] ] \\.. \\\\ \ 4 backslashes interpreted as \\ by Java compiler

Using Regular Expressions in Java Compile pattern import java.util.regex.*; Pattern p = Pattern.compile("[a-z]+"); Create matcher for specific piece of text Matcher m = p.matcher("now is the time"); Search text boolean found = m.find(); Returns true if pattern is found anywhere in text boolean exact = m.matches() returns true if pattern matches entire test

Using Regular Expressions in Java If pattern is found in text m.group() string found m.start() index of the first character matched m.end() index after last character matched m.group() is same as s.substring(m.start(), m.end()) Calling m.find() again Starts search after end of current pattern match If no more matches, return to beginning of string

Code Output Complete Java Example import java.util.regex.*; public class RegexTest { public static void main(string args[]) { Pattern p = Pattern.compile( [a-z]+ ); Matcher m = p.matcher( Now is the time ); while (m.find()) { System.out.print(m.group() + ); } } } ow is the time

Language Recognition Accept string if and only if in language Abstract representation of computation Performing language recognition can be Simple Strings with even number of 1 s Not Simple Hard Strings with any number of a s, followed by the same number of b s Strings representing legal Java programs Impossible! Strings representing nonterminating Java programs

Automata Simple abstract computers Can be used to recognize languages Finite state machine States + transitions Turing machine States + transitions + tape

Finite State Machine States Starting Accepting Finite number allowed 0 1 1 Transitions State to state Labeled by symbol a q 1 q 2 Start State 0 Accept State L(M) = { w w ends in a 1}

Operations Finite State Machine Move along transitions based on symbol Accept string if ends up in accept state Reject string if ends up in non-accepting state 011 Accept 0 1 1 10 Reject q 1 q 2 0

Properties Finite State Machine Powerful enough to recognize regular expressions In fact, finite state machine regular expression Languages recognized by finite state machines 1-to-1 mapping Languages recognized by regular expressions

Turing Machine Defined by Alan Turing in 1936 Finite state machine + tape Tape Infinite storage Read / write one symbol at tape head Move tape head one space left / right 0 1 1 Tape Head q 1 q 2 0

Allowable actions Turing Machine Read symbol from current square Write symbol to current square Move tape head left Move tape head right Go to next state

Tape Head Turing Machine * 1 0 0 1 0 * Current State Current Content Value to Write Direction to Move New state to enter START * * Left MOVING MOVING 1 0 Left MOVING MOVING 0 1 Left MOVING MOVING * * No move HALT

Operations Turing Machine Read symbol on current square Select action based on symbol & current state Accept string if in accept state Reject string if halts in non-accepting state Reject string if computation does not terminate Halting problem It is undecidable in general whether long-running computations will eventually accept

Computability Computability A language is computable if it can be recognized by some algorithm with finite number of steps Church-Turing thesis Turing machine can recognize any language computable on any machine Intuition Turing machine captures essence of computing Both in a formal sense, and in an informal practical sense