1 / 16

Lexical Analysis Part 2

Lexical Analysis Part 2. Professor Yihjia Tsai Tamkang University. Regular Expressions and FA are Equivalent. For every regular expression, there is a deterministic finite-state machine that defines the same language, and vice versa. NFA. Regular expressions. DFA. e Closure Operator.

yukio
Download Presentation

Lexical Analysis Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lexical Analysis Part 2 Professor Yihjia Tsai Tamkang University

  2. Regular Expressions and FA are Equivalent For every regular expression, there is a deterministic finite-state machine that defines the same language, and vice versa NFA Regular expressions DFA

  3. eClosure Operator • The eClosure operator is defined as eClosure(s) = { s } U states reachable from s using e transitions. • Example: eClosure(1) = {1,3} a  start 1 5 3 a a/b b 2 4

  4. Subset Construction for NFA-> DFA • Compute A = eClosure(start) • Compute the set of states reachable from A on transition a, call this new set S’ • Compute eClosure(S’) – this is the new state and label it with the next available label • Continue for all possible transitions from the current state for all applicable elements of S • Repeat steps 2-4 for each new state

  5. e a b e e c 1 6 3 2 5 4 e b b a A C B D c c Comparison from Last Time NFA: DFA:

  6. Example: aa*b | aab* NFA: a a b 2 1 3 b a a 4 5

  7. Example: aa*b | aab*

  8. a b B A D b a a C a E b b b F G Resultant DFA How is this result different than before?

  9. Generalized NFA • Generalized Nondeterministic Finite Automaton M=(Q, , , qstart, qaccept) with • Q finite set of states •  the input alphabet • qstart the start state • qaccept the accept state • :(Q\{qaccept})(Q\{qstart})  R the transition function • (R is the set of regular expressions over )

  10. Example GNFA 0110 0  qS qA  01* 0* | 11

  11. Characteristics of GNFA’s  • :(Q\{qaccept})(Q\{qstart})  R The interior Q\{qaccept,qstart} is fully connected by  From qstart only ‘outgoing transitions’ To qaccept only ‘ingoing transitions’ Impossible qiqj transitions are “(qi,qj) = ” Observation: This GNFA:recognizes the language L(R) RR qS qA

  12. qj DFA M  Equivalent GNFA M’ Let M have k states Q={q1,…,qk} - Add two states qaccept and qstart  q1 qS - Connect qstart to earlier q1:  qj qA - Connect old accepting states to qaccept  - Complete missing transitions by qi - Join multiple transitions: 1 becomes 0|1 qj qi qj qi 0

  13. Remove Internal state of GNFA If the GNFA M has more than 2 states, ripinternal qrip to get equivalent GNFA M’ by: - Removing state qrip: Q’=Q\{qrip} - Changing the transition function  by’(qi,qj) = (qi,qj) | ((qi,qrip)((qrip,qrip))*(qrip,qj)) for every qiQ’\{qaccept} and qjQ’\{qstart} R1 qrip R2 R4|(R1R2*R3) qj qi qi = R3 qj R4

  14. Try at Home • What is the regular expression for • Binary numbers, multiples of 2 • Strings of a’s and b’s with a at start and finish • Strings of a’s and b’s, no consecutive a’s

  15. Try at Home • What is the NFA for the following RE? ((a|b)*c) | (a b c*)

  16. References • Aho, A.V., R. Sethi, and J.D. Ullman, Compilers Principles, Techniques and Tools, Addison-Wesley, 1988. ISBN 0-201-10088-6. • Appel, A., Modern Compiler Implementation In Java (2nd Ed), Cambridge University Press, 2002. ISBN 052182060X.

More Related