Lecture 18: Theory of Computation Regular Expressions and Dfas

Total Page:16

File Type:pdf, Size:1020Kb

Lecture 18: Theory of Computation Regular Expressions and Dfas Introduction to Theoretical CS Lecture 18: Theory of Computation Two fundamental questions. ! What can a computer do? ! What can a computer do with limited resources? General approach. Pentium IV running Linux kernel 2.4.22 ! Don't talk about specific machines or problems. ! Consider minimal abstract machines. ! Consider general classes of problems. COS126: General Computer Science • http://www.cs.Princeton.EDU/~cos126 2 Why Learn Theory In theory . Regular Expressions and DFAs ! Deeper understanding of what is a computer and computing. ! Foundation of all modern computers. ! Pure science. ! Philosophical implications. a* | (a*ba*ba*ba*)* In practice . ! Web search: theory of pattern matching. ! Sequential circuits: theory of finite state automata. a a a ! Compilers: theory of context free grammars. b b ! Cryptography: theory of computational complexity. 0 1 2 ! Data compression: theory of information. b "In theory there is no difference between theory and practice. In practice there is." -Yogi Berra 3 4 Pattern Matching Applications Regular Expressions: Basic Operations Test if a string matches some pattern. Regular expression. Notation to specify a set of strings. ! Process natural language. ! Scan for virus signatures. ! Search for information using Google. Operation Regular Expression Yes No ! Access information in digital libraries. ! Retrieve information from Lexis/Nexis. Concatenation aabaab aabaab every other string ! Search-and-replace in a word processors. cumulus succubus Wildcard .u.u.u. ! Filter text (spam, NetNanny, Carnivore, malware). jugulum tumultuous ! Validate data-entry fields (dates, email, URL, credit card). aa Union aa | baab baab every other string ! Search for markers in human genome using PROSITE patterns. aa ab Closure ab*a abbba ababa Parse text files. aaaab ! Compile a Java program. a(a|b)aab every other string abaab ! Crawl and index the Web. Parentheses a ! ! Read in data stored in TOY input file format. (ab)*a ababababa abbbaa ! Automatically create Java documentation from Javadoc comments. 5 6 Regular Expressions: Examples Generalized Regular Expressions Regular expression. Notation is surprisingly expressive. Regular expressions are a standard programmer's tool. ! Built in to Java, Perl, Unix, Python, . ! Additional operations typically added for convenience. ! Ex: [a-e]+ is shorthand for (a|b|c|d|e)(a|b|c|d|e)*. Regular Expression Yes No .* spb .* raspberry subspace contains the trigraph spb crispbread subspecies Operation Regular Expression Yes No a* | (a*ba*ba*ba*)* bbb b aaa bb multiple of three b’s abcde ade bbbaababbaa baabbbaa One or more a(bc)+de abcbcde bcde .*0.... 10000 111111111 capitalized camelCase fifth to last digit is 0 98701234 403982772 Character classes [A-Za-z][a-z]* Word 4illegal gcg (cgg|agg)* ctg gcgctg gcgcgg 08540-1321 111111111 Exactly k [0-9]{5}-[0-9]{4} gcgcggctg cggcggcggctg 19072-5541 166-54-111 fragile X syndrome indicator gcgcggaggctg gcgcaggctg Negations [^aeiou]{6} rhythm decade 7 8 Regular Expressions in Java Solving the Pattern Match Problem Validity checking. Is input in the set described by the re? Regular expressions are a concise way to describe patterns. ! How would you implement String.matches ? ! Hardware: build a deterministic finite state automaton (DFA). public class Validate { public static void main(String[] args) { ! Software: simulate a DFA. String re = args[0]; String input = args[1]; DFA: simple machine that solves the pattern match problem. System.out.println(input.matches(re)); } ! Different machine for each pattern. } powerful string library method ! Accepts or rejects string specified on input tape. ! Focus on true or false questions for simplicity. need help solving crosswords? % java Validate "..oo..oo." bloodroot true legal Java identifier % java Validate "[$_A-Za-z][$_A-Za-z0-9]*" ident123 true valid email address (simplified) % java Validate "[a-z]+@([a-z]+\\.)+(edu|com)" [email protected] true need quotes to "escape" the shell 9 10 Deterministic Finite State Automaton (DFA) Theory of DFAs and REs Simple machine with N states. RE. Concise way to describe a set of strings. ! Begin in start state. DFA. Machine to recognize whether a given string is in a given set. ! Read first input symbol. ! Move to new state, depending on current state and input symbol. Duality: for any DFA, there exists a regular expression to describe ! Repeat until last input symbol read. the same set of strings; for any regular expression, there exists a ! Accept or reject string depending on last state. DFA that recognizes the same set. a a a a a a b b Y N N a* | (a*ba*ba*ba*)* b b b DFA Y N N multiple of 3 b's multiple of 3 b's b Practical consequence of duality proof: to match regular expression Input b b a a b b a b b patterns, (i) build DFA and (ii) simulate DFA on input string. 11 12 Implementing a Pattern Matcher Application: Harvester Problem: given a regular expression, create program that tests Harvest information from input stream. whether given input is in set of strings described. ! Harvest patterns from DNA. Step 1: build the DFA. % java Harvester "gcg(cgg|agg)*ctg" chromosomeX.txt ! A compiler! gcgcggcggcggcggcggctg ! See COS 226 or COS 320. gcgctg gcgctg Step 2: simulate it with given input. Easy. gcgcggcggcggaggcggaggcggctg ! Harvest email addresses from web for spam campaign. State state = start; while (!CharStdIn.isEmpty()) { % java Harvester "[a-z]+@([a-z]+\\.)+(edu|com|net|tv)" char c = CharStdIn.readChar(); http://www.princeton.edu/~cos126 state = state.next(c); [email protected] } email validator (simplified) [email protected] System.out.println(state.accept()); [email protected] 13 14 Application: Harvester Application: Parsing a Data File Harvest information from input stream. Ex: parsing an NCBI genome data file. ! Use Pattern data type to compile regular expression to NFA. LOCUS AC146846 128142 bp DNA linear HTG 13-NOV-2003 ! Use Matcher data type to simulate NFA. DEFINITION Ornithorhynchus anatinus clone CLM1-393H9, ACCESSION AC146846 ! (NFA is fancy but equivalent variety of DFA) VERSION AC146846.2 GI:38304214 KEYWORDS HTG; HTGS_PHASE2; HTGS_DRAFT. SOURCE Ornithorhynchus anatinus (platypus) import java.util.regex.Pattern; ORIGIN 1 tgtatttcat ttgaccgtgc tgttttttcc cggtttttca gtacggtgtt agggagccac import java.util.regex.Matcher; 61 gtgattctgt ttgttttatg ctgccgaata gctgctcgat gaatctctgc atagacagct // a comment public class Harvester { 121 gccgcaggga gaaatgacca gtttgtgatg acaaaatgta ggaaagctgt ttcttcataa ... public static void main(String[] args) { 128101 ggaaatgcga cccccacgct aatgtacagc ttctttagat tg String re = args[0]; // In in = new In(args[1]); String input = in.readAll(); String re = "[ ]*[0-9]+([actg ]*).*"; Pattern pattern = Pattern.compile(re); Pattern pattern = Pattern.compile(re); Matcher matcher = pattern.matcher(input); In in = new In(filename); while (matcher.find()) { String line; System.out.println(matcher.group()); while ((line = in.readLine()) != null) { } Matcher matcher = pattern.matcher(line); } if (matcher.find()) { extract the RE part in parentheses } String s = matcher.group(1).replaceAll(" ", ""); // do something with s } replace this RE with this string } 15 16 Limitations of DFA Fundamental Questions No DFA can recognize the language of all bit strings with an equal Which languages CANNOT be described by any RE? number of 0's and 1's. ! Bit strings with equal number of 0s and 1s. ! Decimal strings that represent prime numbers. ! Suppose an N-state DFA can recognize this language. ! Genomic strings that are Watson-Crick complemented palindromes. ! Consider following input: 0000000011111111 ! Many more. N+1 0's N+1 1's How can we extend REs to describe richer sets of strings? ! DFA must accept this string. ! Context free grammar (e.g., Java). ! Some state x is revisited during first N+1 0's since only N states. 0000000011111111 Reference: http://java.sun.com/docs/books/jls/second_edition/html/syntax.doc.html x x Q. How can we make simple machines more powerful? ! Machine would accept same string without intervening 0's. Q. Are there any limits on what kinds of problems machines can solve? 000011111111 ! This string doesn't have an equal number of 0's and 1's. 17 18 Summary Programmer. Turing Machines ! Regular expressions are a powerful pattern matching tool. ! Implement regular expressions with finite state machines. Theoretician. ! Regular expression is a compact description of a set of strings. ! DFA is an abstract machine that solves pattern match problem for Challenge: Design simplest machine that is regular expressions. "as powerful" as conventional computers. ! DFAs and regular expressions have limitations. Variations ! Yes (accept) and No (reject) states sometimes drawn differently ! Terminology: Deterministic Finite State Automaton (DFA), Finite State Machine (FSM), Finite State Automaton (FSA) are the same Alan Turing (1912-1954) ! DFA’s can have output, specified on the arcs or in the states – These may not have explicit Yes and No states 19 20 Turing Machine: Components Turing Machine: Fetch, Execute Alan Turing sought the most primitive model of a computing device. States. ! Finite number of possible machine configurations. Tape. ! Determines what machine does and which way tape head moves. ! Stores input, output, and intermediate results. ! One arbitrarily long strip, divided into cells. State transition diagram. tape head ! Finite alphabet of symbols. ! Ex. if in state 2 and input symbol is 1 then: overwrite the 1 with x, tape move to state 0, move tape head to left. 2 Tape head. R 1:x ! Points to one cell of tape. #:# 0:x ! 0 1 Reads a symbol from active cell. 3 5 #:# #:# ! Writes a symbol to active cell. L R Y N ! Moves left or right one cell at a time. 1:x 0:x 4 #:# R Before … # # x x x 1 1 0 # # … 22 23 Turing Machine: Fetch, Execute Turing Machine: Initialization and Termination States. Initialization. ! Finite number of possible machine configurations. ! Set input on some portion of tape. ! Determines what machine does and which way tape head moves. ! Set tape head. … # # 0 0 1 1 1 0 # # … ! Set initial state. State transition diagram.
Recommended publications
  • Use Perl Regular Expressions in SAS® Shuguang Zhang, WRDS, Philadelphia, PA
    NESUG 2007 Programming Beyond the Basics Use Perl Regular Expressions in SAS® Shuguang Zhang, WRDS, Philadelphia, PA ABSTRACT Regular Expression (Regexp) enhance search and replace operations on text. In SAS®, the INDEX, SCAN and SUBSTR functions along with concatenation (||) can be used for simple search and replace operations on static text. These functions lack flexibility and make searching dynamic text difficult, and involve more function calls. Regexp combines most, if not all, of these steps into one expression. This makes code less error prone, easier to maintain, clearer, and can improve performance. This paper will discuss three ways to use Perl Regular Expression in SAS: 1. Use SAS PRX functions; 2. Use Perl Regular Expression with filename statement through a PIPE such as ‘Filename fileref PIPE 'Perl programm'; 3. Use an X command such as ‘X Perl_program’; Three typical uses of regular expressions will also be discussed and example(s) will be presented for each: 1. Test for a pattern of characters within a string; 2. Replace text; 3. Extract a substring. INTRODUCTION Perl is short for “Practical Extraction and Report Language". Larry Wall Created Perl in mid-1980s when he was trying to produce some reports from a Usenet-Nes-like hierarchy of files. Perl tries to fill the gap between low-level programming and high-level programming and it is easy, nearly unlimited, and fast. A regular expression, often called a pattern in Perl, is a template that either matches or does not match a given string. That is, there are an infinite number of possible text strings.
    [Show full text]
  • Perl Regular Expressions Tip Sheet Functions and Call Routines
    – Perl Regular Expressions Tip Sheet Functions and Call Routines Basic Syntax Advanced Syntax regex-id = prxparse(perl-regex) Character Behavior Character Behavior Compile Perl regular expression perl-regex and /…/ Starting and ending regex delimiters non-meta Match character return regex-id to be used by other PRX functions. | Alternation character () Grouping {}[]()^ Metacharacters, to match these pos = prxmatch(regex-id | perl-regex, source) $.|*+?\ characters, override (escape) with \ Search in source and return position of match or zero Wildcards/Character Class Shorthands \ Override (escape) next metacharacter if no match is found. Character Behavior \n Match capture buffer n Match any one character . (?:…) Non-capturing group new-string = prxchange(regex-id | perl-regex, times, \w Match a word character (alphanumeric old-string) plus "_") Lazy Repetition Factors Search and replace times number of times in old- \W Match a non-word character (match minimum number of times possible) string and return modified string in new-string. \s Match a whitespace character Character Behavior \S Match a non-whitespace character *? Match 0 or more times call prxchange(regex-id, times, old-string, new- \d Match a digit character +? Match 1 or more times string, res-length, trunc-value, num-of-changes) Match a non-digit character ?? Match 0 or 1 time Same as prior example and place length of result in \D {n}? Match exactly n times res-length, if result is too long to fit into new-string, Character Classes Match at least n times trunc-value is set to 1, and the number of changes is {n,}? Character Behavior Match at least n but not more than m placed in num-of-changes.
    [Show full text]
  • Unicode Regular Expressions Technical Reports
    7/1/2019 UTS #18: Unicode Regular Expressions Technical Reports Working Draft for Proposed Update Unicode® Technical Standard #18 UNICODE REGULAR EXPRESSIONS Version 20 Editors Mark Davis, Andy Heninger Date 2019-07-01 This Version http://www.unicode.org/reports/tr18/tr18-20.html Previous Version http://www.unicode.org/reports/tr18/tr18-19.html Latest Version http://www.unicode.org/reports/tr18/ Latest Proposed http://www.unicode.org/reports/tr18/proposed.html Update Revision 20 Summary This document describes guidelines for how to adapt regular expression engines to use Unicode. Status This is a draft document which may be updated, replaced, or superseded by other documents at any time. Publication does not imply endorsement by the Unicode Consortium. This is not a stable document; it is inappropriate to cite this document as other than a work in progress. A Unicode Technical Standard (UTS) is an independent specification. Conformance to the Unicode Standard does not imply conformance to any UTS. Please submit corrigenda and other comments with the online reporting form [Feedback]. Related information that is useful in understanding this document is found in the References. For the latest version of the Unicode Standard, see [Unicode]. For a list of current Unicode Technical Reports, see [Reports]. For more information about versions of the Unicode Standard, see [Versions]. Contents 0 Introduction 0.1 Notation 0.2 Conformance 1 Basic Unicode Support: Level 1 1.1 Hex Notation 1.1.1 Hex Notation and Normalization 1.2 Properties 1.2.1 General
    [Show full text]
  • Regular Expressions with a Brief Intro to FSM
    Regular Expressions with a brief intro to FSM 15-123 Systems Skills in C and Unix Case for regular expressions • Many web applications require pattern matching – look for <a href> tag for links – Token search • A regular expression – A pattern that defines a class of strings – Special syntax used to represent the class • Eg; *.c - any pattern that ends with .c Formal Languages • Formal language consists of – An alphabet – Formal grammar • Formal grammar defines – Strings that belong to language • Formal languages with formal semantics generates rules for semantic specifications of programming languages Automaton • An automaton ( or automata in plural) is a machine that can recognize valid strings generated by a formal language . • A finite automata is a mathematical model of a finite state machine (FSM), an abstract model under which all modern computers are built. Automaton • A FSM is a machine that consists of a set of finite states and a transition table. • The FSM can be in any one of the states and can transit from one state to another based on a series of rules given by a transition function. Example What does this machine represents? Describe the kind of strings it will accept. Exercise • Draw a FSM that accepts any string with even number of A’s. Assume the alphabet is {A,B} Build a FSM • Stream: “I love cats and more cats and big cats ” • Pattern: “cat” Regular Expressions Regex versus FSM • A regular expressions and FSM’s are equivalent concepts. • Regular expression is a pattern that can be recognized by a FSM. • Regex is an example of how good theory leads to good programs Regular Expression • regex defines a class of patterns – Patterns that ends with a “*” • Regex utilities in unix – grep , awk , sed • Applications – Pattern matching (DNA) – Web searches Regex Engine • A software that can process a string to find regex matches.
    [Show full text]
  • Regular Expressions
    CS 172: Computability and Complexity Regular Expressions Sanjit A. Seshia EECS, UC Berkeley Acknowledgments: L.von Ahn, L. Blum, M. Blum The Picture So Far DFA NFA Regular language S. A. Seshia 2 Today’s Lecture DFA NFA Regular Regular language expression S. A. Seshia 3 Regular Expressions • What is a regular expression? S. A. Seshia 4 Regular Expressions • Q. What is a regular expression? • A. It’s a “textual”/ “algebraic” representation of a regular language – A DFA can be viewed as a “pictorial” / “explicit” representation • We will prove that a regular expressions (regexps) indeed represent regular languages S. A. Seshia 5 Regular Expressions: Definition σ is a regular expression representing { σσσ} ( σσσ ∈∈∈ ΣΣΣ ) ε is a regular expression representing { ε} ∅ is a regular expression representing ∅∅∅ If R 1 and R 2 are regular expressions representing L 1 and L 2 then: (R 1R2) represents L 1⋅⋅⋅L2 (R 1 ∪∪∪ R2) represents L 1 ∪∪∪ L2 (R 1)* represents L 1* S. A. Seshia 6 Operator Precedence 1. *** 2. ( often left out; ⋅⋅⋅ a ··· b ab ) 3. ∪∪∪ S. A. Seshia 7 Example of Precedence R1*R 2 ∪∪∪ R3 = ( ())R1* R2 ∪∪∪ R3 S. A. Seshia 8 What’s the regexp? { w | w has exactly a single 1 } 0*10* S. A. Seshia 9 What language does ∅∅∅* represent? {ε} S. A. Seshia 10 What’s the regexp? { w | w has length ≥ 3 and its 3rd symbol is 0 } ΣΣΣ2 0 ΣΣΣ* Σ = (0 ∪∪∪ 1) S. A. Seshia 11 Some Identities Let R, S, T be regular expressions • R ∪∪∪∅∅∅ = ? • R ···∅∅∅ = ? • Prove: R ( S ∪∪∪ T ) = R S ∪∪∪ R T (what’s the proof idea?) S.
    [Show full text]
  • Context-Free Grammar for the Syntax of Regular Expression Over the ASCII
    Context-free Grammar for the syntax of regular expression over the ASCII character set assumption : • A regular expression is to be interpreted a Haskell string, then is used to match against a Haskell string. Therefore, each regexp is enclosed inside a pair of double quotes, just like any Haskell string. For clarity, a regexp is highlighted and a “Haskell input string” is quoted for the examples in this document. • Since ASCII character strings will be encoded as in Haskell, therefore special control ASCII characters such as NUL and DEL are handled by Haskell. context-free grammar : BNF notation is used to describe the syntax of regular expressions defined in this document, with the following basic rules: • <nonterminal> ::= choice1 | choice2 | ... • Double quotes are used when necessary to reflect the literal meaning of the content itself. <regexp> ::= <union> | <concat> <union> ::= <regexp> "|" <concat> <concat> ::= <term><concat> | <term> <term> ::= <star> | <element> <star> ::= <element>* <element> ::= <group> | <char> | <emptySet> | <emptyStr> <group> ::= (<regexp>) <char> ::= <alphanum> | <symbol> | <white> <alphanum> ::= A | B | C | ... | Z | a | b | c | ... | z | 0 | 1 | 2 | ... | 9 <symbol> ::= ! | " | # | $ | % | & | ' | + | , | - | . | / | : | ; | < | = | > | ? | @ | [ | ] | ^ | _ | ` | { | } | ~ | <sp> | \<metachar> <sp> ::= " " <metachar> ::= \ | "|" | ( | ) | * | <white> <white> ::= <tab> | <vtab> | <nline> <tab> ::= \t <vtab> ::= \v <nline> ::= \n <emptySet> ::= Ø <emptyStr> ::= "" Explanations : 1. Definition of <metachar> in our definition of regexp: Symbol meaning \ Used to escape a metacharacter, \* means the star char itself | Specifies alternatives, y|n|m means y OR n OR m (...) Used for grouping, giving the group priority * Used to indicate zero or more of a regexp, a* matches the empty string, “a”, “aa”, “aaa” and so on Whi tespace char meaning \n A new line character \t A horizontal tab character \v A vertical tab character 2.
    [Show full text]
  • PHP Regular Expressions
    PHP Regular Expressions What is Regular Expression Regular Expressions, commonly known as "regex" or "RegExp", are a specially formatted text strings used to find patterns in text. Regular expressions are one of the most powerful tools available today for effective and efficient text processing and manipulations. For example, it can be used to verify whether the format of data i.e. name, email, phone number, etc. entered by the user was correct or not, find or replace matching string within text content, and so on. PHP (version 5.3 and above) supports Perl style regular expressions via its preg_ family of functions. Why Perl style regular expressions? Because Perl (Practical Extraction and Report Language) was the first mainstream programming language that provided integrated support for regular expressions and it is well known for its strong support of regular expressions and its extraordinary text processing and manipulation capabilities. Let's begin with a brief overview of the commonly used PHP's built-in pattern- matching functions before delving deep into the world of regular expressions. Function What it Does preg_match() Perform a regular expression match. preg_match_all() Perform a global regular expression match. preg_replace() Perform a regular expression search and replace. preg_grep() Returns the elements of the input array that matched the pattern. preg_split() Splits up a string into substrings using a regular expression. preg_quote() Quote regular expression characters found within a string. Note: The PHP preg_match() function stops searching after it finds the first match, whereas the preg_match_all() function continues searching until the end of the string and find all possible matches instead of stopping at the first match.
    [Show full text]
  • A Quick Guide to PERL Regular Expressions Second Edition © 2006 Transliteration: Translate Operator Tr/// EXPR =~ Tr/SEARCHLIST/REPLACELIST/Cds
    \W Any non-word character. Warning \w != \S Search&Replace: substitution operator s/// A Quick Guide To EXPR =~ s/MOTIF/REPLACE/egimosx POSIX Character class PERL Regular Expressions Example: correct typo for the word rabbit [[:class:]] class can be any of: $ex =~ s/rabit/rabbit/g; alnum alpha ascii blank cntrl digit graph lower This is a Quick reference Guide for PERL regular expressions Here is the content of $ex: print punct space upper xdigit (also known as regexps or regexes). The ID sp:UBP5_RAT is similar to the rabbit AC tr:Q12345 These tools are used to describe text as “motifs” or “patterns” for matching, quoting, substituting or translitterating. Each Example: find and tag any TrEMBL AC Special characters programming language (Perl, C, Java, Python...) define its $ex =~ s/tr:/trembl_ac=/g; \a alert (bell) Here is the content of $ex: own regular expressions although the syntax might differ from \b backspace details to extensive changes. In this guide we will concentrate The ID sp:UBP5_RAT is similar to the rabit AC trembl_ \e escape ac=Q12345 on the Perl regexp syntax, we assume that the reader has some \f form feed preliminary knowledge of Perl programming. Options \n newline \r carriage return e evaluate REPLACE as an expression Perl uses a Traditional Nondeterministic Finite Automata \t horizontal tabulation (NFA) match engine. This means that it will compare each g global matches (matches all occurrences) element of the motif to the input string, keeping track of i case insensitive \nnn octal nnn the positions. The engine choose the first leftmost match m multiline, allow “^” and “$” to match with (\n) \xnn hexadecimal nn after greedy (i.e., longest possible match) quantifiers have o compile MOTIF only once \cX control character X matched.
    [Show full text]
  • CSCI 3434: Theory of Computation Lecture 4: Regular Expressions
    CSCI 3434: Theory of Computation Lecture 4: Regular Expressions Ashutosh Trivedi 0; 1 0; 1 1 0;" 1 start s1 s2 s3 s4 Department of Computer Science University of Colorado Boulder Ashutosh Trivedi { 1 of 21 Ashutosh Trivedi Lecture 3: Regular Expressions Definition (Regular Languages) We call a language regular if it can be accepted by a deterministic finite state automaton. What are Regular Languages? { An alphabet Σ = fa; b; cg is a finite set of letters, { The set of all strings (aka, words) Σ∗ over an alphabet Σ can be recursively defined as: as : { Base case: " 2 Σ∗ (empty string), { Induction: If w 2 Σ∗ then wa 2 Σ∗ for all a 2 Σ. {A language L over some alphabet Σ is a set of strings, i.e. L ⊆ Σ∗. { Some examples: ∗ { Leven = fw 2 Σ : w is of even lengthg ∗ n m { La∗b∗ = fw 2 Σ : w is of the form a b for n; m ≥ 0g ∗ n n { Lanbn = fw 2 Σ : w is of the form a b for n ≥ 0g ∗ 0 { Lprime = fw 2 Σ : w has a prime number of a sg { Deterministic finite state automata define languages that require finite resources (states) to recognize. Ashutosh Trivedi { 2 of 21 Ashutosh Trivedi Lecture 3: Regular Expressions What are Regular Languages? { An alphabet Σ = fa; b; cg is a finite set of letters, { The set of all strings (aka, words) Σ∗ over an alphabet Σ can be recursively defined as: as : { Base case: " 2 Σ∗ (empty string), { Induction: If w 2 Σ∗ then wa 2 Σ∗ for all a 2 Σ.
    [Show full text]
  • 10 Patterns, Automata, and Regular Expressions
    CHAPTER 10 Patterns, Automata, ✦ ✦ ✦ ✦ and Regular Expressions A pattern is a set of objects with some recognizable property. One type of pattern is a set of character strings, such as the set of legal C identifiers, each of which is a string of letters, digits, and underscores, beginning with a letter or underscore. Another example would be the set of arrays of 0’s and 1’s of a given size that a character reader might interpret as representing the same symbol. Figure 10.1 shows three 7 × 7-arrays that might be interpreted as letter A’s. The set of all such arrays would constitute the pattern called “A.” 0001000 0000000 0001000 0011100 0010000 0010100 0010100 0011000 0110100 0110110 0101000 0111110 0111110 0111000 1100011 1100011 1001100 1000001 1000001 1000100 0000000 Fig. 10.1. Three instances of the pattern “A.” The two fundamental problems associated with patterns are their definition and their recognition, subjects of this and the next chapter. Recognizing patterns is an integral part of tasks such as optical character recognition, an example of which was suggested by Fig. 10.1. In some applications, pattern recognition is a component of a larger problem. For example, recognizing patterns in programs is an essential part of compiling — that is, the translation of programs from one language, such as C, into another, such as machine language. There are many other examples of pattern use in computer science. Patterns play a key role in the design of the electronic circuits used to build computers and other digital devices. They are used in text editors to allow us to search for instances of specific words or sets of character strings, such as “the letters if followed by any sequence of characters followed by then.” Most operating systems allow us to use 529 530 PATTERNS, AUTOMATA, AND REGULAR EXPRESSIONS patterns in commands; for example, the UNIX command “ls *tex” lists all files whose names end with the three-character sequence “tex”.
    [Show full text]
  • Regular Expressions for Perl, C, PHP, Python, Java, and .NET
    Regular Expressions for Perl, C, PHP, Python, Java, and .NET Regular Expression Pocket Reference Tony Stubblebine Regular Expression Pocket Reference Regular Expression Pocket Reference Tony Stubblebine Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo PHP This reference covers PHP 4.3’s Perl-style regular expression support contained within the preg routines. PHP also pro- vides POSIX-style regular expressions, but these do not offer additional benefit in power or speed. The preg routines use a Traditional NFA match engine. For an explanation of the rules behind an NFA engine, see “Introduction to Regexes and Pattern Matching.” Supported Metacharacters PHP supports the metacharacters and metasequences listed in Tables 31 through 35. For expanded definitions of each metacharacter, see “Regex Metacharacters, Modes, and Constructs.” Table 31. Character representations Sequence Meaning \a Alert (bell), x07. \b Backspace, x08, supported only in character class. \e ESC character, x1B. \n Newline, x0A. \r Carriage return, x0D. \f Form feed, x0C. \t Horizontal tab, x09 \octal Character specified by a three-digit octal code. \xhex Character specified by a one- or two-digit hexadecimal code. \x{hex} Character specified by any hexadecimal code. \cchar Named control character. Table 32. Character classes and class-like constructs Class Meaning [...] A single character listed or contained within a listed range. PHP | 63 Table 32. Character classes and class-like constructs Class Meaning [^...] A single character not listed and not contained within a listed range. [:class:] POSIX-style character class valid only within a regex character class. Any character except newline (unless single-line mode,/s). \C One byte; however, this may corrupt a Unicode character stream.
    [Show full text]
  • Regular Languages and Finite Automata for Part IA of the Computer Science Tripos
    N Lecture Notes on Regular Languages and Finite Automata for Part IA of the Computer Science Tripos Prof. Andrew M. Pitts Cambridge University Computer Laboratory c 2010 A. M. Pitts First Edition 1998. Revised 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010. Contents Learning Guide ii 1 Regular Expressions 1 1.1 Alphabets, strings, and languages . ...... 1 1.2 Patternmatching ................................ 4 1.3 Some questions about languages . ... 6 1.4 Exercises .................................... 8 2 Finite State Machines 11 2.1 Finiteautomata ................................. 11 2.2 Determinism, non-determinism, and ε-transitions . 14 2.3 Asubsetconstruction . .... .... .... ..... .... .... ... 17 2.4 Summary .................................... 20 2.5 Exercises .................................... 20 3 Regular Languages, I 23 3.1 Finite automata from regular expressions . ........ 23 3.2 Decidabilityofmatching . 28 3.3 Exercises .................................... 30 4 Regular Languages, II 31 4.1 Regular expressions from finite automata . ..... 31 4.2 Anexample ................................... 32 4.3 Complement and intersection of regular languages . ......... 34 4.4 Exercises .................................... 36 5 The Pumping Lemma 39 5.1 ProvingthePumpingLemma. 40 5.2 UsingthePumpingLemma.. .... .... ..... .... .... .... 41 5.3 Decidability of language equivalence . ....... 44 5.4 Exercises .................................... 45 6 Grammars 47 6.1 Context-freegrammars . 47 6.2 Backus-NaurForm ..............................
    [Show full text]