def test_parseRange(self): S = tokens('{1..23}')[0] actural = (('intRange', [1, 23]), True) expected =parseRange(S) self.assertEqual(expected,actural) S = tokens('{-1..2}')[0] actural = (('intRange', [('-1', [1]), 2]), True) expected =parseRange(S) self.assertEqual(expected,actural)
def validarXML(string): xmlContent = tokens(string) stack = Stack() bandera = False for elemento in xmlContent: if elemento == "\n": continue if not isXml(elemento): if not isOpenTag(elemento): if not isCloseTag(elemento): if not isOpenClose(elemento): if stack.is_empty(): return False elif not isOpenTag(stack.top()): return False else: elemento = tagName(elemento) if not stack.is_empty(): if elemento != tagName(stack.pop()): return False else: stack.push(elemento) else: bandera = True return bandera
def run(F=''): if not F == '': compile(F + '.led') #print(Compiler.Program) DefinedFuns = definedFuns(Program) if len(DefinedFuns) > 0: print('Defined functions: ', DefinedFuns) print() print('Enter an expression and hit [return] to get its value.') print('Hit [return] at the prompt to exit.') print() while True: e = input('> ') if e == '': return else: expression, eFlag = tokens(e) if eFlag: tree, tFlag = parseExpression(expression) if tFlag: print(prettyString(val(tree))) print() else: print('Cannot parse the tree.') else: print('Cannot tokenize the expression.')
def run(F=''): if not F=='': compile(F+'.led') #print(Compiler.Program) DefinedFuns = definedFuns(Program) if len(DefinedFuns)>0: print('Defined functions: ',DefinedFuns) print() print('Enter an expression and hit [return] to get its value.') print('Hit [return] at the prompt to exit.') print() while True: e = input('> ') if e=='': return else: expression,eFlag = tokens(e) if eFlag: tree, tFlag = parseExpression(expression) if tFlag: print(prettyString(val(tree))) print() else: print('Cannot parse the tree.') else: print('Cannot tokenize the expression.')
def test_tokens(): while True: e = input('Please input the strings to be tokenized: ') if e=='': return else: print('token is',tokens(e)[0])
def test_tokens(): while True: e = input('Please input the strings to be tokenized: ') if e == '': return else: print(tokens(e)[0])
def expressionValues(L): compile('test.led') values = [] for e in L: e = tokens(e)[0] v = val(parseExpression(e)[0]) values.append(v) return values
def expressionValues(L): compile('test.led') values = [] for e in L: e=tokens(e)[0] v = val(parseExpression(e)[0]) values.append(v) return values
def test_solutionSet(self): S = 'x in {1,2} U {3,4}' expected = [[('x', 1)], [('x', 2)], [('x', 3)], [('x', 4)]] t = tokens(S)[0] expression = parseExpression(t)[0] actural = solutionSet(expression) self.assertEqual(expected,actural) S = 'x = 5 & y=10 & z = 20' expected = [[('x', 5), ('y', 10), ('z', 20)]] t = tokens(S)[0] expression = parseExpression(t)[0] actural = solutionSet(expression) self.assertEqual(expected,actural) S = 'x in {2,3} & y in {10,20} & x*y < 40' expected = [[('x', 2), ('y', 10)], [('x', 3), ('y', 10)]] t = tokens(S)[0] expression = parseExpression(t)[0] actural = solutionSet(expression) self.assertEqual(expected,actural) S = 'x in {1..10} & x < 1' expected = [] t = tokens(S)[0] expression = parseExpression(t)[0] actural = solutionSet(expression) self.assertEqual(expected,actural) S = '(x,y,z) = (10,20,30) & x=z' expected = [] t = tokens(S)[0] expression = parseExpression(t)[0] actural = solutionSet(expression) self.assertEqual(expected,actural) S = 'x=2 & x in {3,4}' expected = [] t = tokens(S)[0] expression = parseExpression(t)[0] actural = solutionSet(expression) self.assertEqual(expected,actural) S = 'x in {1,2} & x in {2,3}' expected = [[('x', 2)]] t = tokens(S)[0] expression = parseExpression(t)[0] actural = solutionSet(expression) self.assertEqual(expected,actural) S = 'x=2 & x=3' expected = [] t = tokens(S)[0] expression = parseExpression(t)[0] actural = solutionSet(expression) self.assertEqual(expected,actural)
def test_tokens(self): L = ['1.2', '1.', '1..3', '1.2..2','.3(145..)','0.(3..)','3.96(721..)','.(4..)',\ '"hi mom"','"Go tell the Spartans\\rThou who passest by"','"John said \\"hello\\""'] expected = [ ['1.2'] , ['1.'], ['1','..', '3'], ['1.2', '..', '2'],['.3(145..)'],['0.(3..)'],['3.96(721..)'],['.(4..)'],\ ['"hi mom"'],['"Go tell the Spartans\\rThou who passest by"'],['"John said \\"hello\\""']] for i in range(len(L)): actural = tokens(L[i])[0] self.assertEqual(expected[i],actural)
def expressionPrettyValues(self,L): #compile('test.led') values = [] for e in L: e=tokens(e)[0] #v = val(parseExpression(e)[0]) tree = parseExpression(e)[0] v = val(tree.expression()) if isinstance(v,Fraction): v = numeralValue(v) #v=tree.val() values.append(prettyString(v)) return values
def test_evaluator(expression): expression,eFlag = tokens(expression) start_time = timeit.default_timer() tree, tFlag = parseExpression(expression) elapsed = timeit.default_timer() - start_time print("Parsing time",elapsed) if tFlag: start_time = timeit.default_timer() value = val(tree.expression()) elapsed = timeit.default_timer() - start_time print("Evaluating time",elapsed) if not value ==None: if isinstance(value,Fraction): value = numeralValue(value) print(prettyString(value))
def functionValues(F, FunN, ParamsL): compile(F + '.led') values = [] for Params in ParamsL: # construct the params for the expression paramsStr = '' for i in range(len(Params)): if not i == len(Params) - 1: paramsStr += str(Params[i]) + ',' else: paramsStr += str(Params[i]) # check for constant definition g = 12 if paramsStr == '': e = FunN else: e = FunN + '(' + paramsStr + ')' expression, eFlag = tokens(e) if eFlag: tree, tFlag = parseExpression(expression) if tFlag: values.append(val(tree)) return values
def functionValues(F,FunN,ParamsL): compile(F+'.led') values = [] for Params in ParamsL: # construct the params for the expression paramsStr = '' for i in range(len(Params)): if not i == len(Params)-1: paramsStr += str(Params[i])+',' else: paramsStr += str(Params[i]) # check for constant definition g = 12 if paramsStr=='': e = FunN else: e = FunN + '(' + paramsStr + ')' expression,eFlag = tokens(e) if eFlag: tree, tFlag = parseExpression(expression) if tFlag: values.append(val(tree)) return values
def test_tokens(self): L = ['1.2', '1.', '1..3', '1.2..2'] expected = [ ['1.2'] , ['1.'], ['1','..', '3'], ['1.2', '..', '2'] ] for i in range(len(L)): actural = tokens(L[i])[0] self.assertEqual(expected[i],actural)
''' This module is used for test individual modules in LED. Please use UnitTest.py if you would like to do the unit testing ''' from Compiler import compile from Tokenizer import tokens from Evaluater import val from Parser import * from GlobalVars import * from IDE import * ''' Test tokens(S) in Tokenizer.py ''' def test_tokens(): while True: e = input('Please input the strings to be tokenized: ') if e == '': return else: print(tokens(e)[0]) ''' ''' #run('test') #test_tokens() print(parseExpression(tokens('{1..23}')[0])) #run()