Presentation is loading. Please wait.

Presentation is loading. Please wait.

WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.

Similar presentations


Presentation on theme: "WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University."— Presentation transcript:

1 WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University

2 2

3 Input “Accept” or “Reject” String Finite Automaton Output

4 4  A state The start state An accepting state A transition a

5 5  A finite automaton that accepts only “1”  A finite automaton accepts a string if we can follow transitions labeled with the characters in the string from the start to some accepting state 1

6 6  A finite automaton accepting any number of 1’s followed by a single 0  Alphabet: {0,1}  Check that “1110” is accepted but “0110” is not 0 1

7 baaa*!

8  Definition: A finite-state automaton is a 5-tuple M=(S, I, f, S 0, F) where  S is a finite set of states  I is a finite input alphabet  f:S  I  S is a transition function from each state- input pair to a state  S 0 is the initial state  F  S is a set of final states  Note: automaton is the singular of automata.

9  FSA is a 5-tuple consisting of  Q: set of states {q0,q1,q2,q3,q4}   : an alphabet of symbols {a,b,!}  q0: a start state in Q  F: a set of final states in Q {q4}   (q,i): a transition function mapping Q x  to Q q0 q4 q1q2q3 ba a a!

10  An FSA recognizes (accepts) strings of a regular language  baa!  baaa!  baaaa! ……  Tape metaphor: will this input be accepted? baAa

11 State Input ba! 01-- 1-2- 2-3- 3-34 4--- q0 q4 q1q2q3 ba a a!

12  Possible Goals  Determine whether a string should be accepted by a machine  Or… determine whether a string is in the language defined by the automaton  Or… determine whether a regular expression matches a string  This process can be represented with an input tape

13 aba!b q0q0 0 1234 baa ! a REJECT Slide from Dorr/Monz

14 baaa q0q0 q1q1 q2q2 q3q3 q3q3 q4q4 ! 0 1234 baa ! a ACCEPT Slide from Dorr/Monz

15 15  Alphabet {0,1}  What language does this recognize? 0 1 0 1 0 1

16 16  Alphabet still { 0, 1 } The operation of the automaton is not completely defined by the input  On input “11” the automaton could be in either state 1 1

17  Definition: A finite-state automaton accepts (or recognizes) a string x if f(S 0, x)  F. That is, the finite state automaton ends up in a final state.  Definition: The language accepted (or recognized) by a finite-state automaton M, denoted by L(M), is the set of all strings recognized by M.  Definition: Two finite-state automata are equivalent if they recognize the same language.

18  If end of input  If in accepting state => accept  Otherwise => reject  If no transition possible (got stuck) => reject

19  A finite automaton is a recognizer that takes a string, and answers “yes” if the string matches a pattern of a specified language, and “no” otherwise.  Two kinds:  Nondeterministic finite automaton (NFA)  no restriction on the labels of their edges  Deterministic finite automaton (DFA)  exactly one edge with a distinguished symbol goes out of each state  Both NFA and DFA have the same capability  We may use NFA or DFA as lexical analyzer 19

20 20  Deterministic Finite Automata (DFA)  One transition per input per state  No  -moves  Nondeterministic Finite Automata (NFA)  Can have multiple transitions for one input in a given state  Can have  -moves  Finite automata have finite memory  Need only to encode the current state

21 21  Another kind of transition:  -moves  Machine can move from state A to state B without reading input A B

22  FSMs can be represented by:  Transition diagrams  Transition tables

23 The language recognized by this NFA is (a|b) * a b 23

24 NFA accepting aa* |bb* 24

25  initial state accepting state transition

26  Input String

27

28

29

30

31 accept Input finished

32

33

34

35

36 reject Input finished

37

38  reject

39

40

41

42

43 accept Input finished

44

45

46

47

48 reject Input finished

49  accept

50

51 51  High-level sketch Regular expressions NFA DFA Lexical Specification Table-driven Implementation of DFA

52  Regular expression is a declarative way to describe the tokens  It describes what is a token, but not how to recognize the token.  FA is used to describe how the token is recognized  FA is easy to be simulated by computer programs;  There is a 1-1 correspondence between FA and regular expression  Scanner generator (such as lex) bridges the gap between regular expression and FA. Scanner generator Finite automaton Regular expression scanner program String stream Tokens

53 Main components of scanner generation (e.g., Lex)  Convert a regular expression to a non-deterministic finite automaton (NFA)  Convert the NFA to a determinstic finite automaton (DFA)  Improve the DFA to minimize the number of states  Generate a program in C or some other language to “simulate” the DFA RE NFA DFA Minimized DFA Program Subset construction DFA simulation Scanner generator Minimization

54 54

55  NFA (Non-deterministic Finite Automaton) is a 5-tuple (S, Σ, , S0, F):  S: a set of states;   : the symbols of the input alphabet;   : a set of transition functions;  move(state, symbol)  a set of states  S0: s0  S, the start state;  F: F  S, a set of final or accepting states.  Non-deterministic -- a state and symbol pair can be mapped to a set of states.  Finite—the number of states is finite.

56  NFA can be represented by a transition graph  Accepts a string x, if and only if there is a path from the starting state to one of accepting states such that edge labels along this path spell out x.  Remarks  The same symbol can label edges from one state to several different states  An edge may be labeled by ε, the empty string 56

57 57  For each kind of rexp, define an NFA  Notation: NFA for rexp A A For   For input a a

58 58  For AB A B  For A | B A B    

59 59  For A* A   

60 60  Consider the regular expression (1 | 0)*1  The NFA is (there are multiple valid answers)  1 C E 0 DF   B   G    A H 1 IJ

61 61 Alphabet =  NFA travels all possible paths, and so it remains in many states at once. As long as at least one of the paths results in an accepting state, the NFA accepts the input.

62 62 Two choices Alphabet =

63 63 No transition Two choices No transition Alphabet =

64 64 An NFA accepts a string: if there is a computation of the NFA that accepts the string i.e., all the input string is processed and the automaton is in an accepting state

65 65 Acceptance Example 1

66 66

67 67 “reject” “accept”

68 68 is accepted by the NFA: “accept” “reject” because this computation accepts this computation is ignored

69 69 An NFA rejects a string: if there is no computation of the NFA that accepts the string. All the input is consumed and the automaton is in a non final state The input cannot be consumed OR For each computation:

70 70 is rejected by the NFA: “reject” All possible computations lead to rejection

71 71 is rejected by the NFA: “reject” All possible computations lead to rejection

72 72 Remember! Bothe Lambda λ and Epsilon є can be used to represent an empty move

73 73 Acceptance Example 2

74 74

75 75 input tape head does not move

76 76 “accept” String is accepted all input is consumed

77 77 Rejection Example 3

78 78

79 79 (read head doesn’t move)

80 80 “reject” String is rejected Input cannot be consumed Automaton halts

81 81 Language accepted:

82 82 Example 4 what is the language accepted by this automata?

83 83

84 84

85 85 “accept”

86 86 Another String

87 87

88 88

89 89

90 90

91 91

92 92 “accept”

93 93 Language accepted

94 94

95 95 Language accepted (redundant state)


Download ppt "WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University."

Similar presentations


Ads by Google