1 / 18

ICS312 set 30

ICS312 set 30. Converting NFAs to DFAs How Lex is constructed. Converting a nfa to a dfa. Defn: The e -closure of a state is the set of all states, including S itself, that you can get to via e -transitions. The e -closure of state S is denoted:. Converting a nfa to a dfa (Cont.).

karmaj
Download Presentation

ICS312 set 30

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. ICS312 set 30 Converting NFAs to DFAs How Lex is constructed

  2. Converting a nfa to a dfa Defn: The e-closure of a state is the set of all states, including S itself, that you can get to via e-transitions. The e-closure of state S is denoted:

  3. Converting a nfa to a dfa (Cont.) Example: The e-closure of state 1 = { 1, 2, 4 } The e-closure of state 3 = { 3, 2, 4 } Defn: The e-closure of a set of states S1, ... Sn is S1È S2È ... È Sn. Example: The e-closure for above states 1 and 3 is { 1, 2, 4 } È { 3, 2, 4 } = { 1, 2, 3, 4 }

  4. To construct a dfa from a nfa Step 1: Let the start state of the dfa be formed from the e-closure of the start state of the nfa. Subsequent steps: If S is any state that you have previously constructed for the dfa and it is formed from say states t1, ... , tr of the nfa, then for any symbol x for which at least one of the states t1, ... , tr has a x-successor, the x-successor of S is the e-closure of the x-successors of t1, ... , tr. Any state of the dfa which is formed from an accepting state, among others, of the nfa becomes an accepting state.

  5. To construct a dfa from a nfa (Cont.1) Example 1: To convert the following nfa: b 5 we get: This constructs a dfa that has no epsilon-transitions and a single accepting state.

  6. To construct a dfa from a nfa (Cont.2) Example 2: To convert the nfa for an identifier to a dfa

  7. To construct a dfa from a nfa (Cont.3) we get:

  8. Minimizing the Number of States in a DFA Step 1: Start with two sets of states (a) all the accepting states, and (b) all the non-accepting states Subsequent steps: Given the sets of states S1, ... Sr consider each set S and each symbol x in turn. If any member of S has a x-successor and this x-successor is in say S', then unless all the members of S have x-successors that are in S', split up S into those members whose x-successors are in S' and the others (which don't have x-successors in S').

  9. Minimizing the Number of States in a DFA(Cont.1) Example 1. Consider the dfa we constructed for an identifier (with renumbered states):

  10. Minimizing the Number of States in a DFA(Cont.2) The sets of states for this dfa are: S1S2 Nonaccepting states Accepting states 1 2 3 4 All states in S2 have the successors letter-successor and digit-successor, and the successor states are all in the set of states S2. Combine all the states of S2 to get:

  11. Minimizing the Number of States in a DFA(Cont.3) Example 2. Consider the dfa: All of the states (1, 2, and 3) are accepting states and all their successors are also accepting states, but state 1 has an a-successor whereas states 2 and 3 do not.

  12. Minimizing the Number of States in a DFA(Cont.4) So, we split the set of accepting states into two sets S1 and S2 where: S1 consists of state 1, and S2 consists of states 2, 3 to get:

  13. HOW LEX WORKS Using the methods described above, Lex constructs a mimimized finite automata for each regular expression in the definition file. Lex generates a C program, which we will refer to as lex.yy.c The finite automatas are represented in lex.yy.c by a set of arrays.

  14. + 7 4 For instance, a portion of a finite automata such as: . can be represented by entering. in the associated array, a 7 in the column for “+” at row 4.

  15. lex.yy.c keeps track of the latest accepting state it has reached in any of the finite automatas, plus the number of source characters it has read at that point. When it reaches a stage that no transition exists for the next source symbol from any of the states it has reached in any of the finite automatas, it picks the regular expression corresponding to the finite automata in which this last accepting state occurs, and it pops off the remaining input only those characters read that lead to that state

  16. Consider, for example, a Lex defn. file containing: {digit}+(”.” {digit}+)? {…return Number;} {digit}+(”.” {digit}+)?e{digit}+ {…return Float;} Finite automata corresponding to the above re’s are: . digit digit digit 1 1 2 3 4 dfa for Number digit digit . e digit digit dfa for Float digit digit e 1 1 2 3 4 4 4 5 6 digit digit digit digit

  17. Example 1: let the remaining input be 36e8=X1… On reading the “3”, lex.yy.c records that the latest accepting state encountered is state 2 in the dfa for Number, and the no. of source characters read in getting to that state is 1. (It has also reached the non-accepting state 2 in the dfa for Float). On reading the “6”, lex.yy.c records the above again, i.e , except that here the no. of characters read in getting to accepting state 2 is 2. On reading the “e”, lex.yy.c records that it has reached state5 in the dfa for Float and the no. of characters read is 3 On reading the “8”, lex.yy.c records that the latest accepting state is state 6 in the dfa for Float, and no. of characters leading to that state is 4. On reading the “=”, lex.yy.c finds that state 6 has no “=“ successor. This is the 5th character read. So the last accepting state (state 6) is in the dfa for Float after 4 characters had been read. Hence Float is taken as matching the remaining input, and the 4 character read, i.e 36e8 is removed from the remaining input, and placed in yytext, and yyleng is set to 4.

  18. Example 2. Now consider the case where the remaining input is: 36e-45-23 Here, after reading 3 symbols (36e), the finite automata for Float reaches state 5, where no action is defined for the character “-”. State 5 is not an accepting state. The last accepting state was state 2 in the finite automata for Number which was reached after two characters had been read. So Lex.yy.c reports the next input symbol is Number, and two symbols are removed from the remaining input, which then becomes e-45-23. The two symbols involved (36) are placed in yytext, and yyleng is set to 2.

More Related