680 likes | 931 Views
Theory of Computation. Shakir Al Faraji Computer Science Dept., Petra University Amman - Jordan. email: shussain@uop.edu.jo. Thank you, Shakir. IMPORTANT NOTES. Students…
E N D
Theory of Computation Shakir Al Faraji Computer Science Dept., Petra University Amman - Jordan. email: shussain@uop.edu.jo Thank you,Shakir Dr. Shakir Al Faraji
IMPORTANT NOTES • Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply going through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Thank you,Shakir. Dr. Shakir Al Faraji
Course Strategy • Be Warned: This is not a course that spoon-feeds students. • Students are expected to be investigative and resourceful. • Reading books and other research of topics are expected. Thank you,Shakir. Dr. Shakir Al Faraji
Material • There is a book: Hopcroft, Rajeev, & Ullman 3ed Edition (2007), Addison Wesley These were the lecture notes. Well, apart from the slides. Thank you,Shakir. Dr. Shakir Al Faraji
Regular Expression Dr. Shakir Al Faraji
Definition A regular expression, or RE, describes strings of characters (words or phrases or any arbitrary text). It's a pattern that matches certain strings and doesn't match others.A regular expressionis a set ofcharacters that specify a pattern. ORLanguage defining symbols. Dr. Shakir Al Faraji
Definition-Cont Regular expressions are used to generate patterns of strings. A regular expression is an algebraic formula whose value is a pattern consisting of a set of strings, called the language of the expression. Dr. Shakir Al Faraji
Operands in a regular expression Operands in a regular expression can be: • characters from the alphabet over which the regular expression is defined. • variables whose values are any pattern defined by a regular expression. • epsilon which denotes the empty string containing no characters. • null which denotes the empty set of strings. Dr. Shakir Al Faraji
Operators used in regular expressions • Union: If R1 and R2 are regular expressions, then R1 | R2 (also written as R1 U R2 or R1 + R2) is also a regular expression. L(R1|R2) = L(R1) U L(R2). • Concatenation: If R1 and R2 are regular expressions, then R1R2 (also written as R1.R2) is also a regular expression. L(R1R2) = L(R1) concatenated with L(R2). Dr. Shakir Al Faraji
Operators used in regular expressions • Kleene closure: If R1 is a regular expression, then R1* (the Kleene closure of R1) is also a regular expression. L(R1*) = epsilon U L(R1) U L(R1R1) U L(R1R1R1) U ... Closure has the highest precedence, followed by concatenation, followed by union. Dr. Shakir Al Faraji
Examples The set of strings over {0,1} that end in 3 consecutive 1's. (0 | 1)* 111 OR(0 + 1)* 111 The set of strings over {0,1} that have at least one 1. 0* 1 (0 + 1) * Dr. Shakir Al Faraji
Examples-Cont. The set of strings over {0,1} that have at most one 1. 0* | 0* 1 0* The set of strings over {A..Z,a..z} that contain the word "main". Let <letter> = A | B | ... | Z | a | b | ... | z <letter>* main <letter>* Dr. Shakir Al Faraji
Examples-Cont. The set of strings over {A..Z,a..z} that contain 3 x's. <letter>* x <letter>* x <letter>* x <letter>* Dr. Shakir Al Faraji
Examples-Cont. The set of identifiers in Pascal. Let <letter> = A | B | ... | Z | a | b | ... | z Let <digit> = 0 | 1 | 2 | 3 ... | 9 <letter> (<letter> | <digit>)* Dr. Shakir Al Faraji
Examples-Cont. The set of real numbers in Pascal. Let <digit> = 0 | 1 | 2 | 3 ... | 9 Let <exp> = 'E' <sign> <digit> <digit>* | epsilon Let <sign> = '+' | '-' | epsilon Let <decimal> = '.' <digit> <digit>* | epsilon <digit> <digit>* <decimal> <exp> Dr. Shakir Al Faraji
Examples-Cont. Consider = { a } L is a language that each word is of odd length a (aa)* Dr. Shakir Al Faraji
Examples-Cont. Consider = { a, b } L is a language that each word must start with the letter b b (a+b)* Dr. Shakir Al Faraji
Examples-Cont. Consider = { a, b } L is a language that each word must start with the letter b and end with the letter a b (a+b)* a Dr. Shakir Al Faraji
Examples-Cont. Consider = { a, b, c } L = { a, c, ab, cb, abb, cbb, abbb, cbbb, abbbb, cbbbb . . . } L - language ((a+c) b*) Dr. Shakir Al Faraji
Examples-Cont. Consider (a+b)*a(a+b)* L = language of all words over the = { a, b }that have an a in them L = { a, aa, ba, aab, aba, baa, bba, aaaa, aaba, abaa . . . } Dr. Shakir Al Faraji
Examples-Cont. Consider the following RE (a+b)* a(a+b)* a(a+b)* L = language of all words over the = { a, b }that have at least two a’s in them Dr. Shakir Al Faraji
Examples-Cont. (a+b)* a(a+b)* a(a+b)* = b*ab*a(a+b)* ? (a+b)* a(a+b)* a(a+b)* = (a+b)*ab*ab* ? (a+b)* a(a+b)* a(a+b)* = b*a(a+b)*ab* Dr. Shakir Al Faraji
Examples-Cont. (a+b)* a(a+b)* b(a+b)* ? Dr. Shakir Al Faraji
Examples-Cont. (a+b)* a(a+b)* b(a+b)* ? Language of all words that have at least one a and at least one b!!!! Dr. Shakir Al Faraji
Examples-Cont. (a+b)* a(a+b)* b(a+b)* What about the word ba!!!! Dr. Shakir Al Faraji
Examples-Cont. (a+b)* a(a+b)* b(a+b)* What about the word ba!!!! MUST BE (a+b)* a(a+b)* b(a+b)* + (a+b)* b(a+b)* a(a+b)* Dr. Shakir Al Faraji
Examples-Cont. IS (a+b)* a(a+b)* b(a+b)* + (a+b)* b(a+b)* a(a+b)* SAME AS (a+b)* a(a+b)* b(a+b)* + bb*aa* Dr. Shakir Al Faraji
Examples-Cont. b* + ab* ( + a )b* b* + ab* = ( + a )b* Dr. Shakir Al Faraji
More on RE. Definition If S and T are sets of strings of letters, we define the product set of strings of letters to be ST= {all combinations of a string from S concatenated with a string fromT} Dr. Shakir Al Faraji
More on RE - Cont. If S = { a, aa, aaa } , T = { bb, bbb } then ST= {abb,abbb,aabb,aabbb,aaabb,aaabbb } Dr. Shakir Al Faraji
Languages Associated with RE. Definition • language associated with the RE just is a single letter is that one-letter word alone and the language associated with is just {}, a one-word language. • if r1 is a RE associated with the language L1 and r2 is a RE associated with the language L2 then: • the RE (r1)(r2) is associated with the product L1L2 • RE (r1+r2) is associated with the language formed by the union of sets L1 and L2 • RE (r1)* is associated with the language L1* Dr. Shakir Al Faraji
Finite Languages are Regular. Theorem If L is a finite language, then L can be defined by a regular expression. In other words, all finite languages are regular. Proof Let L = { aa, ab, ba, bb } RE is aa+ab+ba+bb (a+b)(a+b) Dr. Shakir Al Faraji
Examples. Can you describe the following RE (a+b)* (aa+bb) (a+b)* Dr. Shakir Al Faraji
Examples. Can you describe the following RE (a+b)* (aa+bb) (a+b)* All strings of a’s and b’s that at some point contain a double letter. Dr. Shakir Al Faraji
Examples. = { a, b } What strings do not contain a double letter? Dr. Shakir Al Faraji
Examples. = { a, b } What strings do not contain a double letter? (ab)* Dr. Shakir Al Faraji
Examples. = { a, b } What strings do not contain a double letter? (ab)* Is it correct ? Dr. Shakir Al Faraji
Examples. = { a, b } What strings do not contain a double letter? (ab)* Is it correct ? NO Dr. Shakir Al Faraji
Examples. = { a, b } What strings do not contain a double letter? (ab)* Is it correct ? NO ( +b)(ab)*( +a) Dr. Shakir Al Faraji
Examples. ( a + b* )* = (a + b )* ? ( aa + ab*)* = (aa + ab)*? ( a* b* )* = (a + b )* ? Dr. Shakir Al Faraji
Examples. ( a + b* )* = (a + b )* YES ( aa + ab*)* = (aa + ab)*NO ( a* b* )* = (a + b )* YES Dr. Shakir Al Faraji
Examples. [aa + bb + ( ab + ba) (aa+bb)*(ab + ba) ]* Dr. Shakir Al Faraji
Examples. [aa + bb + ( ab + ba) (aa+bb)*(ab + ba) ]* EVEN-EVEN type1 = aa type2 = bb type3 = (ab+ba)(aa+bb)*(ab+ba) E = [ type1 + type2 + type3 ] * Dr. Shakir Al Faraji
What Regular Expressions Are Exactly - Terminology Basically, a regular expression is a pattern describing a certain amount of text. Their name comes from the mathematical theory on which they are based. But we will not dig into that. Since most people including myself are lazy to type, you will usually find the name abbreviated to regex or regexp. Dr. Shakir Al Faraji
What Regular Expressions Are Exactly – Cont. This first example is actually a perfectly valid regex. It is the most basic pattern, simply matching the literal text regex. A "match" is the piece of text, or sequence of bytes or characters that pattern was found to correspond to by the regex processing software. Matches are highlighted in blue on this site. Dr. Shakir Al Faraji
What Regular Expressions Are Exactly – Cont. b[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b Dr. Shakir Al Faraji
What Regular Expressions Are Exactly – Cont. b[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b is a more complex pattern. It describes a series of letters, digits, dots, percentage signs and underscores, followed by an at sign, followed by another series of letters, digits, dots, percentage signs and underscores, finally followed by a single dot and between two and four letters. In other words: this pattern describes an email address. Dr. Shakir Al Faraji
What Regular Expressions Are Exactly – Cont. With the above regular expression pattern, you can search through a text file to find email addresses, or verify if a given string looks like an email address. In this tutorial, I will use the term "string" to indicate the text that I am applying the regular expression to. Dr. Shakir Al Faraji
What Regular Expressions Are Exactly – Cont. The term "string" or "character string" is used by programmers to indicate a sequence of characters. In practice, you can use regular expressions with whatever data you can access using the application or programming language you are working with. Dr. Shakir Al Faraji
What Regular Expressions Are Exactly – Cont. A regular expression uses metacharacters (characters that assume special meaning for matching other characters) such as *, [ ], $ and .. For example, the RE [Hh]ello!* would match Hello and hello and Hello! (and hello!!!!!). The RE [Hh](ello|i)!* would match Hello and Hi and Hi! (and so on). A backslash (\) disables the special meaning of the following character, so you could match the string [Hello] with the RE \[Hello\]. Dr. Shakir Al Faraji