def invalidDeclarationsTest(): srcs = ('(q, "a") -> ()', '() -> ({q, p})', '() -> (q, "a", R)', '("a", q) -> (q)', '(q, "a", L) -> (q)', '(q) -> ("a", q)', '(q) -> (q, "a", R, p)', '(q) -> (q, eps)') for src in srcs: try: parse(src) except ParserException as e: pass else: print src assert 0 == 1
def setDeclarationsTest(): src = '({q0, q1}, {"a0", "a1", "a2"}) -> ({q2}, {"b0", "b1", "b0"}, {L, N, R})' dec = parse(src)[0] assert len(dec['inStates']) == 2 assert len(dec['inSymbols']) == 3 assert len(dec['outStates']) == 1 assert len(dec['outSymbols']) == 2 assert len(dec['movement']) == 3
def assembleTM(src): tm = TuringMachine() for dec in parse(src): if len(dec['inStates']) == len(dec['inSymbols']) == 0: tm.setStartState(dec['outStates'].pop()) elif len(dec['outStates']) == len(dec['outSymbols']) == len( dec['movement']) == 0: tm.addAcceptStates(dec['inStates']) else: tm.addTransition(**dec) return tm
def exampleTest(): src = ''' # This is an example declaration ({q0, q1}, "a") -> (q1, "b", R) ''' declarations = parse(src) assert len(declarations) == 1 dec = declarations[0] assert dec['inStates'] == {IdentToken('q0'), IdentToken('q1')} assert dec['inSymbols'] == {StringToken('a')} assert dec['outStates'] == {IdentToken('q1')} assert dec['outSymbols'] == {StringToken('b')} assert dec['movement'] == {IdentToken('R')}
def shortenedDeclarationsTest(): src = ''' () -> (q) (q) -> () (q) -> (q, "a", N) ("a") -> (q, "a", N) (q, "a") -> (q, "a") (q, "a") -> (q, N) (q, "a") -> ("a", N) (q, "a") -> (q) (q, "a") -> ("a") (q, "a") -> (N) ''' declarations = parse(src) for dec in declarations: for key in ('inStates', 'inSymbols', 'outStates', 'outSymbols', 'movement'): assert dec.has_key(key)
def epsDeclarationTest(): src = '(q, eps) -> (q, _, N)' dec = parse(src)[0] assert dec['inSymbols'] == {IdentToken('eps')}
def blankDeclarationTest(): src = '(q, _) -> (q, _, N)' dec = parse(src)[0] assert dec['inSymbols'] == dec['outSymbols'] == {IdentToken('_')}