Presentation is loading. Please wait.

Presentation is loading. Please wait.

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.

Similar presentations


Presentation on theme: "College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation."— Presentation transcript:

1 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation Techniques Dr. Ying JIN Associate Professor Oct. 2007

2 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -2- Implementation of DFA

3 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -3- Implementation of DFA Objective (meaning of implementing a DFA) –Given a DFA which defines rules for a set of strings –Develop a program, which Read a string Check whether this string is accepted by the DFA If a string is accepted by a DFA, –Next state; –Stop in the final state; If a string is not accepted by a DAF, –No next state ( ⊥ ); –Not stop in the final state;

4 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -4- Implementation of DFA Two ways –Basing on transforming table of DFA –Basing on graphical representation of DFA

5 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -5- Transforming Table based Implementation Main idea –Input : a string –Output: true if acceptable, otherwise false –Data structure Transforming table (two dimensional array T) –Two variables CurrentState: record current state; CurrentChar: record current character that is read in the string;

6 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -6- Transforming Table based Implementation Main idea –General Algorithm 1.CurrentState = S 0 2. read the first character as CurrentChar 3. if CurrentChar is not the end of the string, if T(CurrentState,CurrentChar)  error, CurrentState = T(CurrentState,CurrentChar), read next character of the string as CurrentChar, goto 3; 4. if CurrentChar is the end of the string and CurrentState is one of the terminal states, return true; otherwise, return false.

7 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -7- Example abcd S0S0 S1 ⊥ S2S*S* S1 ⊥ ⊥ S2 S*S* ⊥⊥⊥ S*S* ⊥⊥ S*S* ⊥ 1) abbacc true 2) cab false

8 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -8- Transforming table for the DFA Variables: CurrentChar, CurrentState Read the string that want to be checked Checking process: Program structure for Table-based Implementation

9 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -9- each state corresponds to a case statement each edge corresponds to a goto statement for accept state, add one more branch, if current char is the end of the string then accept; Graph based Implementation of DFA i b j k a Li: case CurrentChar of a : goto Lj b : goto Lk other : Error( ) i Li: case CurrentChar of a : goto Lj b : goto Lk # : return true; other : Error( )

10 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -10- S0S0 a S1 S2 S*S* c d d a b c LS0: read character to CurrentChar; case CurrentChar of a: goto LS1; c: goto LS2: d: goto LS3; other: return false; LS1: read character to CurrentChar; case CurrentChar of b: goto LS1; d: goto LS2: other: return false;

11 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -11- S0S0 a S1 S2 S*S* c d d a b c LS2: read character to CurrentChar; case CurrentChar of a: goto LS3; other: return false; LS3: read character to CurrentChar; case CurrentChar of c: goto LS2: #: return true; other: return false;

12 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -12- Definition of NFA

13 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -13- Formal Definition – (  , SS, SS0, , TS ) –  ( alphabet ), set of allowed characters, each character can be called as input symbol; –SS = {S0, S1, S2, ……} , a finite set, each element is called state; –S 0  SS, set of start states –  : SS    power set of SS  {  }, transforming function –TS  SS, set of terminal (accept) states –Note :  is a function which accepts a state and a symbol and returns a set of states or  (no definition) ;

14 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -14- Differences between DFA & NFA DFANFA Start stateOne start stateSet of start states  T (S, a) S’ or ⊥ {S1, …, Sn} or ⊥ implementationeasyNon-deterministic

15 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -15- Example of NFA :: {a, b, c, d} SS: {S 0, S1 0, S2, S*} Set of Start state: {S 0, S1 0 } Set of terminal states: {S * } :: {(S 0,a)  {S1 0, S* },(S 0,  )  {S2}, (S1 0,b)  {S1 0 }, (S1 0,  )  {S2}, (S2,  )  {S *}, (S *, c)  {S * }} S0S0 a S1 0 S2 S*S*  a   b c

16 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -16- From NFA to DFA

17 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -17- Main Idea Solve two problems –  edge  -closure (SS)  闭包 –Merging those edges with the same symbol NextStates(SS, a) Conversion of NFA to DFA –Using a set of states in NFA as one state in DFA –Assuring accepting the same set of strings

18 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -18- The process to calculate  -closure (  闭包 ) For a given NFA A, and a set of states SS, –  -closure( SS ) = SS ; –If there exists a state s in SS, which has a  -edge referring to a state s’ and s’  -closure( SS ), add to s’ to  -closure( SS ) ; –Repeat until there is no state having  -edge to states that is not in  -closure( SS ) ;

19 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -19-  -closure (  闭包 ) -- Example S0S0 a S1 0 S2 S*S*  a   b c  -closure({S 0, S1 0 }) = ① {S 0, S1 0 } ② {S 0, S1 0, S2} ③ {S 0, S1 0, S2} ④ {S 0, S1 0, S2,S * }

20 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -20- Moving States For a given set of states SS and a symbol a in a NFA A, –NextStates( SS, a ) = {s | if there is a state s1  SS, and a edge s1 s in A } a

21 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -21- Moving States S0S0 a S1 0 S2 S*S*  a   b c NextStates({S 0, S1 0 }, a) = {S1 0, S * } NextStates({S 0, S1 0 }, b) = {S2}

22 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -22- Algorithm Given a NFA A = { , SS, SS 0, , TS} Generating an equivalent DFA A’ = { , SS’,S 0,  ’, TS’} Steps –(1) S 0 =  -closure(SS 0 ), add S 0 to SS’; –(2) select one state s from SS’, for any symbol a , let s’ = NextStates(  -closure(s), a), add (s, a)  s’ to  ’, if s’  SS’, add s’ to SS’; –(3) repeat (2) until all states are handled; –(4) for a state s in SS’, s = {S1,.., Sn}, if there exists Si  TS, then s is an accept state in A’, add s to TS’;

23 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -23- Example S0S0 a S1 0 S2 S*S*  a   b c  = {a, b, c}, S0 =  -closure({S 0, S1 0 }) = {S 0, S1 0, S2,S * }, abc {S 0, S1 0, S2,S * }{S1 0, S *,S2} {S*}{S*} {S*}{S*} {S*}{S*}{S*}{S*}

24 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -24- Minimizing DFA

25 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -25- Problem Equivalent of two DFAs –If the set of strings accepted by two DFAs are the same; Among those DFAs that accept the same set of strings, the minimal DFA refers to the one that has minimal number of states; How this happens?

26 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -26- Equivalent DFAs S0S0 a S1 S4 * b d S3 * S2 d S0S0 a S1 b d S*S* There are states that accepting the same set of strings! {ad, bd}

27 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -27- Main Idea Equivalent states( 等价状态 ) –For two states s1 and s2 in a DFA, if treat s1 and s2 as start states and they accept the same set of strings, s1 and s2 will be called equivalent states; Two ways to minimizing DFA –Merging equivalent states; ( 状态合并 ) –Splitting non-equivalent states; (状态分离)

28 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -28- Algorithm Given a DFA A = { , SS, S 0, , TS} Generating an equivalent DFA A’ = { , SS’,S 0 ’,  ’, TS’} Splitting Steps –(1) two groups {non-terminal states}, {terminal states}; –(2) select one group of states SS i = {Si1,…, Sin}, replace SS i with split(SS i ); –(3) repeat (2) until all groups are handled; –(4) SS’ = set of groups; –(5) S 0 ’ is the group consisting of S 0 ; –(6) if the group consisting of terminal states of A, it is terminal state of A’; –(7)  ’: SS i SS j, if there is Si Sj in A, Si  SS i, Sj  SS j aa

29 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -29- Splitting a Set of States Given –a NFA A = { , SS, S 0, , TS }; –Groups of states {SS1, …, SSm}, SS1  ……  SSm = SS; –SS i = {Si1,…, Sin}, split(SS i ) is to split SS i into two group G1 and G2, –For j = 1 to n for any a , If (Si1,a )  Sk  (Sij, a)  Sl  Sk and Sl belong to the same group SSp, add Sij to G1; Otherwise, add Sij to G2;

30 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -30- Simple Example S0S0 a S1 S4 * b d S3 * S2 d {S 0, S1, S2}, {S3 *, S4 * } {S 0 }, {S1, S2}, {S3 *, S4 * }

31 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -31- Assignment Define a DFA for accepting a set of binary numbers, each binary number can be divided by 4; ( 定义一个 DFA, 它所接受的符号串为能被 4 整除的 二进制数 ;) Implementation of the DFA above;

32 College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -32- Assignment From NFA to DFA, and minimize it; S0S0 a S1 S3 * S2 b a b a  


Download ppt "College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation."

Similar presentations


Ads by Google