1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007.

Slides:



Advertisements
Similar presentations
CSE 311 Foundations of Computing I
Advertisements

4b Lexical analysis Finite Automata
CSC 361NFA vs. DFA1. CSC 361NFA vs. DFA2 NFAs vs. DFAs NFAs can be constructed from DFAs using transitions: Called NFA- Suppose M 1 accepts L 1, M 2 accepts.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 4 Mälardalen University 2010.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 4 School of Innovation, Design and Engineering Mälardalen University 2012.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
Costas Busch - RPI1 Grammars. Costas Busch - RPI2 Grammars Grammars express languages Example: the English language.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
Courtesy Costas Busch - RPI
1 Grammars. 2 Grammars express languages Example: the English language.
1 Languages and Finite Automata or how to talk to machines...
1 Regular Grammars Generate Regular Languages. 2 Theorem Regular grammars generate exactly the class of regular languages: If is a regular grammar then.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Nondeterminism.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
1 Reverse of a Regular Language. 2 Theorem: The reverse of a regular language is a regular language Proof idea: Construct NFA that accepts : invert the.
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
CS5371 Theory of Computation Lecture 4: Automata Theory II (DFA = NFA, Regular Language)
1 Non-Deterministic Automata Regular Expressions.
CS Chapter 2. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Fall 2004COMP 3351 Another NFA Example. Fall 2004COMP 3352 Language accepted (redundant state)
Costas Busch - LSU1 Non-Deterministic Finite Automata.
Prof. Busch - LSU1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
1 Non-Deterministic Finite Automata. 2 Alphabet = Nondeterministic Finite Automaton (NFA)
Fall 2003Costas Busch - RPI1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
Formal Language Finite set of alphabets Σ: e.g., {0, 1}, {a, b, c}, { ‘{‘, ‘}’ } Language L is a subset of strings on Σ, e.g., {00, 110, 01} a finite language,
Regular Expressions (RE) Empty set Φ A RE denotes the empty set Empty string λ A RE denotes the set {λ} Symbol a A RE denotes the set {a} Alternation M.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
Finite-State Machines with No Output
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
REGULAR LANGUAGES.
Theory of Languages and Automata
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Lecture 05: Theory of Automata:08 Kleene’s Theorem and NFA.
Lexical Analysis Constructing a Scanner from Regular Expressions.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2006.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
CS 321 Programming Languages and Compilers Lectures 16 & 17 Introduction to Formal Languages Regular Languages Lexical Analysis.
CHAPTER 1 Regular Languages
CS 3813: Introduction to Formal Languages and Automata Chapter 2 Deterministic finite automata These class notes are based on material from our textbook,
Lecture # 12. Nondeterministic Finite Automaton (NFA) Definition: An NFA is a TG with a unique start state and a property of having single letter as label.
Chapter 3 Regular Expressions, Nondeterminism, and Kleene’s Theorem Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction.
CS 154 Formal Languages and Computability February 9 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
Algorithms for hard problems Automata and tree automata Juris Viksna, 2015.
Chapter 5 Finite Automata Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.
CS 154 Formal Languages and Computability February 11 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
Costas Busch - LSU1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
Complexity and Computability Theory I Lecture #5 Rina Zviel-Girshin Leah Epstein Winter
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2006.
Deterministic Finite Automata Nondeterministic Finite Automata.
1 Finite Automata. 2 Introductory Example An automaton that accepts all legal Pascal identifiers: Letter Digit Letter or Digit "yes" "no" 2.
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
Non Deterministic Automata
Two issues in lexical analysis
Chapter 2 FINITE AUTOMATA.
Non-Deterministic Finite Automata
Principles of Computing – UFCFA3-30-1
Non-Deterministic Finite Automata
CSE322 Definition and description of finite Automata
Non Deterministic Automata
Finite Automata.
Discrete Math II Howon Kim
Chapter 3. Lexical Analysis (2)
4b Lexical analysis Finite Automata
Chapter 1 Regular Language
Presentation transcript:

1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007

2 Content Finite Automata, FA Deterministic Finite Automata, DFA Nondeterministic Automata NFA NFA  DFA Equivalence Grammatik Linear grammar Regular grammar

3 Finite Automata FA (Finite State Machines)

4 There is no formal general definition for "automaton". Instead, there are various kinds of automata, each with it's own formal definition. has some form of input has some form of output has internal states, may or may not have some form of storage is hard-wired rather than programmable Generally, an automaton

5 Finite Automaton Input String Output String Finite Automaton

6 Finite Accepter Input “Accept” or “Reject” String Finite Automaton Output

7 Nodes = States Edges = Transitions An edge with several symbols is a short-hand for several edges: FA as Directed Graph

8 Deterministic there is no element of choice Finite only a finite number of states and arcs Acceptors produce only a yes/no answer Deterministic Finite Automata DFA

9 Transition Graph initial state final state “accept” state transition abba -Finite Acceptor Alphabet =

10 Formal definitions Deterministic Finite Accepter (DFA) : set of states : input alphabet : transition function : initial state : set of final states

11 Input Aplhabet

12 Set of States

13 Initial State

14 Set of Final States

15 Transition Function

16

17

18

19 Transition Function

20 Extended Transition Function

21

22

23 Formally For a DFA Language accepted by : alphabet transition function initial state final states

24 Observation Language accepted by Language rejected by

25 Regular Languages All regular languages form a language family. A language is regular if there is a DFA such that

26 Nondeterministic there is an element of choice: in a given state NFA can act on a given string in different ways. Several start/final states are allowed. -transitions are allowed. Finite only a finite number of states and arcs Acceptors produce only a yes/no answer Nondeterministic Automata NFA

27 Two choices Alphabet = Nondeterministic Finite Accepter (NFA)

28 First Choice

29 First Choice

30 First Choice

31 “accept” First Choice

32 Second Choice

33 Second Choice

34 Second Choice No transition: the automaton hangs

35 Second Choice “reject”

36 Observation An NFA accepts a string if there is a computation of the NFA that accepts the string.

37 Example is accepted by the NFA:

38 Lambda Transitions

39

40

41 (read head doesn’t move)

42

43 “accept” String is accepted

44 Language accepted:

45 Another NFA Example Alphabet =

46

47

48

49 “accept”

50 Another String Alphabet =

51

52

53

54

55

56

57 “accept”

58 Language accepted Alphabet =

59 Further NFA Example Alphabet =

60 Language accepted

61 Formal Definition of NFA Set of states, i.e. Input alphabet, i.e. Transition function Initial state Final states

62 Transition Function

63

64

65

66 Extended Transition Function (Utvidgad övergångsfunktion)

67

68

69 Formally if and only if there is a walk from to with label

70 The Language of an NFA Alphabet =

71

72

73

74

75 Formally The language accepted by NFA is: (final state) where and there is some (at least one)

76

77 Equivalence of NFAs and DFAs Accept the same languages? YES! NFAs  DFAs ? Same power?

78 We will prove: Languages accepted by DFAs Languages accepted by NFAs NFAs and DFAs have the same computation power!

79 Languages accepted by DFAs Languages accepted by NFAs Step 1 Proof Every DFA is also an NFA. A language accepted by a DFA is also accepted by an NFA.

80 Languages accepted by NFAs Languages accepted by DFAs Step 2 Proof Any NFA can be converted to an equivalent DFA. A language accepted by an NFA is also accepted by a DFA.

81 Procedure NFA to DFA 1. Initial state of NFA: Initial state of DFA:

82 Example NFA DFA Step 1

83 Procedure NFA to DFA 2. For every DFA’s state Compute in the NFA Add transition

84 Example NFA DFA Step 2

85 Procedure NFA to DFA Repeat Step 2 for all letters in alphabet, until no more transitions can be added.

86 Example NFA DFA Step 3

87 Procedure NFA to DFA 3. For any DFA state If some is a final state in the NFA Then is a final state in the DFA

88 Example NFA DFA Step 4

89 Theorem Take NFA Apply procedure to obtain DFA Then and are equivalent :

90 Languages accepted by NFAs Languages accepted by DFAs We have proven (proof by construction): Regular Languages END OF PROOF

91 Nondeterministic vs. Deterministic Automata

92 Formal Definition of NFA Set of states, i.e. Input alphabet, i.e. Transition function Initial state Final (accepting) states NFA is a mathematical model defined as a quintuple:

93 Deterministic Finite Automata A deterministic finite automaton (DFA) is a special case of a nondeterministic finite automaton in which 1. no state has an -transition, i.e. a transition on input, and 2. for each state q and input symbol a, there is at most one edge labeled a leaving q.

94 STATE INPUT SYMBOL ab {0, 1} - {0} {2} {3} Transition table for the finite automaton above A nondeterministic finite automaton b 0 start 1 a 2 bb 3 a Example

95 NFA accepting aa* + bb* 0 start 1 a 2 a 3 b 4 b Example

96 NFA accepting (a+b)*abb 0 start 1 a 2 bb b aa a b 3 a Example

97 NFA recognizing three different patterns. (a) NFA for a, abb, and a*b +. (b) Combined NFA. Example 4 1 start a 2 3 a 65 b b 7 b 8 b a 4 1 a 2 3 a 65 bb 7 b 8 b a 0

98 Ways to think of nondeterminism always make the correct guess “backtracking” (systematically try all possibilities) For a particular string, imagine a tree of possible state transitions: q0q0 q3q3 q0q0 q4q4 q2q2 q1q1 a a a a b a

99 Advantages of nondeterminism An NFA can be smaller, easier to construct and easier to understand than a DFA that accepts the same language. NFA’s can be useful for proving some theorems. NFA’s can be good introduction to nondeterminism in more powerful computational models, where nondeterminism plays an important role.

100 Space and time taken to recognize regular expressions: - NFA more compact but take time to backtrack all choices - DFA take place, but save time AUTOMATONSPACETIME NFA DFA O(|r|) O(2 |r| ) O(|r|  |x|) O(|x|) Determinism vs. nondeterminism (Where r is regular expression, and x is input string)

101 Equivalent automata Two finite automata M 1 and M 2 are equivalent if L(M 1 ) = L(M 2 ) that is, if they both accept the same language.

102 Equivalence of NFAs and DFAs To show that NFAs and DFAs accept the same class of languages, we show two things: –Any language accepted by a DFA can also be accepted by some NFA (As DFA is a special case of NFA). –Any language accepted by a NFA can also be accepted by some (corresponding, specially constructed) DFA.

103 Proof Strategy To show that any language accepted by a NFA is also accepted by some DFA, we describe an algorithm that takes any NFA and converts it into a DFA that accepts the same language. The algorithm is called the “subset construction algorithm”. We can use mathematical induction (on the length of a string accepted by the automaton) to prove the DFA that is constructed accepts the same language as the NFA.

104 Converting NFA to DFA Subset construction Given a NFA construct a DFA that accepts the same language. The equivalent DFA simulates the NFA by keeping track of the possible states it could be in. Each state of the DFA is a subset of the set of states of the NFA - hence, the name of the algorithm. If the NFA has n states, the DFA can have as many as 2 n states, although it usually has many less.

105 Steps of Subset Construction The initial state of the DFA is the set of all states the NFA can be in without reading any input. For any state {q i,q j,…,q k } of the DFA and any input a, the next state of the DFA is the set of all states of the NFA that can result as next states if the NFA is in any of the states q i,q j,…,q k when it reads a. This includes states that can be reached by reading a, followed by any number of -moves. Use this rule to keep adding new states and transitions until it is no longer possible to do so. The accepting states of the DFA are those states that contain an accepting state of the NFA.

106 Example Here is a NFA that we want to convert to an equivalent DFA

107 {0,1} The start state of the DFA is the set of states the NFA can be in before reading any input. This includes the start state of the NFA and any states that can be reached by a -transition. NFA DFA

108 {0,1} a b {2} For start state {0,1}, make transitions for each possible input, here a and b. Reading b from start {0,1}, we reach state {2}. Means from either {0}, or {1} we reach {2}. NFA DFA

109 For state {2}, we create a transition for each possible input, a and b. From {2}, with b we are either back to {2} (loop) or we reach {1}- see the little framed original NFA. So from {2}, with b we end in state {1, 2}. Reading a leads us from {2} to {0} in the original NFA, which means state {0, 1} in the new DFA. {0,1} {1,2} {2} NFA DFA

110 For state {1, 2}, we make again transition for each possible input, a and b. From {2} a leads us to {0}. From {1} with a we are back to {1}. So, we reach {0, 1} with a from {1,2}. With b we are back to {1,2}. At this point, a transition is defined for every state-input pair. {0,1} {1,2} {2} DFA NFA

111 The last step is to mark the final states of the DFA. As {1} was the accepting state in NFA, all states containing {1} in DFA will be accepting states: ({0, 1} and {1, 2}). {0,1} {1,2} {2} DFA NFA

112 Subset Construction Algorithm States of nondeterministic M´ will correspond to sets of states of deterministic M. Where q 0 is start state of M, use {q 0 } as start state of M´. Accepting states of M´ will be those state-sets containing at least one accepting state of M.

113 Subset Construction (cont.) For each state-set S and for each s in alphabet of M, we draw an arc labeled s from state S to that state-set consisting of all and only the s-successors of members of S. Eliminate any state-set, as well as all arcs incident upon it, such that there is no path leading to it from {q 0 }.

114 The power set of a finite set, Q, consists of 2 |Q| elements The DFA corresponding to a given NFA with Q states have a finite number of states, 2 |Q|. If |Q| = 0 then Q is the empty set, | P(Q)| = 1 = 2 0. If |Q| = N and N  1, we construct subset of a given set so that for each element of the initial set there are two alternatives, either is the element member of a subset or not. So we have 2 · 2 · 2 · 2 · 2 · 2 · 2…. ·2 = 2 N N times

115 From an NFA to a DFA Subset Construction Operation Description - closure(s) - closure(T) Move(T,a) Set of NFA states reachable from an NFA state s on -transitions along Set of NFA states reachable from some NFA state s in T on -transitions along Set of NFA states reachable from some NFA state set with a transition on input symbol a

116 From an NFA to a DFA Subset Construction Initially, -closure (s 0 ) is the only states in D and it is unmarked while there is an unmarked state T in D do mark T; for each input symbol a do U:= e-closure(move(T,a)); if U is not in D then add U as an unmarked state to D Dtran[T,a]:=U; end(for) end(while)

117 Grammars Grammars express languages Example: the English language

118 barksverb singsverb   dognoun birdnoun   thearticle a  

119 A derivation of “the bird sings”:         birdthe verbbirdthe verbnounthe verbnounarticle predicatenounarticle predicatephrasenounsentence sings _

120 A derivation of “a dog barks”: barksdoga verbdoga verbnouna verbnounarticle verbphrasenoun predicatephrasenounsentence       _ _

121 The language of the grammar:

122 Notation Non-terminal (Variable) Terminal Production rule

123 Example Derivation of sentence: Grammar:

124 Grammar: Derivation of sentence

125 Other derivations

126 The language of the grammar

127 Formal Definition Grammar Set of variables Set of terminal symbols Start variable Set of production rules

128 Example Grammar

129 Sentential Form A sentence that contains variables and terminals Example sentential formsSentence (sats)

130 We write: Instead of:

131 In general we write if By default ()

132 Example Grammar Derivations

133 baaaaaSbbbbaaSbb   S   Grammar Example Derivations

134 Another Grammar Example Derivations Grammar

135 More Derivations

136 The Language of a Grammar For a grammar with start variable String of terminals

137 Example For grammar Since

138 Notation

139 Linear Grammars Grammars with at most one variable (non-terminal) at the right side of a production Examples:

140 A Non-Linear Grammar Grammar

141 Another Linear Grammar Grammar

142 Right-Linear Grammars All productions have form: or Example

143 Left-Linear Grammars All productions have form or Example

144 Regular Grammars Generate Regular Languages

145 Theorem Languages Generated by Regular Grammars Regular Languages

146 Theorem - Part 1 Languages Generated by Regular Grammars Regular Languages Any regular grammar generates a regular language

147 Theorem - Part 2 Any regular language is generated by a regular grammar Languages Generated by Regular Grammars Regular Languages

148 Proof – Part 1 The language generated by any regular grammar is regular Languages Generated by Regular Grammars Regular Languages

149 The case of Right-Linear Grammars Let be a right-linear grammar We will prove: is regular Proof idea We will construct NFA with

150 Grammar is right-linear Example

151 Construct NFA such that every state is a grammar variable: special final state

152 Add edges for each production:

153

154

155

156

157

158 Grammar NFA

159 In General A right-linear grammar has variables: and productions: or

160 We construct the NFA such that: each variable corresponds to a node: special final state ….

161 For each production: we add transitions and intermediate nodes ………

162 Example

163 The case of Left-Linear Grammars Let be a left-linear grammar We will prove: is regular Proof idea We will construct a right-linear grammar with

164 Since is left-linear grammar the productions look like:

165 Construct right-linear grammar In :

166 Construct right-linear grammar In :

167 It is easy to see that: Since is right-linear, we have: Regular Language Regular Language Regular Language

168 Proof - Part 2 Any regular language is generated by some regular grammar Languages Generated by Regular Grammars Regular Languages

169 Proof idea Any regular language is generated by some regular grammar Construct from a regular grammar such that Since is regular there is an NFA such that

170 Example

171 Convert to a right-linear grammar

172 In General For any transition: Add production: variableterminalvariable

173 For any final state: Add production:

174 Since is right-linear grammar is also a regular grammar with

175 Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples

176 Observation Regular grammars generate regular languages Examples

177 Regular Languages Chomsky’s Language Hierarchy Non-regular languages