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
Example #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
# -*- coding: utf-8 -*-

__author__ = 'SkywalkerAtlas'

from BNF import getBNF

def get_grammer_symbol(grammer_productions_list):
	grammer_symbol = []
	for each_production in grammer_productions_list:
		symbols = each_production.split('->',)
		if symbols[0] not in grammer_symbol:
			grammer_symbol.append(symbols[0])
		for symbol in symbols[1]:
			if symbol not in grammer_symbol:
				grammer_symbol.append(symbol)
	return grammer_symbol


if __name__ == '__main__':
    grammer_productions_list = getBNF('grammer.txt')
    grammer_symbol = get_grammer_symbol(grammer_productions_list)
    print grammer_symbol
# -*- coding: utf-8 -*-

__author__ = 'SkywalkerAtlas'

from BNF import getBNF


def get_grammer_symbol(grammer_productions_list):
    grammer_symbol = []
    for each_production in grammer_productions_list:
        symbols = each_production.split('->', )
        if symbols[0] not in grammer_symbol:
            grammer_symbol.append(symbols[0])
        for symbol in symbols[1]:
            if symbol not in grammer_symbol:
                grammer_symbol.append(symbol)
    return grammer_symbol


if __name__ == '__main__':
    grammer_productions_list = getBNF('grammer.txt')
    grammer_symbol = get_grammer_symbol(grammer_productions_list)
    print grammer_symbol