return first_list return first_list def checkForeps(grammar, first): for non_terminal in grammar.keys(): for eachProduction in grammar[non_terminal]: m = re.match("[A-Z]+", eachProduction) if m: if m.end() == len(eachProduction): eps_present = True for symbol in eachProduction: if 'e' not in first[symbol]: eps_present = False break if eps_present: if 'e' not in first[non_terminal]: first[non_terminal].append('e') else: if 'e' in first[non_terminal]: first[non_terminal].remove('e') if __name__ == '__main__': file_name = raw_input('Enter the file name: \n') grammar = read_grammar(file_name) first = find_first(grammar) print first
d['from'] = [fol[j].fro] d['to'] = [fol[j].name] d['on'] = [fol[j].on] for k in fol[j].sl: m1 = re.search("(.)->(.*)",k) str1 = m1.group(1)[:] str2 = m1.group(2)[:] if str1 in d.keys(): d[str1].append(createspace(str2)) else: d[str1] = [createspace(str2)] lod.append(d) return lod if __name__=='__main__': filename = raw_input('Enter the file name:\n') list_ = read_grammar(filename) fl = getclosures(list_) #print list_ #a = ["S->.H","H->.aHbB","B->.aA","A->.b"] #fl = getclosures() for d in fl: print d
for r1 in r[1]: # Checking if dot is present in string. Because '.' is not present in 'from','to','on' and 'state' if '.' in r1: pos_of_dot=r1.index('.') #Checking if dot is present in the middle of the string. If in middle goto shift if pos_of_dot < (len(r1)-2): L = shift(r1[pos_of_dot+2],i,i1,L,O) # If dot is present at the end of the string, reduce. elif pos_of_dot==len(r1)-2 : if len(r1)==4: r2=r1.split('.') r3=r2[0].split(' ') if r3[0]==grammar[0][0] and r[0]=='H': L[i1]['$']='accept' else: L = reduce(r[0],r1,i,L,grammar,follow) else: L = reduce(r[0],r1,i,L,grammar,follow) return L if __name__ == '__main__': file_name = raw_input('Enter the file name: \n') grammar = read_grammar(file_name) print grammar generate_table(grammar)
d = {} d['from'] = [fol[j].fro] d['to'] = [fol[j].name] d['on'] = [fol[j].on] for k in fol[j].sl: m1 = re.search("(.)->(.*)", k) str1 = m1.group(1)[:] str2 = m1.group(2)[:] if str1 in d.keys(): d[str1].append(createspace(str2)) else: d[str1] = [createspace(str2)] lod.append(d) return lod if __name__ == '__main__': filename = raw_input('Enter the file name:\n') list_ = read_grammar(filename) fl = getclosures(list_) #print list_ #a = ["S->.H","H->.aHbB","B->.aA","A->.b"] #fl = getclosures() for d in fl: print d