Question: ( Please make all files, and make sure the code compiles and produces teh given output. ) - - - - - - Implement the

(Please make all files, and make sure the code compiles and produces teh given output.)------Implement the RE2DFA algorithm using the PLY package. The context-free grammar for the language of regular expressions is shown below.
re : re PLUS term
re : term
term : term factor
term : factor
factor : factor STAR
factor : niggle
niggle : LETTER
niggle : LPAREN re RPAREN
Since the expression tree nodes have various attributes associated with them, such as nullable, firstpos, lastpos, etc., this problem can benefit from building a traditional tree data structure with nodes and pointers to children nodes. I am providing a skeletal file, RENode.py, below, which you should use to construct the tree data structure in REParser.py and also use in the main program to compute the various functions such as nullable, firstpos, and lastpos. For this assignment, we will assume that there are no \epsi or \phi regular expressions.
class RENode:
def __init__(self):
self.operator ='' # '*','.','+', 'leaf'
self.symbol ='' # only for leaf nodes
self.position =0 # only for leaf nodes
self.lchild = None
self.rchild = None # only for . and +
self.nullable = False
self.firstpos = set()
self.lastpos = set()
# Your methods go here
def to_string(self,n):
result =''*n
if self.operator == 'leaf':
result += 'SYMBOL: '+self.symbol
result +=' NULLABLE='+str(self.nullable)
result +=' FIRSTPOS='+str(self.firstpos)
result +=' LASTPOS='+str(self.lastpos)+'
'
elif self.operator =='*':
result += 'OPERATOR: STAR'
result +=' NULLABLE='+str(self.nullable)
result +=' FIRSTPOS='+str(self.firstpos)
result +=' LASTPOS='+str(self.lastpos)+'
'
result += self.lchild.to_string(n+2)
elif self.operator =='.':
result += 'OPERATOR: DOT'
result +=' NULLABLE='+str(self.nullable)
result +=' FIRSTPOS='+str(self.firstpos)
result +=' LASTPOS='+str(self.lastpos)+'
'
result += self.lchild.to_string(n+2)
result += self.rchild.to_string(n+2)
elif self.operator =='+':
result += 'OPERATOR: PLUS'
result +=' NULLABLE='+str(self.nullable)
result +=' FIRSTPOS='+str(self.firstpos)
result +=' LASTPOS='+str(self.lastpos)+'
'
result += self.lchild.to_string(n+2)
result += self.rchild.to_string(n+2)
else:
result += 'SOMETHING WENT WRONG'
return result
def __str__(self):
return self.to_string(0)
Submit RE.py, RELexer.py, REParser.py, RENode.py, and DFA.py. A sample run is shown below:
$ python3 RE2DFA.py (aa+bb)*aaa
start 1_3_5
final 8_2_6
1_3_5:a:2_6
1_3_5:b:4
2_6:a:1_3_5_7
2_6:b:EMPTY
4:a:EMPTY
4:b:1_3_5
1_3_5_7:a:8_2_6
1_3_5_7:b:4
2_6_8:a:1_3_5_7
2_6_8:b:EMPTY
EMPTY:a:EMPTY
EMPTY:b:EMPTY

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!