Presentation is loading. Please wait.

Presentation is loading. Please wait.

Models of Computing Regular Expressions 1. Formal models of computation What can be computed? What is a valid program? What is a valid name of a variable.

Similar presentations


Presentation on theme: "Models of Computing Regular Expressions 1. Formal models of computation What can be computed? What is a valid program? What is a valid name of a variable."— Presentation transcript:

1 Models of Computing Regular Expressions 1

2 Formal models of computation What can be computed? What is a valid program? What is a valid name of a variable in python? What is the strongest possible programming language? What is the simplest possible computer? Can we say something about “all the possible algorithms”? How much time is required (by the “best” algorithm) for solving a given computational task? How “complicated” is solving that task? 2

3 Three types of theories What can be computed using “simple” computing machines or simple computing processes?  “Automata and Formal Languages” What can be computed by general computers?  “Computability Theory” (" חישוביות ") What can be computed efficiently?  “Complexity Theory” ( סיבוכיות ") 3

4 Machines vs. Functions Study the computing machines “Finite State Machines” – Finite memory “Push Down Automata” – + Stack “Turing Machines” – + Array-like Memory Study the computed functions Regular languages – Simple rules Context-Free languages – + Simple recursion Computable Languages – Anything 4

5 Functions that machines compute Mathematicians: – Function : Natural Numbers  Natural Numbers Computer Scientists: – Function : Strings  Strings Basically the same: – A natural number can be encoded by a string: str(x) – A string can be encoded by a number:  i ord(s[i])*base i Theoretical Computer Scientists: – Function : Strings  Yes/No (Called a “Language”) Basically the same: – Consider each bit of the answer 5

6 Finite Automata Very simply model of a machine with finite memory. Input: a String. Output: accept/reject 6 start 0 0 0 1 1 1 accept 0  reject 1  accept 10  reject 11  reject 100  accept … 10100  reject 10101  reject 10110  accpet …

7 Regular Languages: Rules of the game Finite “Alphabet” Compose a “regular expression” from a finite combination of the following ingredients: – Empty String – Single letter – Concatenation – Or (Notation: A | B) – Repetition (Notation A*) The “Language” of this regular expression is the set of Strings that fit the expression 7

8 Examples with Alphabet = {0, 1} (0 | 1) (0 | 1) (0 | 1) – 010 0(00)* – 00000 (0 | 1)* 0 – 10110 (00 | 11)* – 001111110011 (1 0 0*)* 1 – 10001000101 (0* 1 0* 1 0*)* – 10110001 8

9 More examples Int = 0 | (+ | - |  )( (1 | 2 | … | 9) (0 | 1 |... | 9)*) – -530 Int_List = [  | Int (, Int)* ] – [34,12] The (boy | girl | dog) (ate | lost) the (apple | pear) – The boy ate the pear The boy ran very* fast from the scary* lion – The boy ran very very very fast from the lion 9 Empty string

10 Context Free Languages Recursive definitions: exp: number | ( exp ) | exp op exp 10 ( 5 + 2 ) - ( 1 + 3 ) ( E + E ) - ( E + E ) ( E ) - ( E ) E - E E

11 The algorithmic problem Input: regular expression, text Output: does the text fit the regular expression Variants: – Does it contain the regexp as a substring? – Which parts match which parts? Algorithm 1: Recursion + Memoization Algorithm 2: Use Finite State Machines… 11

12 Regular Expressions in Python 12

13 Some Convenient Syntax “[1fg]” == 1|f|g “[a-e]” == [abcde] “[^abc]” == any letter except a|b|c “.” == any letter “^” == beginning of line “$” == end of line 13

14 Repetitions * : 0 or more times + : 1 or more times ? : 0 or 1 times {m} : Exactly m times {m,n} : between m and n times 14

15 The annoying backslash “\” is used in python as an “escape character”, e.g. “\n” “\” is used in regular expressions for various purposes such as matching the “special characters” The python notation r`abc’ gives “raw” strings, eliminating the python escapes 15

16 Groups 16

17 Parsing Phone Numbers 17


Download ppt "Models of Computing Regular Expressions 1. Formal models of computation What can be computed? What is a valid program? What is a valid name of a variable."

Similar presentations


Ads by Google