def testRegxConstructor1(self):
        checkFileName = 'testRegxConstructor1' + '.good'
        outFileName = 'testRegxConstructor1' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                print(f"NFA for regular expression '{a.val}' is")
                print(a.nfa)

                b = regx('b')
                print(f"NFA for regular expression '{b.val}' is")
                print(b.nfa)

                e = regx('#')  # represents the empty string
                print(
                    f"NFA for regular expression '{e.val}' (empty string) is")
                print(e.nfa)

                n = regx('@')  # represents the empty set
                print(f"NFA for regular expression '{n.val}' (empty set) is")
                print(n.nfa)

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
    def testRegxEquiv2(self):
        checkFileName = 'testRegxEquiv2' + '.good'
        outFileName = 'testRegxEquiv2' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                # r1 is the regx of {w in {0,1}*: w has no substring 111}
                r1 = regx('0* U 0*(1U11)(00*(1U11))*0*')
                r2 = regx('(#U1U11)(0U01U011)*')
                r3 = regx('(0U10U110)*(#U1U11)')
                print(r1, '==', r2, 'is', r1 == r2)
                print(r1, '==', r3, 'is', r1 == r3)
                print(r2, '==', r3, 'is', r2 == r3)

                r4 = regx('(1U11)(0U01U011)*')
                print(r1, '==', r4, 'is', r1 == r4)
                print(r2, '==', r4, 'is', r2 == r4)
                print(r3, '==', r4, 'is', r3 == r4)

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 3
0
    def testPrimRegxGen1(self):
        checkFileName = 'testPrimRegxGen1' + '.good'
        outFileName = 'testPrimRegxGen1' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                for s in ['a', 'b', '1', '']:
                    print(f"regx {a.val} : string '{s}' -> {a.gen(s)}")
                print()

                b = regx('b')
                for s in ['a', 'b', '1', '']:
                    print(f"regx {b.val} : string '{s}' -> {b.gen(s)}")
                print()

                e = regx('#')  # represents the empty string
                for s in ['a', 'b', '1', '']:
                    print(f"regx {e.val} : string '{s}' -> {e.gen(s)}")
                print()

                n = regx('@')  # represents the empty set
                for s in ['a', 'b', '1', '']:
                    print(f"regx {n.val} : string '{s}' -> {n.gen(s)}")
                print()
        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 4
0
    def testRegxMix33(self):
        checkFileName = 'testRegxMix33' + '.good'
        outFileName = 'testRegxMix33' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                x = regx('aU(bc)*')
                for s in ['a', '', 'bc', 'bcbc', 'abc', 'bcc', 'ac']:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

                x = regx('(aUb)*c')
                for s in ['', 'c', 'ac', 'bc', 'abbac', 'aab', 'cab']:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

                x = regx('(aUbc)*')
                for s in [
                        '', 'a', 'bc', 'bcbc', 'aaa', 'bcabcaa', 'aabc', 'cb',
                        'acbc'
                ]:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()
        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 5
0
    def testRegxOp1(self):
        checkFileName = 'testRegxOp1' + '.good'
        outFileName = 'testRegxOp1' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                print(f"NFA for regular expression '{a.val}' is")
                print(a.nfa)
                b = regx('b')
                print(f"NFA for regular expression '{b.val}' is")
                print(b.nfa)
                aUb = a | b
                print(f"NFA for regular expression '{aUb.val}' is")
                print(aUb.nfa)

                for s in ['a', 'b', '1', 'ba', '12a', '']:
                    print(f"regx {aUb.val} : string '{s}' -> {aUb > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 6
0
    def testRegxMix2(self):
        checkFileName = 'testRegxMix2' + '.good'
        outFileName = 'testRegxMix2' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                b = regx('b')
                c = regx('c')

                aUbc = a | b & c
                print(
                    f"NFA for regular expression 'a|b&c' with value '{aUbc.val}' is"
                )
                for s in ['a', 'b', 'c', 'bc', 'ab', 'ac', '']:
                    print(f"regx {aUbc.val} : string '{s}' -> {aUbc > s}")
                print()

                aUb_c = (a | b) & c
                print(
                    f"NFA for regular expression '(a|b)&c' with value '{aUb_c.val}' is"
                )
                for s in ['a', 'b', 'c', 'bc', 'ab', 'ac', '']:
                    print(f"regx {aUb_c.val} : string '{s}' -> {aUb_c > s}")
                print()

                aUbSTARc = a | b.star & c
                print(
                    f"NFA for regular expression 'a|b.star&c' with value '{aUbSTARc.val}' is"
                )
                for s in ['a', 'b', 'c', 'bc', 'bbc', 'ac', '']:
                    print(
                        f"regx {aUbSTARc.val} : string '{s}' -> {aUbSTARc > s}"
                    )
                print()

                x = a & (b & c).star
                print(
                    f"NFA for regular expression 'a&(b&c).star' with value '{x.val}' is"
                )
                for s in ['a', 'b', 'c', 'abc', 'abcbcbc', 'bc', 'acbbc', '']:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
    def testRegxEquiv1(self):
        checkFileName = 'testRegxEquiv1' + '.good'
        outFileName = 'testRegxEquiv1' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                r1 = regx('c*(aUbc*)*')
                print(f'r1 is {r1}')

                del11 = {
                    (1, 'a'): 2,
                    (1, 'b'): 1,
                    (1, 'c'): 1,
                    (2, 'a'): 2,
                    (2, 'b'): 1,
                    (2, 'c'): 3,
                    (3, 'a'): 3,
                    (3, 'b'): 3,
                    (3, 'c'): 3
                }
                d11 = dfa(delta=del11, start=1, finals={1, 2})
                print(f'DFA d11 is')
                print(40 * '-')
                print(d11)
                print(40 * '-')
                r11 = d11.to_regx()
                print(f'r11 is {r11}')
                print(f'r1.equiv(r11) is {r1.equiv(r11)}')
                print(r1, '==', r11, 'is', r1 == r11)
                print(40 * '-')
                r2 = regx('(#Ucc*)(aUb(c*cU#))*')
                print(f'r2 is {r2}')
                print(f'r1.equiv(r2) is {r1.equiv(r2)}')
                print(r1, '==', r2, 'is', r1 == r2)
                print(f'r11.equiv(r2) is {r11.equiv(r2)}')
                print(r11, '==', r2, 'is', r11 == r2)
                print(40 * '-')
                r3 = regx('(#Ucc*)(aUbc*c)*')
                print(f'r3 is {r3}')
                print(f'r1.equiv(r3) is {r1.equiv(r3)}')
                print(r1, '==', r3, 'is', r1 == r3)
                print(f'r11.equiv(r3) is {r11.equiv(r3)}')
                print(r11, '==', r3, 'is', r11 == r3)
        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 8
0
    def testRegxMix3(self):
        checkFileName = 'testRegxMix3' + '.good'
        outFileName = 'testRegxMix3' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                b = regx('b')
                c = regx('c')

                aU_bc_STAR = a | (b & c).star
                print(
                    f"NFA for regular expression 'a|(b&c).star' with value '{aU_bc_STAR.val}' is"
                )
                for s in ['a', '', 'bc', 'bcbc', 'abc', 'bcc', 'ac']:
                    print(
                        f"regx {aU_bc_STAR.val} : string '{s}' -> {aU_bc_STAR > s}"
                    )
                print()

                _aUb_STARc = (a | b).star & c
                print(
                    f"NFA for regular expression '(a|b).star&c' with value '{_aUb_STARc.val}' is"
                )
                for s in ['', 'c', 'ac', 'bc', 'abbac', 'aab', 'cab']:
                    print(
                        f"regx {_aUb_STARc.val} : string '{s}' -> {_aUb_STARc > s}"
                    )
                print()

                aUbc_STAR = (a | b & c).star
                print(
                    f"NFA for regular expression '(a|b&c).star' with value '{aUbc_STAR.val}' is"
                )
                for s in [
                        '', 'a', 'bc', 'bcbc', 'aaa', 'bcabcaa', 'aabc', 'cb',
                        'acbc'
                ]:
                    print(
                        f"regx {aUbc_STAR.val} : string '{s}' -> {aUbc_STAR > s}"
                    )
                print()
        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
    def testRegxToNfa1(self):
        checkFileName = 'testRegxToNfa1' + '.good'
        outFileName = 'testRegxToNfa1' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):
                
                x = regx('aU(bc)*')
                n1 = x.nfa
                print(f'Attached NFA is')
                print(n1)
                n2 = x.to_nfa()
                print(f'Obtained NFA is')
                print(x.to_nfa())
                if id(n1) == id(n2):
                    print('Two NFAs are the same object')
                else:
                    print('Two NFAs are different objects')
                
        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 10
0
    def testRegxStar1(self):
        checkFileName = 'testRegxStar1' + '.good'
        outFileName = 'testRegxStar1' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                print(f"NFA for regular expression '{a.val}' is")
                print(a.nfa)

                astar = a.star
                print(
                    f"NFA for regular expression 'a.star' with value '{astar.val}' is"
                )
                print(astar.nfa)

                for s in ['', 'a', 'aa', 'aaa', 'b', 'ab', 'aaa3']:
                    print(f"regx {astar.val} : string '{s}' -> {astar > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 11
0
    def testRegxOp3(self):
        checkFileName = 'testRegxOp3' + '.good'
        outFileName = 'testRegxOp3' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                print(f"NFA for regular expression '{a.val}' is")
                print(a.nfa)
                n = regx('@')  # empty set
                print(f"NFA for regular expression '{n.val}' is")
                print(n.nfa)

                aUn = a | n
                print(
                    f"NFA for regular expression 'aU@' with value '{aUn.val}' is"
                )
                print(aUn.nfa)
                for s in ['a', 'b', 'aa', '12a', '']:
                    print(f"regx {aUn.val} : string '{s}' -> {aUn > s}")
                print()

                nUa = n | a
                print(
                    f"NFA for regular expression '@Ua' with value '{nUa.val}' is"
                )
                print(nUa.nfa)
                for s in ['a', 'b', 'aa', '12a', '']:
                    print(f"regx {nUa.val} : string '{s}' -> {nUa > s}")
                print()

                nUn = n | n
                print(
                    f"NFA for regular expression '@U@' with value {nUn.val}' is"
                )
                print(nUn.nfa)
                for s in ['a', 'b', 'aa', '12a', '']:
                    print(f"regx {nUn.val} : string '{s}' -> {nUn > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 12
0
    def testRegxMix1(self):
        checkFileName = 'testRegxMix1' + '.good'
        outFileName = 'testRegxMix1' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                b = regx('b')
                c = regx('c')

                aUbUc = a | b | c
                print(
                    f"NFA for regular expression 'a|b|c' with value '{aUbUc.val}' is"
                )
                print(aUbUc.nfa)
                for s in ['a', 'b', 'c', '1', 'ab', 'cbaa', '']:
                    print(f"regx {aUbUc.val} : string '{s}' -> {aUbUc > s}")
                print()

                abc = a & b & c
                print(
                    f"NFA for regular expression 'a&b&c' with value '{abc.val}' is"
                )
                print(abc.nfa)
                for s in [
                        'a', 'b', 'c', 'ab', 'bc', 'abc', 'abcd', 'a12b', ''
                ]:
                    print(f"regx {abc.val} : string '{s}' -> {abc > s}")
                print()

                ass = a.star.star
                print(
                    f"NFA for regular expression 'a.star.star' with value '{ass.val}' is"
                )
                print(ass.nfa)
                for s in ['', 'a', 'aa', 'aaa', 'aaaa', 'b', 'aaab', 'a12b']:
                    print(f"regx {ass.val} : string '{s}' -> {ass > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 13
0
    def testRegxOp22(self):
        checkFileName = 'testRegxOp22' + '.good'
        outFileName = 'testRegxOp22' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                print(f"NFA for regular expression '{a.val}' is")
                print(a.nfa)
                e = regx('#')  # represents the empty string
                print(f"NFA for regular expression '{e.val}' is")
                print(e.nfa)

                ae = a & e
                print(
                    f"NFA for regular expression a# with value '{ae.val}' is")
                print(ae.nfa)
                for s in ['a', 'b', 'aa', '12a', '']:
                    print(f"regx {ae.val} : string '{s}' -> {ae > s}")
                print()

                ea = e & a
                print(
                    f"NFA for regular expression #a with value '{ea.val}' is")
                print(ea.nfa)
                for s in ['a', 'b', 'aa', '12a', '']:
                    print(f"regx {ea.val} : string '{s}' -> {ea > s}")
                print()

                ee = e & e
                print(
                    f"NFA for regular expression ## with value '{ee.val}' is")
                print(ee.nfa)
                for s in ['a', 'b', 'aa', '12a', '']:
                    print(f"regx {ee.val} : string '{s}' -> {ee > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 14
0
    def testRegxMix44(self):
        checkFileName = 'testRegxMix44' + '.good'
        outFileName = 'testRegxMix44' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                x = regx('c*(aUbc*)*')
                for s in ['', 'a', 'b', 'ccc', 'abcccaabc', 'ac', 'babcacc']:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

                x = regx('(#U1U11)(0U01U011)*')
                for s in [
                        '', '0', '1', '11', '01101', '11010001101', '111',
                        '001110', '0110001011101'
                ]:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

                x = regx('b* U (b*ab*ab*ab*)*')  # no.of a's divisible by 3
                for s in [
                        '', 'ab', 'baabbab', 'bbaa', 'aaa', 'aababbbaaab',
                        'aaaabaaaaa', 'babaabaa'
                ]:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

                x = regx('0* U 0*(1U11)(00*(1U11))*0*')
                for s in [
                        '', '0', '1', '11', '01101', '11010001101', '111',
                        '001110', '0110001011101'
                ]:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 15
0
    def testRegxMix11(self):
        checkFileName = 'testRegxMix11' + '.good'
        outFileName = 'testRegxMix11' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                x = regx('aUbUc')
                print(
                    f"NFA for regular expression 'aUbUc' with value '{x.val}' is"
                )
                print(x.nfa)
                for s in ['a', 'b', 'c', '1', 'ab', 'cbaa', '']:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

                x = regx('abc')
                print(
                    f"NFA for regular expression 'abc' with value '{x.val}' is"
                )
                print(x.nfa)
                for s in [
                        'a', 'b', 'c', 'ab', 'bc', 'abc', 'abcd', 'a12b', ''
                ]:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

                x = regx('a**')
                print(
                    f"NFA for regular expression 'a**' with value '{x.val}' is"
                )
                print(x.nfa)
                for s in ['', 'a', 'aa', 'aaa', 'aaaa', 'b', 'aaab', 'a12b']:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 16
0
    def testRegxOp44(self):
        checkFileName = 'testRegxOp44' + '.good'
        outFileName = 'testRegxOp44' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                e = regx('#')  # empty string
                print(f"NFA for regular expression '{e.val}' is")
                print(e.nfa)
                n = regx('@')  # empty set
                print(f"NFA for regular expression '{n.val}' is")
                print(n.nfa)

                en = e & n
                print(
                    f"NFA for regular expression '#@' with value '{en.val}' is"
                )
                print(en.nfa)
                for s in ['a', 'b', 'aa', '12a', '']:
                    print(f"regx {en.val} : string '{s}' -> {en > s}")
                print()

                ne = n & e
                print(
                    f"NFA for regular expression '@#' with value '{ne.val}' is"
                )
                print(ne.nfa)
                for s in ['a', 'b', 'aa', '12a', '']:
                    print(f"regx {ne.val} : string '{s}' -> {ne > s}")
                print()

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 17
0
    def testRegxToDfa4(self):
        checkFileName = 'testRegxToDfa4' + '.good'
        outFileName = 'testRegxToDfa4' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                rstr = '@'
                r = regx(rstr)
                m = r.to_dfa()
                print(f'Regular expression is {rstr}')
                print(f'Resulting DFA is')
                print(m)

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 18
0
    def testRegxEquiv3(self):
        checkFileName = 'testRegxEquiv3' + '.good'
        outFileName = 'testRegxEquiv3' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                # r1 is the regx of {w in {0,1}*: w ends with 10}
                r1 = regx('(0U1)*10')
                r2 = regx('@*(0U1)*10')
                r3 = regx('@U(0U1)*10')
                r4 = regx('0*(10*)*10')
                r5 = regx('1*(01*)*10')
                print(r1, '==', r2, 'is', r1 == r2)
                print(r1, '==', r3, 'is', r1 == r3)
                print(r1, '==', r4, 'is', r1 == r4)
                print(r1, '==', r5, 'is', r1 == r5)

                dfa1delta = {  # this dfa accepts the same language as r1's
                    (1, '0'): 1,
                    (1, '1'): 2,
                    (2, '0'): 3,
                    (2, '1'): 2,
                    (3, '0'): 1,
                    (3, '1'): 2
                }
                dfa1 = dfa(delta=dfa1delta, start=1, finals={3})
                print('dfa1 is')
                print(dfa1)
                print(40 * '-')
                r6 = dfa1.to_regx()
                print(f'The above dfa accepts the language with regx {r6}')
                print(40 * '-')
                print(r1, '==', r6, 'is', r1 == r6)
                print(40 * '-')

                r7 = regx('(0U01U011)*')
                print(r1, '==', r7, 'is', r1 == r7)

        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
Esempio n. 19
0
from reglang.regx import regx

rstr = '@'
r = regx(rstr)
m = r.to_dfa()
print(f'Regular expression is {rstr}')
print(f'Resulting DFA is')
print(m)

from reglang.regx import regx

e = regx('#') # empty string
print(f"NFA for regular expression '{e.val}' is")
print(e.nfa)
n = regx('@') # empty set
print(f"NFA for regular expression '{n.val}' is")
print(n.nfa)

eUn = e|n
print(f"NFA for regular expression '#U@' with value '{eUn.val}' is")
print(eUn.nfa)
for s in ['a', 'b', 'aa', '12a', '']:
    print(f"regx {eUn.val} : string '{s}' -> {eUn > s}")
print()

nUe = n|e
print(f"NFA for regular expression '@U#' with value '{nUe.val}' is")
print(nUe.nfa)
for s in ['a', 'b', 'aa', '12a', '']:
    print(f"regx {nUe.val} : string '{s}' -> {nUe > s}")
print()

from reglang.regx import regx

a = regx('a')
print(f"NFA for regular expression '{a.val}' is")
print(a.nfa)
e = regx('#')  # represents the empty string
print(f"NFA for regular expression '{e.val}' is")
print(e.nfa)

aUe = a | e
print(f"NFA for regular expression '{aUe.val}' is")
print(aUe.nfa)
for s in ['a', 'b', 'aa', '12a', '']:
    print(f"regx {aUe.val} : string '{s}' -> {aUe > s}")
print()

eUa = e | a
print(f"NFA for regular expression '{eUa.val}' is")
print(eUa.nfa)
for s in ['a', 'b', 'aa', '12a', '']:
    print(f"regx {eUa.val} : string '{s}' -> {eUa > s}")
print()

eUe = e | e
print(f"NFA for regular expression '{eUe.val}' is")
print(eUe.nfa)
for s in ['a', 'b', 'aa', '12a', '']:
    print(f"regx {eUe.val} : string '{s}' -> {eUe > s}")
print()
from reglang.regx import regx

x = regx('aU(bc)*')
for s in ['a', '', 'bc', 'bcbc', 'abc', 'bcc', 'ac']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()

x = regx('(aUb)*c')
for s in ['', 'c', 'ac', 'bc', 'abbac', 'aab', 'cab']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()

x = regx('(aUbc)*')
for s in ['', 'a', 'bc', 'bcbc', 'aaa', 'bcabcaa', 'aabc', 'cb', 'acbc']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()
Esempio n. 23
0
    def testRegxMix4(self):
        checkFileName = 'testRegxMix4' + '.good'
        outFileName = 'testRegxMix4' + '.out'

        with open(outFileName, 'w') as outf:
            with redirect_stdout(outf):

                a = regx('a')
                b = regx('b')
                c = regx('c')
                e = regx('#')
                i = regx('1')
                o = regx('0')

                no_ac = c.star & (a | b & c.star).star
                print(
                    f"NFA for regular expression 'c*&(a|b&c*)*' with value '{no_ac.val}' is"
                )
                for s in ['', 'a', 'b', 'ccc', 'abcccaabc', 'ac', 'babcacc']:
                    print(f"regx {no_ac.val} : string '{s}' -> {no_ac > s}")
                print()

                no111 = (e | i | i & i) & (o | o & i | o & i & i).star
                print(
                    f"NFA for regular expression '(#|1|11)(0|01|011)*' with value '{no111.val}' is"
                )
                for s in [
                        '', '0', '1', '11', '01101', '11010001101', '111',
                        '001110', '0110001011101'
                ]:
                    print(f"regx {no111.val} : string '{s}' -> {no111 > s}")
                print()

                x = b.star | (b.star & a & b.star & a & b.star & a
                              & b.star).star  # no.of a's divisible by 3
                print(
                    f"NFA for regular expression 'b*|(b*ab*ab*ab*)*' with value '{x.val}' is"
                )
                for s in [
                        '', 'ab', 'baabbab', 'bbaa', 'aaa', 'aababbbaaab',
                        'aaaabaaaaa', 'babaabaa'
                ]:
                    print(f"regx {x.val} : string '{s}' -> {x > s}")
                print()

                no111 = o.star | o.star & (i | i & i) & (
                    o & o.star & (i | i & i)).star & o.star
                print(
                    f"NFA for regular expression '0*U0*(1U11)(00*(1U11))*0*' with value '{no111.val}' is"
                )
                for s in [
                        '', '0', '1', '11', '01101', '11010001101', '111',
                        '001110', '0110001011101'
                ]:
                    print(f"regx {no111.val} : string '{s}' -> {no111 > s}")
                print()
        with open(outFileName) as outf:
            outStr = outf.read()
        with open(checkFileName) as f:
            checkStr = f.read()

        self.assertEqual(outStr, checkStr)
from reglang.regx import regx

x = regx('aUbUc')
print(f"NFA for regular expression 'aUbUc' with value '{x.val}' is")
print(x.nfa)
for s in ['a', 'b', 'c', '1', 'ab', 'cbaa', '']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()

x = regx('abc')
print(f"NFA for regular expression 'abc' with value '{x.val}' is")
print(x.nfa)
for s in ['a', 'b', 'c', 'ab', 'bc', 'abc', 'abcd', 'a12b', '']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()

x = regx('a**')
print(f"NFA for regular expression 'a**' with value '{x.val}' is")
print(x.nfa)
for s in ['', 'a', 'aa', 'aaa', 'aaaa', 'b', 'aaab', 'a12b']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()

Esempio n. 25
0
from reglang.regx import regx

# r1 is the regx of {w in {0,1}*: w has no substring 111}
r1 = regx('0* U 0*(1U11)(00*(1U11))*0*')
r2 = regx('(#U1U11)(0U01U011)*')
r3 = regx('(0U10U110)*(#U1U11)')
print(r1, '==', r2, 'is', r1 == r2)
print(r1, '==', r3, 'is', r1 == r3)
print(r2, '==', r3, 'is', r2 == r3)

r4 = regx('(1U11)(0U01U011)*')
print(r1, '==', r4, 'is', r1 == r4)
print(r2, '==', r4, 'is', r2 == r4)
print(r3, '==', r4, 'is', r3 == r4)

from reglang.regx import regx

a = regx('a')
print(f"NFA for regular expression '{a.val}' is")
print(a.nfa)
b = regx('b')
print(f"NFA for regular expression '{b.val}' is")
print(b.nfa)
aUb = a | b
print(f"NFA for regular expression '{aUb.val}' is")
print(aUb.nfa)

for s in ['a', 'b', '1', 'ba', '12a', '']:
    print(f"regx {aUb.val} : string '{s}' -> {aUb > s}")
print()
from reglang.regx import regx

a = regx('a')
print(f"NFA for regular expression '{a.val}' is")
print(a.nfa)
n = regx('@')  # empty set
print(f"NFA for regular expression '{n.val}' is")
print(n.nfa)

aUn = a | n
print(f"NFA for regular expression 'aU@' with value '{aUn.val}' is")
print(aUn.nfa)
for s in ['a', 'b', 'aa', '12a', '']:
    print(f"regx {aUn.val} : string '{s}' -> {aUn > s}")
print()

nUa = n | a
print(f"NFA for regular expression '@Ua' with value '{nUa.val}' is")
print(nUa.nfa)
for s in ['a', 'b', 'aa', '12a', '']:
    print(f"regx {nUa.val} : string '{s}' -> {nUa > s}")
print()

nUn = n | n
print(f"NFA for regular expression '@U@' with value {nUn.val}' is")
print(nUn.nfa)
for s in ['a', 'b', 'aa', '12a', '']:
    print(f"regx {nUn.val} : string '{s}' -> {nUn > s}")
print()
from reglang.regx import regx

a = regx('a')
for s in ['a', 'b', '1', '']:
    print(f"regx {a.val} : string '{s}' -> {a.gen(s)}")
print()

b = regx('b')
for s in ['a', 'b', '1', '']:
    print(f"regx {b.val} : string '{s}' -> {b.gen(s)}")
print()

e = regx('#')  # represents the empty string
for s in ['a', 'b', '1', '']:
    print(f"regx {e.val} : string '{s}' -> {e.gen(s)}")
print()

n = regx('@')  # represents the empty set
for s in ['a', 'b', '1', '']:
    print(f"regx {n.val} : string '{s}' -> {n.gen(s)}")
print()
Esempio n. 29
0
from reglang.regx import regx

a = regx('a')
b = regx('b')
c = regx('c')

aUbUc = a | b | c
print(f"NFA for regular expression 'a|b|c' with value '{aUbUc.val}' is")
print(aUbUc.nfa)
for s in ['a', 'b', 'c', '1', 'ab', 'cbaa', '']:
    print(f"regx {aUbUc.val} : string '{s}' -> {aUbUc > s}")
print()

abc = a & b & c
print(f"NFA for regular expression 'a&b&c' with value '{abc.val}' is")
print(abc.nfa)
for s in ['a', 'b', 'c', 'ab', 'bc', 'abc', 'abcd', 'a12b', '']:
    print(f"regx {abc.val} : string '{s}' -> {abc > s}")
print()

ass = a.star.star
print(f"NFA for regular expression 'a.star.star' with value '{ass.val}' is")
print(ass.nfa)
for s in ['', 'a', 'aa', 'aaa', 'aaaa', 'b', 'aaab', 'a12b']:
    print(f"regx {ass.val} : string '{s}' -> {ass > s}")
print()
Esempio n. 30
0
from reglang.regx import regx

x = regx('aUbc')
for s in ['a', 'b', 'c', 'bc', 'ab', 'ac', '']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()

x = regx('(aUb)c')
for s in ['a', 'b', 'c', 'bc', 'ab', 'ac', '']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()

x = regx('aUb*c')
for s in ['a', 'b', 'c', 'bc', 'bbc', 'ac', '']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()

x = regx('a(bc)*')
for s in ['a', 'b', 'c', 'abc', 'abcbcbc', 'bc', 'acbbc', '']:
    print(f"regx {x.val} : string '{s}' -> {x > s}")
print()