コード例 #1
0
def get_follow(file_name):
    follow = {}
    first = get_first(file_name)
    grammer_productions_list = getBNF(file_name)
    production_dist = {}
    for each_production in grammer_productions_list:
        left, right = each_production.split('->')
        if production_dist.has_key(left):
            production_dist[left].append(right)
        else:
            production_dist[left] = [
                right,
            ]
    grammer_symbol = get_grammer_symbol(grammer_productions_list)
    #print grammer_symbol
    non_terminals_list = []
    terminals_list = []

    for each_symbol in grammer_symbol:
        if each_symbol.istitle():
            non_terminals_list.append(each_symbol)
        else:
            terminals_list.append(each_symbol)

    for each_non_symbol in non_terminals_list:
        if follow.has_key(each_non_symbol):
            continue
        else:
            follow_set = get_follow_set(each_non_symbol, production_dist,
                                        terminals_list, non_terminals_list,
                                        follow, first)
            follow[each_non_symbol] = follow_set

    return follow
コード例 #2
0
def get_first(file_name):
	first = {}
	grammer_productions_list = getBNF(file_name)
	production_dist = {}
	for each_production in grammer_productions_list:
		left,right = each_production.split('->')
		if production_dist.has_key(left):
			production_dist[left].append(right)
		else:
			production_dist[left] = [right,]
	grammer_symbol = get_grammer_symbol(grammer_productions_list)
	#print grammer_symbol
	non_terminals_list = []
	terminals_list = []
	
	for each_symbol in grammer_symbol:
		if each_symbol.istitle():
			non_terminals_list.append(each_symbol)
		else:
			terminals_list.append(each_symbol)

	for each_symbol in non_terminals_list:
		if first.has_key(each_symbol):
			continue
		else:
			null_first_set = set()
			first_set = find_first_set(each_symbol,production_dist,terminals_list,null_first_set,first)
			first[each_symbol] = first_set
	
	for each_symbol in terminals_list:
		first[each_symbol] = set([each_symbol])
	return first
コード例 #3
0
def gen_table(file_name):
	SLR1_table = {}

	items_list,grammer_productions_list = getItemCollection(file_name)
	gram_symbol = get_grammer_symbol(grammer_productions_list)
	C_dict = get_cannonical_collection(items_list,grammer_productions_list,gram_symbol)
	follow = get_follow(file_name)
	
	non_terminals_list = []
	terminals_list = []
	for each_symbol in gram_symbol:
		if each_symbol.istitle():
			non_terminals_list.append(each_symbol)
		else:
			terminals_list.append(each_symbol)
	terminals_list.append('#')


	for each_statu in C_dict.keys():
		each_statu_for_SLR1_table_key = int(each_statu.split('_')[1])
		SLR1_table[each_statu_for_SLR1_table_key] = {}
		for each_item in C_dict[each_statu]:

			if not each_item.endswith('`'):
				char_behind = each_item[each_item.index('`')+1]
				for each_goto in DFA[each_statu]:
					edge_end = each_goto.strip('--')
					if edge_end.startswith(char_behind):
						end = edge_end.split('->I_')[1]
						if char_behind in terminals_list:
							SLR1_table[each_statu_for_SLR1_table_key][char_behind] = 'S'+end
						else:
							if char_behind not in SLR1_table[each_statu_for_SLR1_table_key].keys():
								SLR1_table[each_statu_for_SLR1_table_key][char_behind] = end

			elif each_item.endswith('`') and each_item.split('->')[0] != non_terminals_list[0]:
				left_part = each_item.split('->')[0]
				if each_item.strip('`') != grammer_productions_list[0]:
					for each_a in follow[left_part]:
						SLR1_table[each_statu_for_SLR1_table_key][each_a] = 'r'+str(grammer_productions_list.index(each_item.strip('`')))

			else:
				SLR1_table[each_statu_for_SLR1_table_key]['#'] = 'acc'

	return SLR1_table
コード例 #4
0
                    flag = 1
                elif generated_item is not None:
                    J_n = ''
                    for each_i in C_dict:
                        if C_dict[each_i] == generated_item:
                            J_n = each_i
                            break
                    in_flag = 0
                    if DFA.has_key(each_I):
                        for eachiii in DFA[each_I]:
                            if eachiii.find(J_n) != -1:
                                in_flag = 1
                                break
                    if in_flag == 0:
                        add_to_DFA(each_I, each_grammer_symbol, J_n)
        if flag == 0:
            break
    return C_dict


if __name__ == '__main__':
    items_list, grammer_productions_list = getItemCollection("grammer.txt")
    gram_symbol = get_grammer_symbol(grammer_productions_list)
    C_dict = get_cannonical_collection(items_list, grammer_productions_list,
                                       gram_symbol)
    # print DFA
    for each, item in C_dict.items():
        print(each, item)
    for each, item in DFA.items():
        print each, item
コード例 #5
0
from cannonical_collection import DFA
from pylsy import pylsytable

if __name__ == '__main__':
	file_name = raw_input('input file name which contains grammer(eg:grammer.txt):>').strip()
	# file_name = 'grammer.txt'
	SLR1_tabel = gen_table(file_name)
	items_list,grammer_productions_list = getItemCollection(file_name)
	print 'augmented grammar is:\nG\':'
	for each in grammer_productions_list:
		print each

	
	non_terminals_list = []
	terminals_list = []
	grammer_symbol = get_grammer_symbol(grammer_productions_list)
	C_dict = get_cannonical_collection(items_list,grammer_productions_list,grammer_symbol)
	for each_symbol in grammer_symbol:
		if each_symbol.istitle():
			non_terminals_list.append(each_symbol)
		else:
			terminals_list.append(each_symbol)

	print '==================================================='
	print 'cannonical LR(0) collection:'
	for key,val in C_dict.items():
		print key,val
	print '==================================================='
	print 'DFA:'
	for key,val in DFA.items():
		print key,val