示例#1
0
文件: test.py 项目: palmerc/lab
class ParserTestCase(unittest.TestCase):
    """This test check to make sure the internal representation matches the file read"""

    def setUp(self):
        f = open("../grammars/" + self.file)
        G = f.read()
        f.close()

        self.grammar_one = Parser(G)
        expected_s = StringIO()
        self.grammar_one.printer(expected_s)
        expected_s.seek(0, 0)
        self.grammar_two = Parser(expected_s.read())

    def test_start_token(self):
        """%start nonterminal"""
        self.assertEquals(self.grammar_one.grammar.start_token, self.grammar_two.grammar.start_token)

    def test_terminals(self):
        """%token terminals"""
        self.assertEquals(len(self.grammar_one.grammar.terminal_name), len(self.grammar_two.grammar.terminal_name))
        for (a, b) in zip(self.grammar_one.grammar.terminal_name, self.grammar_two.grammar.terminal_name):
            self.assertEquals(a, b)

    def test_productions(self):
        """Parser Production Count"""
        self.assertEquals(len(self.grammar_one.grammar.production_list), len(self.grammar_two.grammar.production_list))
        for (a, b) in zip(self.grammar_one.grammar.production_list, self.grammar_two.grammar.production_list):
            self.assertEquals(a.ls, b.ls)
示例#2
0
文件: lr.py 项目: palmerc/lab
def main(argv):
	# open file
	try:
		filename = argv[0]
	except IndexError:
		print "Usage: %s grammar.yacc" % sys.argv[0]
		sys.exit(2)
	
	try:
		f = open(filename, "r")
	except IOError:
		print "Unable to open file", filename
		sys.exit(2)

	G = f.read()
	f.close()
       
	parse = Parser(G)
	
	t = Transform(parse)
	t.pa()
	
	parse.printer()