Cs419 lec6 lexical analysis using nfa

download Cs419 lec6   lexical analysis using nfa

of 42

  • date post

    11-May-2015
  • Category

    Education

  • view

    153
  • download

    0

Embed Size (px)

Transcript of Cs419 lec6 lexical analysis using nfa

  • 1.WELCOME TO A JOURNEY TO CS419Dr. Hussien Sharaf Computer Science Departmentdr.sharaf@from-masr.com

2. Dr. Hussien M. SharafNON DETERMINISTIC FINITE AUTOMATA NFA There is a fixed number of states but we can be in multiple states at one time.NFA = a 5-tuple (Q, , , q0, F)Q A finite set of states A finite input alphabet q0 The initial/starting state, q0 is in Q F A set of final/accepting states, which is a subset of Q A transition function, which is a total function from Q x to 2Q , this function: Takes a state and input symbol as arguments. Returns a set of states instead a single state as in DFA. : (Q x ) > 2Q -2Q is the power set of Q, the set of all subsets of Q (q,s) is a function from Q x S to 2Q (but not to Q) 2 3. NFA A finite automaton is deterministic if It has no edges/transitions labeled with epsilon/lamda. For each state and for each symbol in the alphabet, there is exactly one edge labeled with that symbol. 4. Dr. Hussien M. SharafNFA 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.Alphabet = {a } aq0q1aq2aq3 4 5. Dr. Hussien M. SharafNFA Alphabet ={a }Two choicesaq0q1aq2aq3 5 6. Dr. Hussien M. SharafNFA Alphabet ={a }Two choicesaq0q1aq2No transitionaq3No transition6 7. Dr. Hussien M. SharafNFA An NFA accepts a string: if there is a computation of the NFA that accepts the stringi.e., all the input string is processed and the automaton is in an accepting state7 8. Dr. Hussien M. SharafAcceptance Example 1NFA aaaq0q1aq2aq3 8 9. Dr. Hussien M. SharafFirst ChoiceNFA aaaq0q1aq2aq3 9 10. Dr. Hussien M. SharafFirst ChoiceNFA aaAll input is consumedaq0q1aq3aq2accept 11. Dr. Hussien M. SharafSecond ChoiceNFA aaaq0q1aq3aq2 12. Dr. Hussien M. SharafSecond ChoiceNFA aaInput cannot be consumedaq1aq2Automaton Haltsq0aq 3 reject 13. Dr. Hussien M. SharafNFAaa is accepted by the NFA: acceptaq0q1q2aq0aq3because this computation accepts aaaq1aq2aq3rejectthis computation is ignored 13 14. Dr. Hussien M. SharafNFA An NFA rejects a string: if there is no computation of the NFA that accepts the string. For each computation: All the input is consumed and the automaton is in a non final stateOR The input cannot be consumed 14 15. Dr. Hussien M. SharafNFAais rejected by the NFA:rejectaq0q1aq2aq0aq 3 rejectq1aq2aq3All possible computations lead to rejection 15 16. Dr. Hussien M. SharafNFA aaa is rejected by the NFA: rejectaq0q1aq2aq0aq3q1aq2aq3rejectAll possible computations lead to rejection 16 17. Dr. Hussien M. SharafLAMBDA TRANSITIONSq0aq1q2aq317 18. Dr. Hussien M. SharafLAMBDA TRANSITIONSAcceptance Example 2 aaq0aq1q2aq318 19. Dr. Hussien M. SharafLAMBDA TRANSITIONSaaq0aq1q2aq319 20. Dr. Hussien M. SharafLAMBDA TRANSITIONS input tape head does not moveaaq0aq1q2aq3 21. Dr. Hussien M. SharafLAMBDA TRANSITIONS all input is consumedaaacceptq0aq1q2aq3String aa is accepted 21 22. Dr. Hussien M. SharafLAMBDA TRANSITIONSRejection Example 3 aaaq0aq1q2aq322 23. Dr. Hussien M. SharafLAMBDA TRANSITIONSaaaq0aq1q2aq323 24. Dr. Hussien M. SharafLAMBDA TRANSITIONS (read head doesnt move)aaaq0aq1q2aq324 25. Dr. Hussien M. SharafLAMBDA TRANSITIONS Input cannot be consumedaaaAutomaton haltsrejectq0aq1q2aq3String aaa is rejected 25 26. Dr. Hussien M. SharafLAMBDA TRANSITIONSLanguage accepted:q0aq1L {aa }q2aq326 27. Dr. Hussien M. SharafExample 4q0abq1q2q3 27 28. Dr. Hussien M. Sharafa bq0abq1q2q3 28 29. Dr. Hussien M. Sharafa bq0abq1q2q3 29 30. Dr. Hussien M. Sharafa bacceptq0abq1q2q3 30 31. Dr. Hussien M. SharafAnother Stringa b a bq0abq1q2q3 31 32. Dr. Hussien M. Sharafa b a bq0abq1q2q3 32 33. Dr. Hussien M. Sharafa b a bq0abq1q2q3 33 34. Dr. Hussien M. Sharafa b a bq0abq1q2q3 34 35. Dr. Hussien M. Sharafa b a bq0abq1q2q3 35 36. Dr. Hussien M. Sharafa b a bq0abq1q2q3 36 37. Dr. Hussien M. Sharafa b a bacceptq0abq1q2q3 37 38. Dr. Hussien M. SharafLanguage acceptedL ab , abab , ababab , ... ab q0abq1q2q3 38 39. Dr. Hussien M. SharafEXAMPLE 50 q01q10, 1q2 39 40. Dr. Hussien M. SharafLanguage acceptedL ( M ) = { , 10 , 1010 , 101010 , ... } = {10 } *0 q01q10, 1q2(redundant state) 40 41. DETERMINISTIC AND NONDETERMINISTIC AUTOMATA Deterministic Finite Automata (DFA) Onetransition per input per state No -moves Nondeterministic Finite Automata (NFA) Canhave multiple transitions for one input in a given state Can have -moves41 42. THANK YOUDr. Hussien M. Sharaf42