Update app.py
Browse files67 unmodified lines686970717273742 unmodified lines77787980818283843 unmodified lines8889909192939495961 unmodified line98991001011021031045 unmodified lines11011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019129 unmodified lines22122222322422522622722822923023123223323423567 unmodified linesreturn newDict# Function to calculate FIRST setdef first(rule):if len(rule) != 0 and rule[0] in term_userdef:return rule[0]elif len(rule) != 0 and rule[0] == '#':2 unmodified linesfres = []rhs_rules = diction[rule[0]]for itr in rhs_rules:indivRes = first(itr)if type(indivRes) is list:fres.extend(indivRes)else:fres.append(indivRes)3 unmodified linesnewList = fresfres.remove('#')if len(rule) > 1:ansNew = first(rule[1:])if ansNew is not None:if type(ansNew) is list:newList = fres + ansNewelse:newList = fres + [ansNew]1 unmodified linereturn fres# Function to calculate FOLLOW setdef follow(nt):solset = set()if nt == start_symbol:solset.add('$')5 unmodified linesindex_nt = subrule.index(nt)subrule = subrule[index_nt + 1:]if len(subrule) != 0:res = first(subrule)if res is not None:if '#' in res:res.remove('#')follow_res = follow(curNT)if follow_res:res += follow_reselse:res = []else:if nt != curNT:res = follow(curNT)if res is None:res = []solset.update(res if type(res) is list else [res])return list(solset)# Compute FIRST for all non-terminalsdef computeAllFirsts():global firstsfor y in diction.keys():firsts[y] = set()for sub in diction[y]:result = first(sub)if result is not None:firsts[y].update(result if type(result) is list else [result])# Compute FOLLOW for all non-terminalsdef computeAllFollows():global followsfor NT in diction.keys():follows[NT] = set(follow(NT))# Parse table creation functiondef createParseTable():global term_userdef, firsts, followstable = {}grammar_is_LL = Truefor lhs in diction.keys():table[lhs] = {}for term in term_userdef + ['$']:table[lhs][term] = ""for lhs in diction:for rule in diction[lhs]:first_res = first(rule)if '#' in first_res:first_res.remove('#')follow_res = follows[lhs]first_res.update(follow_res)for term in first_res:if table[lhs][term] == "":table[lhs][term] = f"{lhs} -> {' '.join(rule)}"else:grammar_is_LL = Falsereturn table, grammar_is_LL# Validate input string functiondef validateStringUsingStackBuffer(parse_table, input_string):stack = [start_symbol, '$']buffer = ['$'] + input_string.split()[::-1]while stack:top_stack = stack.pop(0)top_buffer = buffer.pop()if top_stack in term_userdef:if top_stack != top_buffer:return "Invalid String"elif top_stack == top_buffer:continueelse:rule = parse_table.get(top_stack, {}).get(top_buffer, None)if rule:rule_rhs = rule.split('->')[1].strip().split()stack = rule_rhs + stackelse:return "Invalid String"return "Valid String"# Streamlit Interfacest.title("LL(1) Grammar Analyzer")29 unmodified linesdiction = LeftFactoring(diction)st.write(diction)computeAllFirsts()st.subheader("FIRST Sets")st.write(firsts)computeAllFollows()st.subheader("FOLLOW Sets")st.write(follows)parse_table, grammar_is_LL = createParseTable()st.subheader("Parse Table")st.write(parse_table)67 unmodified lines686970717273742 unmodified lines77787980818283843 unmodified lines8889909192939495961 unmodified line98991001011021031045 unmodified lines11011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517629 unmodified lines20620720820921021121221321421521621721821922067 unmodified linesreturn newDict# Function to calculate FIRST setdef first(rule, diction, term_userdef):if len(rule) != 0 and rule[0] in term_userdef:return rule[0]elif len(rule) != 0 and rule[0] == '#':2 unmodified linesfres = []rhs_rules = diction[rule[0]]for itr in rhs_rules:indivRes = first(itr, diction, term_userdef)if isinstance(indivRes, list):fres.extend(indivRes)else:fres.append(indivRes)3 unmodified linesnewList = fresfres.remove('#')if len(rule) > 1:ansNew = first(rule[1:], diction, term_userdef)if ansNew is not None:if isinstance(ansNew, list):newList = fres + ansNewelse:newList = fres + [ansNew]1 unmodified linereturn fres# Function to calculate FOLLOW setdef follow(nt, diction, start_symbol):solset = set()if nt == start_symbol:solset.add('$')5 unmodified linesindex_nt = subrule.index(nt)subrule = subrule[index_nt + 1:]if len(subrule) != 0:res = first(subrule, diction, term_userdef)if res is not None:if '#' in res:res.remove('#')follow_res = follow(curNT, diction, start_symbol)if follow_res:res += follow_reselse:res = []else:if nt != curNT:res = follow(curNT, diction, start_symbol)if res is None:res = []solset.update(res if isinstance(res, list) else [res])return list(solset)# Compute FIRST for all non-terminalsdef computeAllFirsts(diction, term_userdef):firsts = {}for y in diction.keys():firsts[y] = set()for sub in diction[y]:result = first(sub, diction, term_userdef)if result is not None:firsts[y].update(result if isinstance(result, list) else [result])return firsts# Compute FOLLOW for all non-terminalsdef computeAllFollows(diction, start_symbol):follows = {}for NT in diction.keys():follows[NT] = set(follow(NT, diction, start_symbol))return follows# Parse table creation functiondef createParseTable(diction, term_userdef, firsts, follows):table = {}grammar_is_LL = Truefor lhs in diction.keys():table[lhs] = {}for term in term_userdef + ['$']: # Include end of input symboltable[lhs][term] = "" # Set default empty entries# Populate the parse tablefor lhs in diction:for rule in diction[lhs]:first_res = first(rule, diction, term_userdef)if '#' in first_res: # Epsilon handlingfirst_res.remove('#')follow_res = follows.get(lhs, [])first_res.update(follow_res)for term in first_res:if table[lhs][term] == "":table[lhs][term] = f"{lhs} -> {' '.join(rule)}"else:grammar_is_LL = Falsest.error(f"Conflict detected in parse table at [{lhs}, {term}]")return table, grammar_is_LL# Streamlit Interfacest.title("LL(1) Grammar Analyzer")29 unmodified linesdiction = LeftFactoring(diction)st.write(diction)firsts = computeAllFirsts(diction, term_userdef)st.subheader("FIRST Sets")st.write(firsts)follows = computeAllFollows(diction, start_symbol)st.subheader("FOLLOW Sets")st.write(follows)parse_table, grammar_is_LL = createParseTable(diction, term_userdef, firsts, follows)st.subheader("Parse Table")st.write(parse_table)
app.py+33-48