Update app.py
Browse files67 unmodified lines68697071717273742 unmodified lines777879808180818283843 unmodified lines88899091919293939495961 unmodified line98991001011011021031045 unmodified lines11011111211311311411511611711711811912012112212312412412512612712712812913013113213113213313413513613613713813813914014114114214214314414414514614714814714814915015115215315315415415515615715815915715816016116216016316416516616716816917016717117216917017117217317417517617717817918018118218318418518618718817317417517629 unmodified lines20620720822420921021121222821321421521623221721821922067 unmodified lines return newDict
# Function to calculate FIRST setdef first(rule):def 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 lines fres = [] rhs_rules = diction[rule[0]] for itr in rhs_rules: indivRes = first(itr) if type(indivRes) is list: indivRes = first(itr, diction, term_userdef) if isinstance(indivRes, list): fres.extend(indivRes) else: fres.append(indivRes)3 unmodified lines newList = fres fres.remove('#') if len(rule) > 1: ansNew = first(rule[1:]) ansNew = first(rule[1:], diction, term_userdef) if ansNew is not None: if type(ansNew) is list: if isinstance(ansNew, list): newList = fres + ansNew else: newList = fres + [ansNew]1 unmodified line return fres
# Function to calculate FOLLOW setdef follow(nt):def follow(nt, diction, start_symbol): solset = set() if nt == start_symbol: solset.add('$')5 unmodified lines index_nt = subrule.index(nt) subrule = subrule[index_nt + 1:] if len(subrule) != 0: res = first(subrule) res = first(subrule, diction, term_userdef) if res is not None: if '#' in res: res.remove('#') follow_res = follow(curNT) follow_res = follow(curNT, diction, start_symbol) if follow_res: res += follow_res else: res = [] else: if nt != curNT: res = follow(curNT) res = follow(curNT, diction, start_symbol) if res is None: res = [] solset.update(res if type(res) is list else [res]) solset.update(res if isinstance(res, list) else [res]) return list(solset)
# Compute FIRST for all non-terminalsdef computeAllFirsts(): global firstsdef computeAllFirsts(diction, term_userdef): firsts = {} for y in diction.keys(): firsts[y] = set() for sub in diction[y]: result = first(sub) result = first(sub, diction, term_userdef) if result is not None: firsts[y].update(result if type(result) is list else [result]) firsts[y].update(result if isinstance(result, list) else [result]) return firsts
# Compute FOLLOW for all non-terminalsdef computeAllFollows(): global followsdef computeAllFollows(diction, start_symbol): follows = {} for NT in diction.keys(): follows[NT] = set(follow(NT)) follows[NT] = set(follow(NT, diction, start_symbol)) return follows
# Parse table creation functiondef createParseTable(): global term_userdef, firsts, followsdef createParseTable(diction, term_userdef, firsts, follows): table = {} grammar_is_LL = True for lhs in diction.keys(): table[lhs] = {} for term in term_userdef + ['$']: table[lhs][term] = "" for term in term_userdef + ['$']: # Include end of input symbol table[lhs][term] = "" # Set default empty entries
# Populate the parse table for lhs in diction: for rule in diction[lhs]: first_res = first(rule) if '#' in first_res: first_res = first(rule, diction, term_userdef) if '#' in first_res: # Epsilon handling first_res.remove('#') follow_res = follows[lhs] 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 = False return table, grammar_is_LL st.error(f"Conflict detected in parse table at [{lhs}, {term}]")
# 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: continue else: rule = parse_table.get(top_stack, {}).get(top_buffer, None) if rule: rule_rhs = rule.split('->')[1].strip().split() stack = rule_rhs + stack else: return "Invalid String" return "Valid String" return table, grammar_is_LL
# Streamlit Interfacest.title("LL(1) Grammar Analyzer")29 unmodified lines diction = LeftFactoring(diction) st.write(diction)
computeAllFirsts() firsts = computeAllFirsts(diction, term_userdef) st.subheader("FIRST Sets") st.write(firsts)
computeAllFollows() follows = computeAllFollows(diction, start_symbol) st.subheader("FOLLOW Sets") st.write(follows)
parse_table, grammar_is_LL = createParseTable() parse_table, grammar_is_LL = createParseTable(diction, term_userdef, firsts, follows) st.subheader("Parse Table") st.write(parse_table)
app.py+33-48