class Traverse(unittest.TestCase): def setUp(self): self.lex = SvelLexer() self.lex.build() self.lexer = self.lex.get_lexer() self.parser = svelYacc.getParser() def test_hello(self): ast = self.parser.parse(Testsuite.hello_world, lexer=self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_comments(self): ast = self.parser.parse(Testsuite.comments, lexer =self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_empty(self): ast = self.parser.parse(Testsuite.empty, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_assignments(self): ast = self.parser.parse(Testsuite.assignments, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_boolean(self): ast = self.parser.parse(Testsuite.expressions_boolean, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code
class Traverse(unittest.TestCase): def setUp(self): self.lex = SvelLexer() self.lex.build() self.lexer = self.lex.get_lexer() self.parser = svelYacc.getParser() def test_hello(self): ast = self.parser.parse(Testsuite.hello_world, lexer=self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_comments(self): ast = self.parser.parse(Testsuite.comments, lexer =self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_empty(self): ast = self.parser.parse(Testsuite.empty, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_assignments(self): ast = self.parser.parse(Testsuite.assignments, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_boolean(self): ast = self.parser.parse(Testsuite.expressions_boolean, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_relational(self): ast = self.parser.parse(Testsuite.expressions_relational, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_math(self): ast = self.parser.parse(Testsuite.expressions_math, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_while(self): ast = self.parser.parse(Testsuite.loops_while, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_for(self): ast = self.parser.parse(Testsuite.loops_for, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_flow(self): ast = self.parser.parse(Testsuite.flow, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_array(self): ast = self.parser.parse(Testsuite.array, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_files(self): ast = self.parser.parse(Testsuite.files, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code def test_input_output(self): ast = self.parser.parse(Testsuite.input_output, lexer = self.lexer) code = SvelTraverse(ast).get_code_and_errors() print code
# get and build lexer svel = SvelLexer() svel.build() # get parser parser = svelYacc.getParser() # provide some data data = ''' //tests Hello.java boolean helloWorldTest() { file helloFile = "../Hello.java"; funct helloMain = {__main__, (j_String[]), helloFile}; input in = (); output out = "Hello World!"; return helloMain.assert(in, out); } ''' #main() { # if(helloWorldTest()) { # print "Hello World passed!"; # } else { # print "Hello World failed."; # } #} #''' # print the results of parsing print parser.parse(data, lexer=svel.get_lexer())
# try to open source code try: source_code = open(input_file).read() except IOERROR, e: sys.exit(e) # get and build lexer; errorlog=lex.NullLogger() removes PLY warnings svel = SvelLexer() svel.build(errorlog=lex.NullLogger()) # get parser; errorlog=yacc.NullLogger() removes PLY warnings parser = svelYacc.getParser(errorlog=yacc.NullLogger()) # parse the data into an abstract syntax tree ast = parser.parse(source_code, lexer=svel.get_lexer()) # walk the tree and get the compiled code if verbose == None: compiled_code, errors = SvelTraverse(ast).get_code_and_errors() else: print ast compiled_code, errors = SvelTraverse(ast, verbose=True).get_code_and_errors() # if arg3 = -d # compiled_code = SvelTraverse(ast, debug=true).get_code() # if no errors thrown during compilation, go ahead and write compiled code to file if errors == False: output_file = open(output_filename, 'w') output_file.write(compiled_code) print "Success: compiled to " + output_filename
class Yacc(unittest.TestCase): def setUp(self): self.lex = SvelLexer() self.lex.build() self.lexer = self.lex.get_lexer() self.parser = svelYacc.getParser() def test_hello(self): code = self.parser.parse(Testsuite.hello_world) print code def test_comments(self): code = self.parser.parse(Testsuite.comments) print code def test_empty(self): code = self.parser.parse(Testsuite.empty) print code def test_assingments(self): code = self.parser.parse(Testsuite.assignments) print code def test_boolean(self): code = self.parser.parse(Testsuite.expressions_boolean) print code def test_relational(self): code = self.parser.parse(Testsuite.expressions_relational) print code def test_math(self): code = self.parser.parse(Testsuite.expressions_math) print code def test_while(self): code = self.parser.parse(Testsuite.loops_while) print code def test_for(self): code = self.parser.parse(Testsuite.loops_for) print code def test_flow(self): code = self.parser.parse(Testsuite.flow) print code def test_array(self): code = self.parser.parse(Testsuite.array) print code def test_files(self): code = self.parser.parse(Testsuite.files) print code def test_input_output(self): code = self.parser.parse(Testsuite.input_output) print code
'empty :' pass def p_error(p): # we should throw compiler error in this case if p == None: print "Syntax error at last token." else: print "Syntax error around line number \n %d : %s " % (p.lineno, p.value) if __name__ == '__main__': # get and build lexer svel = SvelLexer() svel.build() parser = getParser() # loop to get user input while True: # print prompt and gather input try: line = raw_input("Enter a string to parse\n") # if Ctrl-D, exit except EOFError: break # otherwise, tokenize the string print parser.parse(line, lexer=svel.get_lexer())
# get and build lexer svel = SvelLexer() svel.build() # get parser parser = svelYacc.getParser() from testsuite import Testsuite ts = Testsuite() # If run with no arguments -- test all cases in testsuite # $ python testSvelYacc.py if len(sys.argv) == 1: cases = ts.getAll() i = 0; for case in cases: print 80*'=' print '#%d test code: \n %s \n' % (i, case) print parser.parse(case, lexer=svel.get_lexer()) i = i + 1 # If run with arguments -- test those case numbers in testsuite # $ python testSvelYacc.py 0 3 else: i = 1; while i < len(sys.argv): case = ts.get(int(sys.argv[i])) print 80*'=' print '#%s test code: \n %s \n' % (sys.argv[i], case) print parser.parse(case, lexer=svel.get_lexer()) i = i + 1