+ All Categories
Home > Documents > Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C...

Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C...

Date post: 24-Mar-2018
Category:
Upload: doanthien
View: 226 times
Download: 1 times
Share this document with a friend
30
Step-by-step Conversion of Regular Expressions to C Code On the regular expression: ((ab)|c) *
Transcript
Page 1: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Step-by-step Conversion of Regular Expressions to C Code

On the regular expression:

((a⋅ b)|c)*

Page 2: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

THOMPSON’S CONSTRUCTION

Convert the regular expression to an NFA.

Page 3: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Step 1: construct NFA for r1.

1 2

a r1:

r1

( (a ⋅ b) | c )*

Page 4: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Step 2: construct NFA for r2.

1 2

a r1: ( (a ⋅ b) | c )*

3 4

b r2:

r2 r1

Page 5: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Step 3: construct NFA for r3.

2 4

b 1

a r3:

( (a ⋅ b) | c )*

r3

Page 6: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Step 4: construct NFA for r4.

2 4

b 1

a

5 6

c

r3:

r4:

( (a ⋅ b) | c )*

r4 r3

Page 7: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Step 5: construct NFA for r5.

2 4

b 1

a

5 6 c

7

𝜀

𝜀

8

𝜀

𝜀

( (a ⋅ b) | c )*

r5 r5:

Page 8: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Step 6: construct NFA for r5*.

2 4

b 1

a

5 6 c

7

𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

Page 9: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

SUBSET CONSTRUCTION

Convert the NFA to a DFA.

Page 10: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Draw transition table for DFA

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

Dstates

Page 11: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Add 𝜀-closure(9) as DFA start state

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A

Dstates

Page 12: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Subset construction: algorithm

while (there is an unmarked state T in Dstates) { mark T; for (each input symbol a) {

U = 𝜀-closure(move(T, a)); Dtran[T, a] = U if (U is not in Dstates) add U as unmarked state to Dstates; } }

Page 13: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Mark state A

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A

Dstates

Page 14: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(A, a))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B

{2} B

Dstates

Page 15: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(A, b))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B -

{2} B

Dstates

Page 16: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(A, c))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B

{6,8,10,7,1,5} C

Dstates

Page 17: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Mark B

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B

{6,8,10,7,1,5} C

Dstates

Page 18: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(B, a))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B -

{6,8,10,7,1,5} C

Dstates

Page 19: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(B, b))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D

{6,8,10,7,1,5} C

{4,8,7,1,5,10} D

Dstates

Page 20: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(B, c))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C

{4,8,7,1,5,10} D

Dstates

Page 21: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Mark C

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C

{4,8,7,1,5,10} D

Dstates

Page 22: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(C, a))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C B

{4,8,7,1,5,10} D

Dstates

Page 23: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(C, b))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C B -

{4,8,7,1,5,10} D

Dstates

Page 24: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(C, c))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C B - C

{4,8,7,1,5,10} D

Dstates

Page 25: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Mark D

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C B - C

{4,8,7,1,5,10} D

Dstates

Page 26: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(D, a))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C B - C

{4,8,7,1,5,10} D B

Dstates

Page 27: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Compute 𝜀-closure(move(D, b))

2 4 b

1 a

5 6 c

7 𝜀

𝜀

8

𝜀

𝜀 9

𝜀

𝜀 𝜀

𝜀

10

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C B - C

{4,8,7,1,5,10} D B - C

Dstates

Page 28: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

Draw DFA

b B a

c

A a

c

a

C

D

c

NFA States DFA

State

Next State

a b c

{9,7,1,5,10} A B - C

{2} B - D -

{6,8,10,7,1,5} C B - C

{4,8,7,1,5,10} D B - C

Page 29: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

TRANSLATION TO C

Convert the DFA into C code.

Page 30: Step-by-step Conversion of Regular Expressions to C Code · PDF fileRegular Expressions to C Code On the regular expression: ((a⋅ b)|c)* THOMPSON’S CONSTRUCTION ... DFA State Next

int match(char* next) { goto A; A: if (*next == '\0') return 1; if (*next == 'a') { next++; goto B; } if (*next == 'c') { next++; goto C; } return 0; B: if (*next == '\0') return 0; if (*next == 'b') { next++; goto D; } return 0; C: if (*next == '\0') return 1; if (*next == 'a') { next++; goto B; } if (*next == 'c') { next++; goto C; } return 0; D: if (*next == '\0') return 1; if (*next == 'a') { next++; goto B; } if (*next == 'c') { next++; goto C; } return 0; }


Recommended