Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
# Helper functions
|
| 5 |
|
|
|
|
| 6 |
def removeLeftRecursion(rulesDiction):
|
| 7 |
store = {}
|
| 8 |
for lhs in rulesDiction:
|
|
@@ -29,20 +29,22 @@ def removeLeftRecursion(rulesDiction):
|
|
| 29 |
rulesDiction[left] = store[left]
|
| 30 |
return rulesDiction
|
| 31 |
|
|
|
|
| 32 |
def LeftFactoring(rulesDiction):
|
| 33 |
newDict = {}
|
| 34 |
for lhs in rulesDiction:
|
| 35 |
allrhs = rulesDiction[lhs]
|
| 36 |
-
temp =
|
| 37 |
for subrhs in allrhs:
|
| 38 |
-
if subrhs[0] not in temp:
|
| 39 |
temp[subrhs[0]] = [subrhs]
|
| 40 |
else:
|
| 41 |
temp[subrhs[0]].append(subrhs)
|
| 42 |
new_rule = []
|
| 43 |
tempo_dict = {}
|
| 44 |
for term_key in temp:
|
| 45 |
-
|
|
|
|
| 46 |
lhs_ = lhs + "'"
|
| 47 |
while (lhs_ in rulesDiction.keys()) or (lhs_ in tempo_dict.keys()):
|
| 48 |
lhs_ += "'"
|
|
@@ -52,132 +54,154 @@ def LeftFactoring(rulesDiction):
|
|
| 52 |
ex_rules.append(g[1:])
|
| 53 |
tempo_dict[lhs_] = ex_rules
|
| 54 |
else:
|
| 55 |
-
new_rule.append(
|
| 56 |
newDict[lhs] = new_rule
|
| 57 |
for key in tempo_dict:
|
| 58 |
newDict[key] = tempo_dict[key]
|
| 59 |
return newDict
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
if type(indivRes) is list:
|
| 73 |
-
fres.extend(indivRes)
|
| 74 |
else:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
else:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
if type(ansNew) is list:
|
| 84 |
-
fres.extend(ansNew)
|
| 85 |
-
else:
|
| 86 |
-
fres.append(ansNew)
|
| 87 |
-
return fres
|
| 88 |
-
fres.append('#')
|
| 89 |
-
return fres
|
| 90 |
-
return []
|
| 91 |
-
|
| 92 |
-
def follow(nt, start_symbol, diction, firsts, follows):
|
| 93 |
-
solset = set()
|
| 94 |
-
if nt == start_symbol:
|
| 95 |
-
solset.add('$')
|
| 96 |
-
for curNT in diction:
|
| 97 |
-
rhs = diction[curNT]
|
| 98 |
-
for subrule in rhs:
|
| 99 |
-
if nt in subrule:
|
| 100 |
-
while nt in subrule:
|
| 101 |
-
index_nt = subrule.index(nt)
|
| 102 |
-
subrule = subrule[index_nt + 1:]
|
| 103 |
-
if len(subrule) != 0:
|
| 104 |
-
res = first(subrule, diction, term_userdef)
|
| 105 |
-
if '#' in res:
|
| 106 |
-
newList = []
|
| 107 |
-
res.remove('#')
|
| 108 |
-
ansNew = follow(curNT, start_symbol, diction, firsts, follows)
|
| 109 |
-
if ansNew:
|
| 110 |
-
newList.extend(ansNew)
|
| 111 |
-
res.extend(newList)
|
| 112 |
-
else:
|
| 113 |
-
if nt != curNT:
|
| 114 |
-
res = follow(curNT, start_symbol, diction, firsts, follows)
|
| 115 |
-
if res is not None:
|
| 116 |
-
solset.update(res)
|
| 117 |
-
return list(solset)
|
| 118 |
-
|
| 119 |
-
# Main Streamlit App Code
|
| 120 |
|
|
|
|
| 121 |
def main():
|
| 122 |
-
|
| 123 |
-
st.
|
| 124 |
-
|
| 125 |
-
# Input
|
| 126 |
-
st.
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
diction = LeftFactoring(diction)
|
| 145 |
-
|
| 146 |
-
st.write("Processed Grammar After Left Recursion and Left Factoring:")
|
| 147 |
-
for y in diction:
|
| 148 |
-
st.write(f"{y} -> {diction[y]}")
|
| 149 |
-
|
| 150 |
-
# Compute First and Follow sets
|
| 151 |
-
firsts = {}
|
| 152 |
-
follows = {}
|
| 153 |
-
start_symbol = list(diction.keys())[0]
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
st.write(f"First({nonterminal}) -> {first_set}")
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
st.write(f"Follow({nonterminal}) -> {follow_set}")
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
|
| 169 |
-
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
st.
|
| 178 |
-
|
| 179 |
-
|
| 180 |
|
| 181 |
-
|
| 182 |
-
if __name__ == '__main__':
|
| 183 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
|
|
|
| 4 |
|
| 5 |
+
# Function to remove left recursion
|
| 6 |
def removeLeftRecursion(rulesDiction):
|
| 7 |
store = {}
|
| 8 |
for lhs in rulesDiction:
|
|
|
|
| 29 |
rulesDiction[left] = store[left]
|
| 30 |
return rulesDiction
|
| 31 |
|
| 32 |
+
# Function for Left Factoring
|
| 33 |
def LeftFactoring(rulesDiction):
|
| 34 |
newDict = {}
|
| 35 |
for lhs in rulesDiction:
|
| 36 |
allrhs = rulesDiction[lhs]
|
| 37 |
+
temp = dict()
|
| 38 |
for subrhs in allrhs:
|
| 39 |
+
if subrhs[0] not in temp.keys():
|
| 40 |
temp[subrhs[0]] = [subrhs]
|
| 41 |
else:
|
| 42 |
temp[subrhs[0]].append(subrhs)
|
| 43 |
new_rule = []
|
| 44 |
tempo_dict = {}
|
| 45 |
for term_key in temp:
|
| 46 |
+
allStartingWithTermKey = temp[term_key]
|
| 47 |
+
if len(allStartingWithTermKey) > 1:
|
| 48 |
lhs_ = lhs + "'"
|
| 49 |
while (lhs_ in rulesDiction.keys()) or (lhs_ in tempo_dict.keys()):
|
| 50 |
lhs_ += "'"
|
|
|
|
| 54 |
ex_rules.append(g[1:])
|
| 55 |
tempo_dict[lhs_] = ex_rules
|
| 56 |
else:
|
| 57 |
+
new_rule.append(allStartingWithTermKey[0])
|
| 58 |
newDict[lhs] = new_rule
|
| 59 |
for key in tempo_dict:
|
| 60 |
newDict[key] = tempo_dict[key]
|
| 61 |
return newDict
|
| 62 |
|
| 63 |
+
# Function to compute the FIRST set
|
| 64 |
+
def first(symbol, grammar, first_sets):
|
| 65 |
+
if symbol in first_sets:
|
| 66 |
+
return first_sets[symbol]
|
| 67 |
+
first_set = set()
|
| 68 |
+
if symbol not in grammar:
|
| 69 |
+
first_set.add(symbol)
|
| 70 |
+
else:
|
| 71 |
+
for rule in grammar[symbol]:
|
| 72 |
+
if rule == ['#']:
|
| 73 |
+
first_set.add('#')
|
|
|
|
|
|
|
| 74 |
else:
|
| 75 |
+
for s in rule:
|
| 76 |
+
first_set |= first(s, grammar, first_sets)
|
| 77 |
+
if '#' not in first(s, grammar, first_sets):
|
| 78 |
+
break
|
| 79 |
+
first_sets[symbol] = first_set
|
| 80 |
+
return first_set
|
| 81 |
+
|
| 82 |
+
# Function to compute the FOLLOW set
|
| 83 |
+
def follow(symbol, grammar, start_symbol, follow_sets, first_sets):
|
| 84 |
+
if symbol in follow_sets:
|
| 85 |
+
return follow_sets[symbol]
|
| 86 |
+
follow_set = set()
|
| 87 |
+
if symbol == start_symbol:
|
| 88 |
+
follow_set.add('$')
|
| 89 |
+
for lhs in grammar:
|
| 90 |
+
for rule in grammar[lhs]:
|
| 91 |
+
for i, s in enumerate(rule):
|
| 92 |
+
if s == symbol:
|
| 93 |
+
if i + 1 < len(rule):
|
| 94 |
+
follow_set |= first(rule[i + 1], grammar, first_sets) - {'#'}
|
| 95 |
+
if i + 1 == len(rule) or '#' in first(rule[i + 1], grammar, first_sets):
|
| 96 |
+
follow_set |= follow(lhs, grammar, start_symbol, follow_sets, first_sets)
|
| 97 |
+
follow_sets[symbol] = follow_set
|
| 98 |
+
return follow_set
|
| 99 |
+
|
| 100 |
+
# Function to compute all FIRST sets
|
| 101 |
+
def computeAllFirsts(grammar):
|
| 102 |
+
first_sets = {}
|
| 103 |
+
for symbol in grammar:
|
| 104 |
+
first(symbol, grammar, first_sets)
|
| 105 |
+
return first_sets
|
| 106 |
+
|
| 107 |
+
# Function to compute all FOLLOW sets
|
| 108 |
+
def computeAllFollows(grammar, start_symbol, first_sets):
|
| 109 |
+
follow_sets = {}
|
| 110 |
+
for symbol in grammar:
|
| 111 |
+
follow(symbol, grammar, start_symbol, follow_sets, first_sets)
|
| 112 |
+
return follow_sets
|
| 113 |
+
|
| 114 |
+
# Function to create the LL(1) parsing table
|
| 115 |
+
def createParseTable(grammar, first_sets, follow_sets, terminals):
|
| 116 |
+
parse_table = {}
|
| 117 |
+
for lhs in grammar:
|
| 118 |
+
for rule in grammar[lhs]:
|
| 119 |
+
first_set = first(rule[0], grammar, first_sets)
|
| 120 |
+
for terminal in first_set - {'#'}:
|
| 121 |
+
if lhs not in parse_table:
|
| 122 |
+
parse_table[lhs] = {}
|
| 123 |
+
parse_table[lhs][terminal] = rule
|
| 124 |
+
if '#' in first_set:
|
| 125 |
+
for terminal in follow_sets[lhs]:
|
| 126 |
+
if lhs not in parse_table:
|
| 127 |
+
parse_table[lhs] = {}
|
| 128 |
+
parse_table[lhs][terminal] = rule
|
| 129 |
+
return parse_table
|
| 130 |
+
|
| 131 |
+
# Function to validate a string using the LL(1) parsing table
|
| 132 |
+
def validateStringUsingStackBuffer(parse_table, grammar_is_LL, terminals, input_string, term_userdef, start_symbol):
|
| 133 |
+
stack = [start_symbol]
|
| 134 |
+
input_string = input_string + ['$']
|
| 135 |
+
idx = 0
|
| 136 |
+
while stack:
|
| 137 |
+
top = stack[-1]
|
| 138 |
+
if top == input_string[idx]:
|
| 139 |
+
stack.pop()
|
| 140 |
+
idx += 1
|
| 141 |
+
elif top in parse_table and input_string[idx] in parse_table[top]:
|
| 142 |
+
rule = parse_table[top][input_string[idx]]
|
| 143 |
+
stack.pop()
|
| 144 |
+
if rule != ['#']:
|
| 145 |
+
stack.extend(reversed(rule))
|
| 146 |
else:
|
| 147 |
+
return "String is not valid."
|
| 148 |
+
if idx == len(input_string):
|
| 149 |
+
return "String is valid."
|
| 150 |
+
return "String is not valid."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
# Main Streamlit App
|
| 153 |
def main():
|
| 154 |
+
st.title("Grammar Analyzer")
|
| 155 |
+
st.markdown("This app performs grammar analysis, including left recursion removal, left factoring, FIRST/FOLLOW set calculations, and LL(1) parsing.")
|
| 156 |
+
|
| 157 |
+
# Input Section
|
| 158 |
+
st.sidebar.header("Input Parameters")
|
| 159 |
+
start_symbol = st.text_input("Start Symbol (Non-terminal):", "S")
|
| 160 |
+
num_rules = st.number_input("Number of Grammar Rules:", min_value=1, value=4)
|
| 161 |
+
|
| 162 |
+
# Input rules dynamically based on the number of rules
|
| 163 |
+
grammar_rules = []
|
| 164 |
+
for i in range(num_rules):
|
| 165 |
+
rule = st.text_input(f"Rule {i + 1}:", f"S -> A k O")
|
| 166 |
+
grammar_rules.append(rule)
|
| 167 |
+
|
| 168 |
+
non_terminals = st.text_input("Non-Terminals (comma-separated):", "S, A, B, C")
|
| 169 |
+
terminals = st.text_input("Terminals (comma-separated):", "a, b, c, d, k, r, O")
|
| 170 |
+
|
| 171 |
+
# Process the inputs
|
| 172 |
+
if st.button("Analyze"):
|
| 173 |
+
# Parse the non-terminals and terminals
|
| 174 |
+
non_terminals_list = [nt.strip() for nt in non_terminals.split(",")]
|
| 175 |
+
terminals_list = [t.strip() for t in terminals.split(",")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
# Parse the grammar rules
|
| 178 |
+
grammar_dict = {}
|
| 179 |
+
for rule in grammar_rules:
|
| 180 |
+
lhs, rhs = rule.split("->")
|
| 181 |
+
rhs_options = rhs.split("|")
|
| 182 |
+
grammar_dict[lhs.strip()] = [option.strip().split() for option in rhs_options]
|
| 183 |
|
| 184 |
+
# Remove left recursion
|
| 185 |
+
grammar_dict = removeLeftRecursion(grammar_dict)
|
|
|
|
| 186 |
|
| 187 |
+
# Apply left factoring
|
| 188 |
+
grammar_dict = LeftFactoring(grammar_dict)
|
|
|
|
| 189 |
|
| 190 |
+
# Compute FIRST and FOLLOW sets
|
| 191 |
+
first_sets = computeAllFirsts(grammar_dict)
|
| 192 |
+
follow_sets = computeAllFollows(grammar_dict, start_symbol, first_sets)
|
| 193 |
|
| 194 |
+
# Generate parsing table
|
| 195 |
+
parse_table = createParseTable(grammar_dict, first_sets, follow_sets, terminals_list)
|
| 196 |
+
|
| 197 |
+
# Display the parsing table
|
| 198 |
+
st.subheader("Parsing Table")
|
| 199 |
+
st.write(parse_table)
|
| 200 |
|
| 201 |
+
# Validate the input string using the parsing table
|
| 202 |
+
input_string = st.text_input("Enter string to validate:", "a c")
|
| 203 |
+
validation_result = validateStringUsingStackBuffer(parse_table, True, terminals_list, input_string.split(), terminals_list, start_symbol)
|
| 204 |
+
st.write(validation_result)
|
| 205 |
|
| 206 |
+
if __name__ == "__main__":
|
|
|
|
| 207 |
main()
|