NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

# First and Follow functions implementation
def first(grammar, symbol):
first_set = set()
if symbol not in grammar:
first_set.add(symbol)
else:
for production in grammar[symbol]:
for char in production:
if char != symbol:
first_set |= first(grammar, char)
if char not in grammar or 'ε' not in grammar[char]:
break
else:
first_set.add('ε')
return first_set

def follow(grammar, start_symbol):
follow_set = {start_symbol}
for left, right in grammar.items():
for production in right:
for i, char in enumerate(production):
if char in grammar:
follow_set |= (first(grammar, production[i+1:]) - {'ε'})
if 'ε' in first(grammar, production[i+1:]):
follow_set |= follow(grammar, left)
return follow_set

# Parse Table creation
def create_parse_table(terminals, non_terminals, grammar):
parse_table = {nt: {t: [] for t in terminals} for nt in non_terminals}
for non_terminal, productions in grammar.items():
for production in productions:
first_of_production = first(grammar, production[0])
for terminal in first_of_production:
if terminal != 'ε':
parse_table[non_terminal][terminal].append(production)
if 'ε' in first_of_production or len(production) == 0:
for terminal in follow(grammar, non_terminal):
parse_table[non_terminal][terminal].append(production)
return parse_table

# User input for Grammar
grammar = {}
terminals = set()
non_terminals = set()

num_productions = int(input("Enter the number of productions: "))
for _ in range(num_productions):
production = input("Enter production (in the form A->alpha): ")
left, right = production.split('->')
if left not in grammar:
grammar[left] = []
grammar[left].append(right.strip())
non_terminals.add(left)
for char in right.strip():
if char.islower():
terminals.add(char)

parse_table = create_parse_table(terminals, non_terminals, grammar)
print(parse_table)


INPUT:Enter the number of productions: 4
Enter production (in the form A->alpha): S->aAB
Enter production (in the form A->alpha): A->a
Enter production (in the form A->alpha): A->bB
Enter production (in the form A->alpha): B->b

OUTPUT :
{
'S': {'a': ['aAB']},
'A': {'a': ['a'], 'b': ['bB'], '$': ['ε']},
'B': {'b': ['b'], '$': ['ε']}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.