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 setUp(self): self.lex = SvelLexer() self.lex.build() self.lexer = self.lex.get_lexer() self.parser = svelYacc.getParser()
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 to lib for svelLex file import os, sys lib_path = os.path.join('../lib') sys.path.append(lib_path) # get to ply lib_path = os.path.join('../lib/ply-3.4') sys.path.append(lib_path) # import SvelLexer class from svelLex import SvelLexer # instantiate and build lexer svel = SvelLexer() svel.build() # 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!";
import os, sys lib_path = os.path.join('../lib') sys.path.append(lib_path) # get to ply lib_path = os.path.join('../lib/ply-3.4') sys.path.append(lib_path) # import lexer class from svelLex import SvelLexer # import parser import svelYacc # 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);
# set the name of the compiled code file # e.g. if they compiled helloworld.svel, they'll get helloworld.py output_filename = input_filename + ".py" if(os.path.isfile(output_filename)): sys.exit("Error: Refusing to overwrite " + output_filename + "\nPlease either delete it or rename your source file") # 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
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
def p_empty(p): '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