• No results found

Solving set equations

In document Basics of Compiler Design (Page 35-38)

2.6 Converting an NFA to a DFA

2.6.1 Solving set equations

The following is a very brief description of how to solve set equations like the above. If you find it confusing, you can read appendix A and in particular sec- tion A.4 first.

In general, a set equation over a single set-valued variableX has the form X =F(X)

whereF is a function from sets to sets. Not all such equations are solvable, so we will restrict ourselves to special cases, which we will describe below. We will use calculation of epsilon-closure as the driving example.

In definition 2.2, ε-closure(M) is the value we have to find, so we make an

equation such that the value ofX that solves the equation will beε-closure(M):

X=M∪ {t|s∈XandsεtT}

So, if we defineFMto be

FM(X) =M∪ {t|s∈Xandsεt∈T}

then a solution to the equationX=FM(X)will beε-closure(M).

FM has a property that is essential to our solution method: If X ⊆Y then FM(X)⊆FM(Y). We say thatFMismonotonic.

There may be several solutions to the equationX=FM(X). For example, if the NFA has a pair of states that connect to each other by epsilon transitions, adding this pair to a solution that does not already include the pair will create a new solution. The epsilon-closure ofMis theleastsolution to the equation (i.e., the smallestX that satistifes the equation).

When we have an equation of the formX =F(X)andFis monotonic, we can find the least solution to the equation in the following way: We first guess that the solution is the empty set and check to see if we are right: We compare0/withF(0/).

If these are equal, we are done and0/ is the solution. If not, we use the following

properties:

• The least solutionSto the equation satisfiesS=F(S). • 0/⊆Simplies thatF(0/)⊆F(S).

to conclude thatF(0/)⊆S. Hence,F(0/)is a new guess atS. We now form the chain /

0⊆F(0/)⊆F(F(0/))⊆. . .

If at any point an element in the sequence is identical to the previous, we have a fixed-point,i.e., a setSsuch thatS=F(S). This fixed-point of the sequence will be the least (in terms of set inclusion) solution to the equation. This is not difficult to verify, but we will omit the details. Since we are iterating a function until we reach a fixed-point, we call this processfixed-point iteration.

If we are working with sets over a finite domain (e.g., sets of NFA states), wewilleventually reach a fixed-point, as there can be no infinite chain of strictly increasing sets.

2.6. CONVERTING AN NFA TO A DFA 25 We can use this method for calculating the epsilon-closure of the set{1}with respect to the NFA shown in figure 2.5. Since we want to find ε-closure({1}),

M={1}, soFM=F{1}. We start by guessing the empty set:

F{1}(0/) = {1} ∪ {t|s∈0/andsεt∈T} = {1} As0/6={1}, we continue. F{1}({1}) = {1} ∪ {t|s∈ {1}andsεt∈T} = {1} ∪ {2,5} = {1,2,5} F{1}({1,2,5}) = {1} ∪ {t|s∈ {1,2,5}andsεt∈T} = {1} ∪ {2,5,6,7} = {1,2,5,6,7} F{1}({1,2,5,6,7}) = {1} ∪ {t|s∈ {1,2,5,6,7}andsεt∈T} = {1} ∪ {2,5,6,7} = {1,2,5,6,7}

We have now reached a fixed-point and found our solution. Hence, we conclude thatε-closure({1}) ={1,2,5,6,7}.

We have done a good deal of repeated calculation in the iteration above: We have calculated the epsilon-transitions from state 1 three times and those from state 2 and 5 twice each. We can make an optimised fixed-point iteration by exploiting that the function is not only monotonic, but alsodistributive:F(X∪Y) =F(X)∪ F(Y). This means that, when we during the iteration add elements to our set, we in the next iteration need only calculateF for the new elements and add the result to the set. In the example above, we get

F{1}(0/) = {1} ∪ {t|s∈0/ andsεt∈T} = {1} F{1}({1}) = {1} ∪ {t|s∈ {1}andsεt∈T} = {1} ∪ {2,5} = {1,2,5} F{1}({1,2,5}) = F({1})∪F({2,5}) = {1,2,5} ∪({1} ∪ {t|s∈ {2,5}andsεtT}) = {1,2,5} ∪({1} ∪ {6,7}) = {1,2,5,6,7} F{1}({1,2,5,6,7}) = F({1,2,5})∪F{1}({6,7}) = {1,2,5,6,7} ∪({1} ∪ {t|s∈ {6,7}andsεtT}) = {1,2,5,6,7} ∪({1} ∪0/) = {1,2,5,6,7}

We can use this principle to formulate a work-list algorithmfor finding the least fixed-point for an equation over a distributive functionF. The idea is that we step- by-step build a set that eventually becomes our solution. In the first step we calcu- lateF(0/). The elements in this initial set areunmarked. In each subsequent step,

we take an unmarked elementxfrom the set, mark it and addF({x})(unmarked) to the set. Note that if an element already occurs in the set (marked or not), it is not added again. When, eventually, all elements in the set are marked, we are done.

This is perhaps best illustrated by an example (the same as before). We start by calculatingF{1}(0/) ={1}. The element 1 is unmarked, so we pick this, mark it and

calculateF{1}({1})and add the new elements 2 and 5 to the set. As we continue, we get this sequence of sets:

{1} { √ 1,2,5} { √ 1, √ 2,5} { √ 1, √ 2, √ 5,6,7} { √ 1, √ 2, √ 5, √ 6,7} { √ 1, √ 2, √ 5, √ 6, √ 7}

We will later also need to solvesimultaneous equationsover sets,i.e., several equa- tions over several sets. These can also be solved by fixed-point iteration in the same way as single equations, though the work-list version of the algorithm becomes a bit more complicated.

In document Basics of Compiler Design (Page 35-38)