Neha13 commited on
Commit
661eb32
·
verified ·
1 Parent(s): 1e555f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
 
 
3
  rules = []
4
  nonterm_userdef = []
5
  term_userdef = []
@@ -8,6 +9,7 @@ firsts = {}
8
  follows = {}
9
  start_symbol = None
10
 
 
11
  def removeLeftRecursion(rulesDiction):
12
  store = {}
13
  for lhs in rulesDiction:
@@ -34,6 +36,7 @@ def removeLeftRecursion(rulesDiction):
34
  rulesDiction[left] = store[left]
35
  return rulesDiction
36
 
 
37
  def LeftFactoring(rulesDiction):
38
  newDict = {}
39
  for lhs in rulesDiction:
@@ -64,6 +67,7 @@ def LeftFactoring(rulesDiction):
64
  newDict[key] = tempo_dict[key]
65
  return newDict
66
 
 
67
  def first(rule):
68
  if len(rule) != 0 and rule[0] in term_userdef:
69
  return rule[0]
@@ -93,6 +97,7 @@ def first(rule):
93
  fres.append('#')
94
  return fres
95
 
 
96
  def follow(nt):
97
  solset = set()
98
  if nt == start_symbol:
@@ -122,6 +127,7 @@ def follow(nt):
122
  solset.update(res if type(res) is list else [res])
123
  return list(solset)
124
 
 
125
  def computeAllFirsts():
126
  global firsts
127
  for y in diction.keys():
@@ -131,11 +137,13 @@ def computeAllFirsts():
131
  if result is not None:
132
  firsts[y].update(result if type(result) is list else [result])
133
 
 
134
  def computeAllFollows():
135
  global follows
136
  for NT in diction.keys():
137
  follows[NT] = set(follow(NT))
138
 
 
139
  def createParseTable():
140
  global term_userdef, firsts, follows
141
  table = {}
@@ -158,6 +166,7 @@ def createParseTable():
158
  grammar_is_LL = False
159
  return table, grammar_is_LL
160
 
 
161
  def validateStringUsingStackBuffer(parse_table, input_string):
162
  stack = [start_symbol, '$']
163
  buffer = ['$'] + input_string.split()[::-1]
@@ -178,10 +187,9 @@ def validateStringUsingStackBuffer(parse_table, input_string):
178
  return "Invalid String"
179
  return "Valid String"
180
 
181
-
182
  st.title("LL(1) Grammar Analyzer")
183
 
184
-
185
  st.subheader("Grammar Rules Input")
186
  start_symbol = st.text_input("Enter Start Symbol (Non-terminal)")
187
  num_rules = st.number_input("Number of Grammar Rules", min_value=1, step=1)
@@ -191,11 +199,9 @@ for i in range(num_rules):
191
  if rule:
192
  rules.append(rule)
193
 
194
-
195
  nonterm_userdef = st.text_input("Enter Non-Terminals (comma-separated)").split(',')
196
  term_userdef = st.text_input("Enter Terminals (comma-separated)").split(',')
197
 
198
-
199
  if st.button("Analyze Grammar"):
200
  diction.clear()
201
  firsts.clear()
 
1
  import streamlit as st
2
 
3
+ # Initial setup for grammar input
4
  rules = []
5
  nonterm_userdef = []
6
  term_userdef = []
 
9
  follows = {}
10
  start_symbol = None
11
 
12
+ # Function to remove left recursion
13
  def removeLeftRecursion(rulesDiction):
14
  store = {}
15
  for lhs in rulesDiction:
 
36
  rulesDiction[left] = store[left]
37
  return rulesDiction
38
 
39
+ # Function to perform left factoring
40
  def LeftFactoring(rulesDiction):
41
  newDict = {}
42
  for lhs in rulesDiction:
 
67
  newDict[key] = tempo_dict[key]
68
  return newDict
69
 
70
+ # Function to calculate FIRST set
71
  def first(rule):
72
  if len(rule) != 0 and rule[0] in term_userdef:
73
  return rule[0]
 
97
  fres.append('#')
98
  return fres
99
 
100
+ # Function to calculate FOLLOW set
101
  def follow(nt):
102
  solset = set()
103
  if nt == start_symbol:
 
127
  solset.update(res if type(res) is list else [res])
128
  return list(solset)
129
 
130
+ # Compute FIRST for all non-terminals
131
  def computeAllFirsts():
132
  global firsts
133
  for y in diction.keys():
 
137
  if result is not None:
138
  firsts[y].update(result if type(result) is list else [result])
139
 
140
+ # Compute FOLLOW for all non-terminals
141
  def computeAllFollows():
142
  global follows
143
  for NT in diction.keys():
144
  follows[NT] = set(follow(NT))
145
 
146
+ # Parse table creation function
147
  def createParseTable():
148
  global term_userdef, firsts, follows
149
  table = {}
 
166
  grammar_is_LL = False
167
  return table, grammar_is_LL
168
 
169
+ # Validate input string function
170
  def validateStringUsingStackBuffer(parse_table, input_string):
171
  stack = [start_symbol, '$']
172
  buffer = ['$'] + input_string.split()[::-1]
 
187
  return "Invalid String"
188
  return "Valid String"
189
 
190
+ # Streamlit Interface
191
  st.title("LL(1) Grammar Analyzer")
192
 
 
193
  st.subheader("Grammar Rules Input")
194
  start_symbol = st.text_input("Enter Start Symbol (Non-terminal)")
195
  num_rules = st.number_input("Number of Grammar Rules", min_value=1, step=1)
 
199
  if rule:
200
  rules.append(rule)
201
 
 
202
  nonterm_userdef = st.text_input("Enter Non-Terminals (comma-separated)").split(',')
203
  term_userdef = st.text_input("Enter Terminals (comma-separated)").split(',')
204
 
 
205
  if st.button("Analyze Grammar"):
206
  diction.clear()
207
  firsts.clear()