0% found this document useful (0 votes)
16 views2 pages

First

Uploaded by

21-524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

First

Uploaded by

21-524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

FINDING FIRST OF NON-TERMINALS FROM DATE:

THE GIVEN PRODUCTIONS/ GRAMMAR PAGE NO.:


TASK NO.:

AIM:
Write a python program to find the first of non-terminals from the given
productions/grammar.

PROGRAM:
def first(grammar, non_terminal, visited=None):
if visited is None:
visited = set()
first_set = set()

# If non-terminal is a terminal, return {non_terminal}


if non_terminal not in grammar:
return {non_terminal}

# If non-terminal has been visited before, return its first set


if non_terminal in visited:
return first_set

visited.add(non_terminal)

# Iterate over productions for the non-terminal


for production in grammar[non_terminal]:
# For each symbol in the production
for symbol in production:
# If symbol is a terminal, add it to the first set and break
if symbol not in grammar:
first_set.add(symbol)
break
# If symbol is a non-terminal, recursively find its first set
else:
# If epsilon is in the first set of this non-terminal, continue to the next symbol
if 'ε' in first(grammar, symbol, visited):
first_set |= (first(grammar, symbol, visited) - {'ε'})
else:
first_set |= first(grammar, symbol, visited)
break
else:
# If the loop completes without encountering a non-terminal without epsilon, add
epsilon to the first set
first_set.add('ε')
return first_set

def main():
grammar = {}
non_terminals = set()

# Take input for the grammar


while True:
production = input("Enter production (or type 'done' to finish): ").strip()
if production.lower() == 'done':
break
head, body = production.split('->')
head = head.strip()
body = [symbol.strip() for symbol in body.split('|')]
non_terminals.add(head)
if head in grammar:
grammar[head].extend(body)
else:
grammar[head] = body

# Find first sets for all non-terminals


for non_terminal in non_terminals:
first_set = first(grammar, non_terminal)
print(f"First({non_terminal}):", first_set)

if _name_ == "_main_":
main()

EXECUTED INPUT AND OUTPUT:


Enter production (or type 'done' to finish): A->abc
Enter production (or type 'done' to finish): A->def
Enter production (or type 'done' to finish): A->ghi
Enter production (or type 'done' to finish): done
First(A): {'g', 'd', 'a'}

Process finished with exit code 0

RESULT:
Program to find the first of the non-terminals from the given grammar/productions have
been executed successfully.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy