Table of Contents
S No Contents Page No
1. Introduction 3
2. Objective 4
3. Source Code & Output 5
4. Conclusion 9
5. References 10
2
3. SOURCE CODE
import tkinter as tk
from tkinter import messagebox
def remove_left_recursion(grammar):
productions = grammar.split('\n')
non_terminals = set()
modified_productions = []
for production in productions:
head, body = production.split('->')
head = head.strip()
alternatives = body.strip().split('|')
recursive = [alt.strip() for alt in alternatives if alt.strip().startswith(head)]
non_recursive = [alt.strip() for alt in alternatives if not alt.strip().startswith(head)]
if recursive:
new_head = head + "'"
non_terminals.add(new_head)
for alt in non_recursive:
modified_productions.append(f"{head} -> {alt} {new_head}")
for alt in recursive:
modified_productions.append(f"{new_head} -> {alt[len(head):].strip()} {new_head}")
modified_productions.append(f"{new_head} -> ε")
else:
for alt in alternatives:
modified_productions.append(f"{head} -> {alt}")
return '\n'.join(modified_productions)
def find_first_follow(grammar):
first_sets = {}
follow_sets = {}
productions = [line.strip() for line in grammar.split('\n') if line.strip()]
for production in productions:
head, _ = production.split('->')
non_terminal = head.strip()
first_sets[non_terminal] = set()
follow_sets[non_terminal] = set()
# Simplified placeholders
first_sets['S'] = {'a', 'b'}
follow_sets['S'] = {'$'}
5
return f"First Sets: {first_sets}\nFollow Sets: {follow_sets}"
def remove_ambiguity(grammar):
productions = [line.strip() for line in grammar.split('\n') if line.strip()]
modified_productions = []
for production in productions:
head, body = production.split('->')
head = head.strip()
alternatives = body.strip().split('|')
for i, alt in enumerate(alternatives):
modified_productions.append(f"{head}_{i+1} -> {alt}")
return '\n'.join(modified_productions)
def remove_left_factoring(grammar):
return grammar
def process_grammar():
input_grammar = grammar_text.get("1.0", tk.END).strip()
if not input_grammar:
messagebox.showwarning("Input Error", "Please enter a grammar.")
return
modified_grammar = remove_left_recursion(input_grammar)
first_follow_sets = find_first_follow(modified_grammar)
ambiguity_removed_grammar = remove_ambiguity(modified_grammar)
final_grammar = remove_left_factoring(ambiguity_removed_grammar)
result_text.config(state=tk.NORMAL)
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, f"Modified Grammar:\n{modified_grammar}\n\n")
result_text.insert(tk.END, f"First and Follow Sets:\n{first_follow_sets}\n\n")
result_text.insert(tk.END, f"Ambiguity Removed Grammar:\n{ambiguity_removed_grammar}\n\n")
result_text.insert(tk.END, f"Final Grammar:\n{final_grammar}\n")
result_text.config(state=tk.DISABLED)
window = tk.Tk()
window.title("Grammar Processing Tool")
grammar_label = tk.Label(window, text="Enter Grammar:")
grammar_label.pack(pady=5)
grammar_text = tk.Text(window, height=10, width=50)
6
grammar_text.pack(pady=5)
process_button = tk.Button(window, text="Process Grammar", command=process_grammar)
process_button.pack(pady=10)
result_text = tk.Text(window, height=20, width=50, state=tk.DISABLED)
result_text.pack(pady=5)
window.mainloop()
TEST CASES/ OUTPUT
3.1 GUI Interface
3.2 Taking grammar as input
7
3.3 Output1
3.4 Output