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

Experiment - 4: Aim: Write A Program To Determine The First Set of The Given Grammar-Theory

The document describes an experiment to write a program to determine the first set of a given grammar. It explains that a parser can efficiently apply production rules by knowing the first character produced by each rule and comparing it to the current character in the input string. The code defines Rule and Grammar classes to represent the grammar rules and calculate the first sets. It then prints the rules and first sets for a sample grammar.

Uploaded by

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

Experiment - 4: Aim: Write A Program To Determine The First Set of The Given Grammar-Theory

The document describes an experiment to write a program to determine the first set of a given grammar. It explains that a parser can efficiently apply production rules by knowing the first character produced by each rule and comparing it to the current character in the input string. The code defines Rule and Grammar classes to represent the grammar rules and calculate the first sets. It then prints the rules and first sets for a sample grammar.

Uploaded by

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

Experiment - 4

Aim: Write a program to determine the First Set of the given grammar-

Theory: If the compiler would have come to know in advance, that what is the “first
character of the string produced when a production rule is applied”, and comparing it
to the current character or token in the input string it sees, it can wisely take decision
on which production rule to apply.

S -> cAd
A -> bc|a
And the input string is “cad”.

Thus, in the example above, if it knew that after reading character ‘c’ in the input
string and applying S->cAd, next character in the input string is ‘a’, then it would
have ignored the production rule A->bc, and directly use the production rule A->a.

Hence it is validated that if the compiler/parser knows about first character of the
string that can be obtained by applying a production rule, then it can wisely apply the
correct production rule to get the correct syntax tree for the given input string.

Code:

"""
Created on Sun Mar 31 18:58:56 2019

@author: harjeet
"""

class Rule:
nextId = 0

def __init__ (self, left, right):


self.left = left
self.right = right
self.id = Rule.nextId
Rule.nextId += 1

def __repr__ (self):


return 'R{}: {} → {}'.format (self.id, self.left, ' '.join (self.right) )

class Grammar:
def __init__ (self, rules):
self.rules = {rule.id: rule for rule in rules}
self.symbols = set (symbol for rule in rules for symbol in [rule.left] + rule.right)
self.nonTerminals = set (rule.left for rule in rules)
self.terminals = self.symbols - self.nonTerminals
self.calculateFirst ()

def calculateFirst (self):


self.first = {}
for nonTerminal in self.nonTerminals:
self.first [nonTerminal] = self.getFirst (nonTerminal)

def getFirst (self, symbol):


if symbol in self.first: return self.first [symbol]

first = set ()
for rule in (rule for rule in self.rules.values () if rule.left == symbol):
prefix = rule.right [0]
if prefix == symbol: continue
if prefix in self.terminals: first.add (prefix)
else: first |= self.getFirst (prefix)

return first

rules = [Rule ('S', ['E'] ), Rule ('E', ['T'] ), Rule ('E', ['(', 'E', ')'] ), Rule ('T', ['n'] ), Rule
('T', ['+', 'n'] ), Rule ('T', ['T', '+', 'n'] ) ]

g = Grammar (rules)
print ('Rules:')
for rule in g.rules.values (): print ('\t{}'.format (rule) )
for nonTerminal in g.nonTerminals:
print ('First ({}) = {}'.format (nonTerminal, g.first [nonTerminal] ) )

Output :

You might also like