Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.

Similar presentations


Presentation on theme: "1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language."— Presentation transcript:

1 1 Regular Expressions

2 2 Regular expressions describe regular languages Example: describes the language

3 3 Recursive Definition Are regular expressions Primitive regular expressions: Given regular expressions and

4 4 Examples A regular expression: Not a regular expression:

5 5 Languages of Regular Expressions : language of regular expression Example

6 6 Definition For primitive regular expressions:

7 7 Definition (continued) For regular expressions and

8 8 Example Regular expression:

9 9 Example Regular expression

10 10 Example Regular expression

11 11 Example Regular expression = { all strings with at least two consecutive 0 }

12 12 Example Regular expression = { all strings without two consecutive 0 }

13 13 Equivalent Regular Expressions Definition: Regular expressions and are equivalent if

14 14 Example = { all strings without two consecutive 0 } and are equivalent regular expr.

15 15 Example Let L be the language that consists of all Strings over {a, b} that begin with aa or end with bb. aa(a+b)* + (a+b)*bb

16 16 Example L is the set of strings over {a, b} that contains exactly two b’s. a*ba*ba*

17 17 Example L is the set of strings over {a, b} that do not end in aaa. (a+b)*(b+ba+baa)+a+aa+

18 18 Example L is the set of strings over {a, b, c} that do not contain the substring bc. c*(b+ac*)*

19 19 Regular Expressions and Regular Languages

20 20 Theorem Languages Generated by Regular Expressions Regular Languages

21 21 Languages Generated by Regular Expressions Regular Languages Generated by Regular Expressions Regular Languages We will show:

22 22 Proof - Part 1 For any regular expression the language is regular Languages Generated by Regular Expressions Regular Languages Proof by induction on the size of

23 23 Induction Basis Primitive Regular Expressions: NFAs regular languages

24 24 Inductive Hypothesis Assume for regular expressions and that and are regular languages

25 25 Inductive Step We will prove: Are regular Languages

26 26 By definition of regular expressions:

27 27 By inductive hypothesis we know: and are regular languages Regular languages are closed under: Union Concatenation Star We also know:

28 28 Therefore: Are regular languages

29 29 And trivially: is a regular language

30 30 Proof - Part 2 Languages Generated by Regular Expressions Regular Languages For any regular language there is a regular expression with Proof by construction of regular expression

31 31 Since is regular take the NFA that accepts it Single final state

32 32 From construct the equivalent Generalized Transition Graph in which transition labels are regular expressions Example:

33 33 Example Delete node 2

34 34 Delete node 3

35 35 Delete node 4

36 36 Another Example:

37 37 Reducing the states:

38 38 Resulting Regular Expression:

39 39 In General Removing states:

40 40 The final transition graph: The resulting regular expression:

41 41 Applications of RE Two common applications of RE: Lexical analysis in compiler Finding patterns in text

42 42 Lexical Analyzer Recognize “ tokens ” in a program source code. The tokens can be variable names, reserved words, operators, numbers, … etc. Each kind of token can be specified as an RE, e.g., a variable name is of the form [A- Za-z][A-Za-z0-9]*. We can then construct an NFA to recognize it automatically.

43 43 By putting all these NFA ’ s together, we obtain one that can recognize different kinds of tokens in the input string. We can convert this NFA to DFA, and implement this DFA as a deterministic program - the lexical analyzer.

44 44 Text Search “ grep ” in Unix stands for “ Global (search for) Regular Expression and Print ”. Unix has its own notations for regular expressions: –Dot “. ” stands for “ any character ”. –[a 1 a 2 … a k ] stands for {a 1, a 2 … a k }, e.g., [bcd12] stands for the set {b, c, d, 1, 2}. –[x-y] stands for all characters from x to y in the ASCII sequence.

45 45 –| means “ or ”, i.e., + in our normal notation. –* means “ Kleene star ”, as in our normal notation. –? means “ zero or one ” –+ means “ one or more ”, e.g., R+ is RR* –{n} means “ n copies of ”, e.g., R{5} is RRRRR (You can find out more by “ man grep ”, “ man regex ” )

46 46 We can use these notations to search for string patterns in text. For example, credit card numbers: –[0-9]{16} | [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}

47 47 Grammar Languages can also be represented by grammar.

48 48 Grammars Grammars express languages Example: the English language

49 49

50 50 A derivation of “the dog walks”:

51 51 A derivation of “a cat runs”:

52 52 Language of the grammar: L = { “a cat runs”, “a cat walks”, “the cat runs”, “the cat walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }

53 53 Notation VariableTerminal Production Rules

54 54 Another Example Grammar: Derivation of sentence :

55 55 Grammar: Derivation of sentence :

56 56 Other derivations:

57 57 Language of the grammar

58 58 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules

59 59 Example Grammar :

60 60 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence

61 61 We write: Instead of:

62 62 In general we write: If:

63 63 By default:

64 64 Example Grammar Derivations

65 65 Grammar Example Derivations

66 66 Another Grammar Example Grammar : Derivations:

67 67 More Derivations

68 68 Language of a Grammar For a grammar with start variable : String of terminals

69 69 Example For grammar : Since:

70 70 A Convenient Notation

71 71 Regular Grammars A grammar G = (V, T, S, P) is right-linear if all productions are of the form A → xB, A → x where A, B  V, x  T* A grammar G = (V, T, S, P) is left-linear if all productions are of the form A → Bx, A → x where A, B  V, x  T* A regular grammar is one that either right-linear or left- linear

72 72 Assignment#1 Read Textbook 3.3 Then finish Exercise 1, 2, 5, 6 on Page 96-97

73 73 Standard Representations of Regular Languages Regular Languages FAs NFAs Regular Expressions Regular Grammar

74 74 Equivalence Regular Grammars DFA Regular Expressions NFA Theorem 3.1 Theorem 3.2 Theorem 2.3 & Theorem 2.4 Theorem 3.4 Theorem 3.3 Any DFA is trivially an NFA Theorem 2.2

75 75 Elementary Questions about Regular Languages

76 76 When we say: We are given a Regular Language We mean:Language is in a standard representation

77 77 Membership Question Question:Given regular language and string how can we check if ? Answer:Take the DFA that accepts and check if is accepted

78 78 DFA

79 79 Given regular language how can we check if is empty: ? Take the DFA that accepts Check if there is any path from the initial state to a final state Question: Answer:

80 80 DFA

81 81 Given regular language how can we check if is finite? Take the DFA that accepts Check if there is a walk with cycle from the initial state to a final state Question: Answer:

82 82 DFA is infinite DFA is finite

83 83 Given regular languages and how can we check if ? Question: Find if Answer:

84 84 and

85 85 or


Download ppt "1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language."

Similar presentations


Ads by Google