cs 10 problem solving via object oriented programming
play

CS 10: Problem solving via Object Oriented Programming Pattern - PowerPoint PPT Presentation

CS 10: Problem solving via Object Oriented Programming Pattern Matching 2 Agenda 1. Pattern matching to validate input Regular expressions Deterministic/Non-Deterministic Finite Automata (DFA/NFA) 2. Finite State Machines (FSM)


  1. CS 10: Problem solving via Object Oriented Programming Pattern Matching

  2. 2

  3. Agenda 1. Pattern matching to validate input Regular expressions • Deterministic/Non-Deterministic • Finite Automata (DFA/NFA) 2. Finite State Machines (FSM) to model complex systems 3

  4. Pattern matching goal: ensure input passes a validation check Pattern matching process: • Given some input (e.g., a series of characters) • Also given a pattern that describes what constitutes valid input • Then check to see if a particular input “passes” validation check (or in other words, input matches the pattern) 4

  5. Sometimes it is useful to be able to detect or require patterns Email addresses follow a pattern: mailbox@domain.TLD example: tjp@cs.dartmouth.edu We can specify a pattern or rules for email addresses: <characters> @ <characters>.<com | edu | org | …> One or One or more Ends with one of a set more characters predefined of values characters Followed Followed by @ by . 5

  6. Regular expressions (regex) are a common way of looking for patterns in Strings Regular expressions (regex) • Most programming languages have support for regex • Can be really complex and messy, but there are basic patterns Operation Meaning Example Character Match a character “a” matches “a” 6

  7. Regular expressions (regex) are a common way of looking for patterns in Strings Regular expressions (regex) • Most programming languages have support for regex • Can be really complex and messy, but there are basic patterns Operation Meaning Example Character Match a character “a” matches “a” Concatenation: One after the other “cat” matches “c” then “a” then “t” R 1 R 2 7

  8. Regular expressions (regex) are a common way of looking for patterns in Strings Regular expressions (regex) • Most programming languages have support for regex • Can be really complex and messy, but there are basic patterns Operation Meaning Example Character Match a character “a” matches “a” Concatenation: One after the other “cat” matches “c” then “a” then “t” R 1 R 2 Alternative: R 1 | One or the other a|e|i|o|u matches any vowel R 2 8

  9. Regular expressions (regex) are a common way of looking for patterns in Strings Regular expressions (regex) • Most programming languages have support for regex • Can be really complex and messy, but there are basic patterns Operation Meaning Example Character Match a character “a” matches “a” Concatenation: One after the other “cat” matches “c” then “a” then “t” R 1 R 2 Alternative: R 1 | One or the other a|e|i|o|u matches any vowel R 2 Grouping: (R) Establishes order; allows c(a|o)t matches “cat” or “cot” reference/extraction 9

  10. Regular expressions (regex) are a common way of looking for patterns in Strings Regular expressions (regex) • Most programming languages have support for regex • Can be really complex and messy, but there are basic patterns Operation Meaning Example Character Match a character “a” matches “a” Concatenation: One after the other “cat” matches “c” then “a” then “t” R 1 R 2 Alternative: R 1 | One or the other a|e|i|o|u matches any vowel R 2 Grouping: (R) Establishes order; allows c(a|o)t matches “cat” or “cot” reference/extraction Character classes Alternative characters and [a-c] matches “a” or “b” or “c”, while [c 1 -c 2 ] and [^c 1 -c 2 ] excluded characters [^a-c] matches any but abc 10

  11. Regular expressions (regex) are a common way of looking for patterns in Strings Regular expressions (regex) • Most programming languages have support for regex • Can be really complex and messy, but there are basic patterns Operation Meaning Example Character Match a character “a” matches “a” Concatenation: One after the other “cat” matches “c” then “a” then “t” R 1 R 2 Alternative: R 1 | One or the other a|e|i|o|u matches any vowel R 2 Grouping: (R) Establishes order; allows c(a|o)t matches “cat” or “cot” reference/extraction Character classes Alternative characters and [a-c] matches “a” or “b” or “c”, while [c 1 -c 2 ] and [^c 1 -c 2 ] excluded characters [^a-c] matches any but abc Repetition: R* Matches 0 or more times “ca*t” matches “ct”, “cat”, “caat” 11

  12. Regular expressions (regex) are a common way of looking for patterns in Strings Regular expressions (regex) • Most programming languages have support for regex • Can be really complex and messy, but there are basic patterns Operation Meaning Example Character Match a character “a” matches “a” Concatenation: One after the other “cat” matches “c” then “a” then “t” R 1 R 2 Alternative: R 1 | One or the other a|e|i|o|u matches any vowel R 2 Grouping: (R) Establishes order; allows c(a|o)t matches “cat” or “cot” reference/extraction Character classes Alternative characters and [a-c] matches “a” or “b” or “c”, while [c 1 -c 2 ] and [^c 1 -c 2 ] excluded characters [^a-c] matches any but abc Repetition: R* Matches 0 or more times “ca*t” matches “ct”, “cat”, “caat” Non-zero Matches 1 or more times “ca+t” matches “cat” or “caat” or 12 repetition: R+ “caaat”, but not “ct”

  13. We can use regex to see if an email address is valid Email addresses follow a pattern: mailbox@domain.TLD example: tjp@cs.dartmouth.edu We can specify a pattern or rules for email addresses: <characters> @ <characters>.<com | edu | org | …> As a simple RegEx: [a-z.]+@[a-z.]* [a-z]+. (com | edu | org …) Check: This simple regex has some issues dealing with real email tjp@cs.dartmouth.edu -- valid addresses Blob.x -- invalid

  14. Turns out a robust email address validator is quite complicated (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0- 9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e- \x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e- \x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a- z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0- 9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0- 9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01- \x09\x0b\x0c\x0e-\x7f])+)\]) • Hard to understand what this does • We can use a graph to make things easier to understand 14 Source: IETF RFC2822

  15. A Graph can implement a regex Email addresses follow a pattern: mailbox@domain.TLD example: tjp@cs.dartmouth.edu com Start . a-z. @ a-z edu org We can specify a pattern or rules for email addresses: <characters> @ <characters>.<com | edu | org | …> . . A Graph can represent the pattern for email addresses Sample addresses can be easily verified if in correct form . 15

  16. Key points 1. We can define a set of rules that must be followed 2. We may be able to represent those rules with a Graph 16

  17. Agenda 1. Pattern matching to validate input Regular expressions • Deterministic/Non-Deterministic • Finite Automata (DFA/NFA) 2. Finite State Machines (FSM) to model complex systems 17

  18. We can model States as Vertices and Transitions as Edges in a directed Graph Finite Automata validating input Set of input symbols called alphabet Begin at Start Transition from A to B 0,1 Double circle indicates valid Start if input 0, else to C end States, non-double circle 0 States are invalid end States A B Operation: Begin at Start State • 1 Read character of input Stay in C • 0,1 Follow graph according regardless if • States as to input given 0 or 1 Graph C Continue until no more • Vertices input characters If at valid end State, • Edges can loop back input valid, else invalid to same vertex Edges as transitions What does this do? (“self loop”) between States based Accepts any input • on input 18 starting with 0

  19. Finite Automata (FA) are formally defined as 5-tuple of States, Transitions, and inputs Finite Automata as 5-tuple (Q, ∑, δ, q 0 , F) FA = (Q, ∑, δ, q 0 , F) • Q – finite set of States (vertices in graph) • ∑ – complete set of possible input symbols (called the alphabet ) • δ – transition function where δ: Q × ∑ → Q (given current State Q and input symbol ∑, transition to next State Q according to δ) • q 0 – initial State; q 0 ∈ Q (means q 0 is an element of Q) • F is a set of valid end States; F ⊆ Q (means F is a subset of Q) We say FA “accepts” (validates) input A=a 1 a 2 a 3 …a n if sequence of States R=r 0 r 1 r 2 …r n exists in Q such that: • r 0 =q 0 //initial State is Start • r i+1 = δ(r i , a i+1 ), for i =0,1, ..., n−1 //input leads to next State • r n ∈ F //last State is an element of the valid end States 19

  20. We can build FAs to validate or reject input Accept any string that starts with 00 Handle any remaining 0,1 Handle first 0 Handle second 0 Start 0 0 A B C 1 Define Start 1 0,1 State D Handle Handle any remaining invalid invalid input input on first two Vertices with no escape characters sometimes called a “trap” 20 Adapted from: https://people.cs.clemson.edu/~goddard/texts/theoryOfComputation/1.pdf

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend