Calculate if two infinite regex solution sets don't intersect

前端 未结 2 1553
轻奢々
轻奢々 2020-12-30 08:14

In calculate if two arbitrary regular expressions have any overlapping solutions (assuming it\'s possible).

For example these two regular expressions can be shown to

2条回答
  •  無奈伤痛
    2020-12-30 08:38

    It is not in the domain of the halting problem; deciding whether the intersection of regular languages is empty or not can be solved as follows:

    1. Construct a DFA M1 for the first language.
    2. Construct a DFA M2 for the second language. Hint: Kleene's Theorem and Power Set machine construction
    3. Construct a DFA M3 for M1 intersect M2. Hint: Cartesian Product Machine construction
    4. Determine whether L(M3) is empty. Hint: If M3 has n states, and M3 doesn't accept any strings of length no greater than n, then L(M3) is empty... why?

    Each of those things can be algorithmically done and/or checked. Also, naturally, once you have a DFA recognizing the intersection of your languages, you can construct a regex to match the language. And if you start out with a regex, you can make a DFA. This is definitely computable.

    EDIT:

    So to build a Cartesian Product Machine, you need two DFAs. Let M1 = (E, q0, Q1, A1, f1) and M2 = (E, q0', Q2, A2, f2). In both cases, E is the input alphabet, q0 is the start state, Q is the set of all states, A is the set of accepting states, and f is the transition function. Construct M3 where...

    1. E3 = E
    2. Q3 = Q1 x Q2 (ordered pairs)
    3. q0'' = (q0, q0')
    4. A3 = {(x, y) | x in A1 and y in A2}
    5. f3(s, (x, y)) = (f1(s, x), f2(s, y))

    Provided I didn't make any mistakes, L(M3) = L(M1) intersect L(M2). Neat, huh?

提交回复
热议问题