Presentation is loading. Please wait.

Presentation is loading. Please wait.

Theory of Languages and Automata By: Mojtaba Khezrian.

Similar presentations


Presentation on theme: "Theory of Languages and Automata By: Mojtaba Khezrian."— Presentation transcript:

1 Theory of Languages and Automata By: Mojtaba Khezrian

2 Theory of Languages and Automata By Dr. Khezrian 2 Review Alphabetic ……. {0,1} String ….. x=‘101010’ Length of String ….. |x| Language Union Intersection Concat

3 Theory of Languages and Automata By Dr. Khezrian 3 Review

4 Theory of Languages and Automata By Dr. Khezrian 4 Grammar G =(V,T,P,S) V= nonFinal symbol T= Alphabetic P= Rules S= Starting symbol

5 Theory of Languages and Automata By Dr. Khezrian 5 Language of Grammar

6 Theory of Languages and Automata By Dr. Khezrian 6 Language of Grammar

7 Theory of Languages and Automata By Dr. Khezrian 7 Language of Grammar

8 Theory of Languages and Automata By Dr. Khezrian 8 Language of Grammar

9 Theory of Languages and Automata By Dr. Khezrian 9 How to Write Grammar? First split the language to small parts then write the grammar of them. Finally join all of them.

10 Theory of Languages and Automata By Dr. Khezrian 10 How to Write Grammar? 1: Split like: A A A …. A A is the symbol of even length

11 Theory of Languages and Automata By Dr. Khezrian 11 How to Write Grammar? 2: Split like: X A A is the symbol of even length X is the symbol of aa or ab or bb or ba

12 Theory of Languages and Automata By Dr. Khezrian 12 How to Write Grammar? 3: Split like: A B A,B is the symbol of even length

13 Theory of Languages and Automata By Dr. Khezrian 13 How to Write Grammar? 4: Split like: A X A is the symbol of even length X is the symbol of aa or ab or bb or ba

14 Theory of Languages and Automata By Dr. Khezrian 14 How to Write Grammar? Split like: X A A is the symbol of even length X is the symbol of a or b

15 Theory of Languages and Automata By Dr. Khezrian 15 How to Write Grammar? 1: Split like: b…a and a…b

16 Theory of Languages and Automata By Dr. Khezrian 16 How to Write Grammar? 2: Split like: b…a… and a…b…

17 Theory of Languages and Automata By Dr. Khezrian 17 How to Write Grammar? 3: Split like: AB

18 Theory of Languages and Automata By Dr. Khezrian 18 How to Write Grammar? 3: Split it to two languages: L1 and L2 L1: n,m are even L2: n,m are odd

19 Theory of Languages and Automata By Dr. Khezrian 19 How to Write Grammar?

20 Theory of Languages and Automata By Dr. Khezrian 20 Type of Grammar Type-3 Grammar (Regular Grammar) Type-2 Grammar (Context free Grammar) Type-1 Grammar (Context Sensitive Grammar) Type-0 Grammar

21 Theory of Languages and Automata By Dr. Khezrian 21 Type-3 Grammar (Regular Grammar) If it would be right-linear: Or left-linear:

22 Theory of Languages and Automata By Dr. Khezrian 22 Type-3 Grammar (Regular Grammar)

23 Theory of Languages and Automata By Dr. Khezrian 23 Type-2 Grammar (Context Free Grammar)

24 Theory of Languages and Automata By Dr. Khezrian 24 Type-1 Grammar (Context Sensetive Grammar)

25 Theory of Languages and Automata By Dr. Khezrian 25 Type-1 Grammar (Context Sensitive Grammar)

26 Theory of Languages and Automata By Dr. Khezrian 26 Type-0 Grammar

27 Theory of Languages and Automata By Dr. Khezrian 27 Type-0 Grammar

28 Theory of Languages and Automata By Dr. Khezrian 28 Type of Language Type-3 Language (Regular Language) Type-2 Language (Context Free Language) Type-1 Language (Context Sensitive Language) Type-0 Language

29 Theory of Languages and Automata By Dr. Khezrian 29 Type of Language Type-3 If there is a type-3 grammar for it Type-2 If there is a type-2 grammar and no type-3 grammar for it Type-1 If there is a type-1 grammar and no type-3 and type-2 grammar for it Type-0 If there is a type-0 grammar and no type-3 and type-2 and type-1 grammar for it

30 Theory of Languages and Automata By Dr. Khezrian 30 Type of Language

31 Theory of Languages and Automata By Dr. Khezrian 31 Type of Automata Automata for type-3 language (Finite Automata) Automata for type-2 language (Push-down Automata) Automata for type-1 language (Linear Bounded Automata) Automata for type-0 language (Turing Machine)

32 Theory of Languages and Automata By Dr. Khezrian 32 Type of Automata

33 Theory of Languages and Automata By Dr. Khezrian 33 Finite Machine

34 Theory of Languages and Automata By Dr. Khezrian 34 Finite Machine Accept: If at the end of string, machine is stated in one of final states. Reject: If at the end of string, machine is stated in one of non-final states or before end of string it’ll be stoped.

35 Theory of Languages and Automata By Dr. Khezrian 35 Finite Machine

36 Theory of Languages and Automata By Dr. Khezrian 36 Finite Machine

37 Theory of Languages and Automata By Dr. Khezrian 37 Example

38 Theory of Languages and Automata By Dr. Khezrian 38 Language of a Finite machine If A is the set of all strings that machine M accepts, we say that A is the language of machine M and write: L(M) = A.  M recognizes A  M accepts A.

39 Theory of Languages and Automata By Dr. Khezrian 39 Finite Machine

40 Theory of Languages and Automata By Dr. Khezrian 40 Finite Machine

41 Theory of Languages and Automata By Dr. Khezrian 41 Finite Machine

42 Theory of Languages and Automata By Dr. Khezrian 42 Example L(M1) = {w | w contains at least one 1 and even number of 0s follow the last 1}

43 Theory of Languages and Automata By Dr. Khezrian 43 Example L(M4) = {w | w accepts all strings that start and end with a or with b}

44 Theory of Languages and Automata By Dr. Khezrian 44 Example L(M5) = {w | keeps a running count of the sum of the numerical input symbols it reads, modulo 3}

45 Theory of Languages and Automata By Dr. Khezrian 45 Designing Finite Automata Put yourself in the place of the machine and then see how you would go about performing the machine’s task. Design a finite automaton to recognize the regular language of all strings that contain the string 001 as a substring.

46 Theory of Languages and Automata By Dr. Khezrian 46 Designing Finite Automata

47 Theory of Languages and Automata By Dr. Khezrian 47 Example

48 Theory of Languages and Automata By Dr. Khezrian 48 Example

49 Theory of Languages and Automata By Dr. Khezrian 49 Type of Finite Automata Deterministic Finite Automata (DFA) Nondeterministic Finite Automata (NDFA)

50 Theory of Languages and Automata By Dr. Khezrian 50 Deterministic Finite Automata (DFA)

51 Theory of Languages and Automata By Dr. Khezrian 51 Nondeterministic Finite Automata (NDFA)

52 Theory of Languages and Automata By Dr. Khezrian 52 Differences between DFA & NFA First, very state of a DFA always has exactly one exiting transition arrow for each symbol in the alphabet. In an NFA a state may have zero, one, or more exiting arrows for each alphabet symbol.

53 Theory of Languages and Automata By Dr. Khezrian 53 Differences between DFA & NFA Second, in a DFA, labels on the transition arrows are symbols from the alphabet. An NFA may have arrows labeled with members of the alphabet or. Zero, one, or many arrows may exit from each state with the label.

54 Theory of Languages and Automata By Dr. Khezrian 54 Differences between DFA & NFA

55 Theory of Languages and Automata By Dr. Khezrian 55 Example Consider the computation of N1 on input 010110.

56 Theory of Languages and Automata By Dr. Khezrian 56 Example (Cont.)

57 Theory of Languages and Automata By Dr. Khezrian 57 Example

58 Theory of Languages and Automata By Dr. Khezrian 58 Example NFA to DFA

59 Theory of Languages and Automata By Dr. Khezrian 59 Example NFA to DFA

60 Theory of Languages and Automata By Dr. Khezrian 60 Example NFA with to DFA

61 Theory of Languages and Automata By Dr. Khezrian 61 Pumping Lemma

62 Theory of Languages and Automata By Dr. Khezrian 62 Pumping Lemma

63 Theory of Languages and Automata By Dr. Khezrian 63 Pumping Lemma

64 Theory of Languages and Automata By Dr. Khezrian 64 Pumping Lemma

65 Theory of Languages and Automata By Dr. Khezrian 65 Pumping Lemma

66 Theory of Languages and Automata By Dr. Khezrian 66 Pumping Lemma

67 Theory of Languages and Automata By Dr. Khezrian 67 Pumping Lemma

68 Theory of Languages and Automata By Dr. Khezrian 68 Pumping Lemma

69 Theory of Languages and Automata By Dr. Khezrian 69 The Regular Operations Let A and B be languages. We define the regular operations union, concatenation, and star as follows:

70 Theory of Languages and Automata By Dr. Khezrian 70 Union Operations Let NFA1 recognize N1 and NFA2 recognize N2. Construct NFA3 to recognize N1 U N2.

71 Theory of Languages and Automata By Dr. Khezrian 71 Concatenation Operations

72 Theory of Languages and Automata By Dr. Khezrian 72 Union Operations We represent another NFA to recognize A*.

73 Theory of Languages and Automata By Dr. Khezrian 73 Regular Expression

74 Theory of Languages and Automata By Dr. Khezrian 74 Regular Expression Language

75 Theory of Languages and Automata By Dr. Khezrian 75 Example

76 Theory of Languages and Automata By Dr. Khezrian 76 Regular Language Cycle NDFA DFA NDFA-λ R RG

77 Theory of Languages and Automata By Dr. Khezrian 77 Regular Language Cycle NDFA DFA NDFA-λ R RG

78 Theory of Languages and Automata By Dr. Khezrian 78 Building an NFA from the Regular Expression

79 Theory of Languages and Automata By Dr. Khezrian 79 Building an NFA from the Regular Expression

80 Theory of Languages and Automata By Dr. Khezrian 80 Regular Language Cycle NDFA DFA NDFA-λ R RG

81 Theory of Languages and Automata By Dr. Khezrian 81 Building Grammar from the NFA G =(V,T,P,S)

82 Theory of Languages and Automata By Dr. Khezrian 82 Building Grammar from the NFA

83 Theory of Languages and Automata By Dr. Khezrian 83 Building Grammar from the NFA

84 Theory of Languages and Automata By Dr. Khezrian 84 Building Grammar from the NFA

85 Theory of Languages and Automata By Dr. Khezrian 85 Building Grammar from the NFA

86 Theory of Languages and Automata By Dr. Khezrian 86 Building Grammar from the NFA

87 Theory of Languages and Automata By Dr. Khezrian 87 Building Grammar from the NFA

88 Theory of Languages and Automata By Dr. Khezrian 88 Regular Language Cycle NDFA DFA NDFA-λ R RG

89 Theory of Languages and Automata By Dr. Khezrian 89 Building NFA from the Grammar G =(V,T,P,S)

90 Theory of Languages and Automata By Dr. Khezrian 90 Building NFA from the Grammar

91 Theory of Languages and Automata By Dr. Khezrian 91 Regular Language Cycle NDFA DFA NDFA-λ R RG

92 Theory of Languages and Automata By Dr. Khezrian 92 Building Grammar from the Regular expression

93 Theory of Languages and Automata By Dr. Khezrian 93 Building Grammar from the Regular expression

94 Theory of Languages and Automata By Dr. Khezrian 94 Building Grammar from the Regular expression

95 Theory of Languages and Automata By Dr. Khezrian 95 Building Grammar from the Regular expression

96 Theory of Languages and Automata By Dr. Khezrian 96 Building Grammar from the Regular expression

97 Theory of Languages and Automata By Dr. Khezrian 97 Regular Language Cycle NDFA DFA NDFA-λ R RG

98 Theory of Languages and Automata By Dr. Khezrian 98 Building Regular expression from the Grammar

99 Theory of Languages and Automata By Dr. Khezrian 99 Regular Language Cycle NDFA DFA NDFA-λ R RG

100 Theory of Languages and Automata By Dr. Khezrian 100 Regular Language Cycle NDFA DFA NDFA-λ R RG

101 Theory of Languages and Automata By Dr. Khezrian 101 Regular Language Cycle NDFA DFA NDFA-λ R RG

102 Theory of Languages and Automata By Dr. Khezrian 102 GNFA Formal Definition A generalized nondeterministic finite automaton is a 5-tuple, a 5-tuple (Q,Σ,δ,qstart, qaccept), where 1.Q is a finite set called states, 2.Σ is a the input alphabet, 3.δ : (−{}) ×(Q−{}) is the transition function, 4.qstart is the start state, and 5.qaccept is the accept state.

103 Theory of Languages and Automata By Dr. Khezrian 103 Assumptions For convenience we require that GNFAs always have a special form that meets the following conditions: 1.The start state has transition arrows going to every other state but no arrows coming in from any other state. 2.There is only a single accept state, and it has arrows coming in from every other state but no arrows going to any other state. Furthermore, the accept state is not the same as the start state. 3.Except for the start and accept states, one arrow goes from every state to every other state and also from each state to itself.

104 Theory of Languages and Automata By Dr. Khezrian 104 How to Eliminate a State?

105 Theory of Languages and Automata By Dr. Khezrian 105 Example

106 Theory of Languages and Automata By Dr. Khezrian 106 Example

107 Theory of Languages and Automata By Dr. Khezrian 107 Example (Cont.)

108 Theory of Languages and Automata By Dr. Khezrian 108 Context-free Languages Context-Free Grammars Pushdown Automata Non-Context-Free Languages

109 Theory of Languages and Automata By Dr. Khezrian 109 Context-free Languages Each rule: a line in the grammar Each line: a variable, an arrow, a string The string: variables and terminals

110 Theory of Languages and Automata By Dr. Khezrian 110 Example G1: A 0A1 A B B # Three rules Variables: A,B Start Variable: A Terminals: 0,1,#

111 Theory of Languages and Automata By Dr. Khezrian 111 Example G2 (describes a fragment of the English language):

112 Theory of Languages and Automata By Dr. Khezrian 112 Derivation Generating the strings of the language: 1.Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. 2.Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of the rule. 3.Repeat step 2 until no variables remain.

113 Theory of Languages and Automata By Dr. Khezrian 113 Example G1: A ⇒ 0A1 ⇒ 00A11 ⇒ 000A111 ⇒ 000B111 ⇒ 000#111 Parse Tree:

114 Theory of Languages and Automata By Dr. Khezrian 114 Example All Strings generated in this way

115 Theory of Languages and Automata By Dr. Khezrian 115 Example Derivation of the sentence “a boy sees” in G2

116 Theory of Languages and Automata By Dr. Khezrian 116 Context-Free Grammar (Formal Definition) A context-free grammar is a 4-tuple (V, Σ, R, S), where : 1.V is a finite set called the variables, 2.Σ is a finite set, disjoint from V, called the terminals, 3.R is a finite set of rules, with each rule being a variable and a string of variables and terminals 4.S ∈ V is the start variable.

117 Theory of Languages and Automata By Dr. Khezrian 117 Example G3 = ({S}, {a,b}, R, S) R: S → aSb | SS | ɛ ➜ Strings such as: abab, aaabbb, aababb ! If a → ( and b → ), then: L(G3) → language of all strings of properly nested parentheses

118 Theory of Languages and Automata By Dr. Khezrian 118 Example G4 = (V, Σ, R, EXPR) V = {EXPR, TERM, FACTOR} Σ = {a, +, ×, (, )} R: EXPR → EXPR + TERM | TERM TERM → TERM × FACTOR | FACTOR FACTOR → (EXPR) | a

119 Theory of Languages and Automata By Dr. Khezrian 119 Example Parse trees for the string a+a×a

120 Theory of Languages and Automata By Dr. Khezrian 120 Example Parse trees for the string (a+a)×a

121 Theory of Languages and Automata By Dr. Khezrian 121 Context Free Grammar Design Break the CFL into simpler pieces, if possible Construct grammar for each piece Combine the rules and the new rule S→S1|S2|…|Sk, where the Si are the start variables for the individual grammars.

122 Theory of Languages and Automata By Dr. Khezrian 122 Example

123 Theory of Languages and Automata By Dr. Khezrian 123 Ambiguity When a grammar can generate the same string in different ways Undesirable for applications such as programming languages

124 Theory of Languages and Automata By Dr. Khezrian 124 Ambiguity EXPR → EXPR + EXPR | EXPR × EXPR |(EXPR) | a This grammar generates the string a+a×a ambiguously

125 Theory of Languages and Automata By Dr. Khezrian 125 Ambiguity A string w is ambiguous if it can be derived by at least two derivation (parse) trees. Leftmost (Rightmost) derivation: at every step, the leftmost (rightmost) remaining variable is replaced.

126 Theory of Languages and Automata By Dr. Khezrian 126 Ambiguity A string w is ambiguous in a context-free grammar if there are at least two different leftmost (rightmost) derivations or two different derivation trees for that string in that grammar. A context- free grammar is ambiguous if it can generates at least one ambiguous string. Otherwise it is unambiguous.

127 Theory of Languages and Automata By Dr. Khezrian 127 Example G: S → aAB A → bBb B → A | ɛ Rightmost derivation S ⇒ aAB ⇒ aA ⇒ abBb ⇒ abAb ⇒ abbBbb ⇒ abbbb Leftmost derivation S ⇒ aAB ⇒ abBbB ⇒ abbB ⇒ abbA ⇒ abbbBb ⇒ abbbb

128 Theory of Languages and Automata By Dr. Khezrian 128 Example

129 Theory of Languages and Automata By Dr. Khezrian 129 Inherently Ambiguous Languages A context-free language is (inherently) ambiguous if all context-free grammars generating it will be ambiguous.

130 Theory of Languages and Automata By Dr. Khezrian 130 Useful Variable

131 Theory of Languages and Automata By Dr. Khezrian 131 Example

132 Theory of Languages and Automata By Dr. Khezrian 132 Theorem Let G = (V, Σ, R, S) be a CFG. Then there exists an equivalent grammar G’ = (V’, Σ’, R’, S) that does not contain any useless variable or rules.

133 Theory of Languages and Automata By Dr. Khezrian 133 ε rules Any rule of a context-free grammar of the form A → ε is called an ε rule. Any variable A for which the derivation A ⇒ * ε is called nullable.

134 Theory of Languages and Automata By Dr. Khezrian 134 Example G: S → ABaC A → BC B → b | ε C → D | ε D → d A, B, C are nullable variables above.

135 Theory of Languages and Automata By Dr. Khezrian 135 Example Then we have: S → ABaC | BaC | AaC | ABa | aC | Aa | Ba | a A → B | C | BC B → b C → D D → d

136 Theory of Languages and Automata By Dr. Khezrian 136 Theorem Let G be any context-free grammar with ε not in L(G). Then there exists an equivalent grammar G’ having no ε rules.

137 Theory of Languages and Automata By Dr. Khezrian 137 Unit Rules Any rule of a context-free grammar of the form A → B where A, B  V is called a unit rule.

138 Theory of Languages and Automata By Dr. Khezrian 138 Example

139 Theory of Languages and Automata By Dr. Khezrian 139 Theorem Let G be any context-free grammar without any ε rule. Then there exists a context-free grammar G’ that does not have any unit rules and that is equivalent to G.

140 Theory of Languages and Automata By Dr. Khezrian 140 Chomsky Normal Form A context-free grammar is in Chomsky normal form if every rule is of the form → Where is an terminal and,, and are an variables except that and may not be the start variable. In addition we permit the rule →, where is the start variable. Any context-free language is generated by a context-free grammar in Chomsky normal form.

141 Theory of Languages and Automata By Dr. Khezrian 141 Example

142 Theory of Languages and Automata By Dr. Khezrian 142 Example

143 Theory of Languages and Automata By Dr. Khezrian 143 Example

144 Theory of Languages and Automata By Dr. Khezrian 144 Pushdown Automata Like nondeterministic finite automata, with an extra component: stack The stack provides additional memory Pushdown automata are equivalent in power to context-free

145 Theory of Languages and Automata By Dr. Khezrian 145 Pushdown Automata Push: Writing a symbol on the stack Pop: Removing a symbol Recognizes As each 0 is read from the input, push it onto the stack. Pop a 0 off the stack for each 1 read. Reading the input is finished exactly when the stack becomes empty → Accept

146 Theory of Languages and Automata By Dr. Khezrian 146 Pushdown Automata

147 Theory of Languages and Automata By Dr. Khezrian 147 Pushdown Automata State diagram for the PDA that recognizes

148 Theory of Languages and Automata By Dr. Khezrian 148 Give a pushdown automaton for the language:

149 Theory of Languages and Automata By Dr. Khezrian 149 Example

150 Theory of Languages and Automata By Dr. Khezrian 150 Example

151 Theory of Languages and Automata By Dr. Khezrian 151 Example

152 Theory of Languages and Automata By Dr. Khezrian 152 Example

153 Theory of Languages and Automata By Dr. Khezrian 153 Example

154 Theory of Languages and Automata By Dr. Khezrian 154 Example

155 Theory of Languages and Automata By Dr. Khezrian 155 Example

156 Theory of Languages and Automata By Dr. Khezrian 156 Example

157 Theory of Languages and Automata By Dr. Khezrian 157 Example

158 Theory of Languages and Automata By Dr. Khezrian 158 Example

159 Theory of Languages and Automata By Dr. Khezrian 159 Example

160 Theory of Languages and Automata By Dr. Khezrian 160 Example

161 Theory of Languages and Automata By Dr. Khezrian 161 Example

162 Theory of Languages and Automata By Dr. Khezrian 162 Example 1 State diagram for the PDA that recognizes

163 Theory of Languages and Automata By Dr. Khezrian 163 Example 2 Give a pushdown automaton for the language:

164 Theory of Languages and Automata By Dr. Khezrian 164 NCFL & DCFL NCFL: Non- deterministic Context Free Languages DCFL: Deterministic Context Free Languages

165 Theory of Languages and Automata By Dr. Khezrian 165 Example 3 Give a pushdown automaton for the language:

166 Theory of Languages and Automata By Dr. Khezrian 166 Example

167 Theory of Languages and Automata By Dr. Khezrian 167 Context Free Languages If R1 and R2 are CFL then: But:

168 Theory of Languages and Automata By Dr. Khezrian 168 NON-CONTEXT-FREE LANGUAGES Pumping Lemma for context-free languages: If A is a context-free language, then there is a number p (the pumping length) where, if s is any string in A of length at least p, then s may be divided into five pieces s=uvxyz satisfying the conditions:

169 Theory of Languages and Automata By Dr. Khezrian 169 Example 1

170 Theory of Languages and Automata By Dr. Khezrian 170 Example 2

171 Theory of Languages and Automata By Dr. Khezrian 171 Turing Machine Several models of computing devices Finite automata Pushdown automata Tasks that are beyond the capabilities of these models Much more powerful model Proposed by Alan Turing in 1936

172 Theory of Languages and Automata By Dr. Khezrian 172 Turing Machine Similar to a finite automaton Unlimited memory Can do everything that a real computer can do An infinite tape A tape head to read and write symbols Initially contains the input string and blank everywhere else Outputs: accept and reject If it doesn’t enter an accepting or a rejecting state, never halts

173 Theory of Languages and Automata By Dr. Khezrian 173 Turing Machine (Q, Σ, Γ, δ, q0, qaccept, qreject), 1.Q is the set of states, 2.Σ is the input alphabet not containing the blank symbol, 3.Γ is the tape alphabet where ∈ Γ and Σ ⊆ Γ, 4.δ : Q × Γ→ Q × Γ × {L, R} is the transition function, 5.q0 ∈ Q is the start state, 6.qaccept ∈ Q is the accept state, 7.qreject ∈ Q is the reject state, where qreject ≠ qaccept.

174 Theory of Languages and Automata By Dr. Khezrian 174 Example 1

175 Theory of Languages and Automata By Dr. Khezrian 175 Example 2

176 Theory of Languages and Automata By Dr. Khezrian 176 Example 3

177 Theory of Languages and Automata By Dr. Khezrian 177 I hope you enjoyed this course; Good Luck & Have a nice exam!


Download ppt "Theory of Languages and Automata By: Mojtaba Khezrian."

Similar presentations


Ads by Google