In python please answer number 4 and 5 correctly with explanation 0whiletop = s. pop()# if top is a terminal, don't expand it, but add it to outputifelse:# choose one expansion from all that...


In python please answer number 4 and 5 correctly with explanation


[Note that to generate a complete sentence, we start with S.]<br>4. Complete Grammar.py by completing the generate method in the code below:<br>def generate(self, start):<br>s = Stack()<br>output = []<br># add the starting symbol to the stack<br># loop while size > 0<br>while<br>top = s. pop()<br># if top is a terminal, don't expand it, but add it to output<br>if<br>else:<br># choose one expansion from all that might be used<br>choices = self.getExpansions(top)<br>expansion = random.choice(choices)<br># put the chosen expansion symbols onto the stack<br>for symbol in expansion:<br>return “

Extracted text: [Note that to generate a complete sentence, we start with S.] 4. Complete Grammar.py by completing the generate method in the code below: def generate(self, start): s = Stack() output = [] # add the starting symbol to the stack # loop while size > 0 while top = s. pop() # if top is a terminal, don't expand it, but add it to output if else: # choose one expansion from all that might be used choices = self.getExpansions(top) expansion = random.choice(choices) # put the chosen expansion symbols onto the stack for symbol in expansion: return “ ".join(output) def isTerminal(self, terminal): return terminal not in self.nonterminals def getExpansions(self, nonterm): expansions = [] for (nt, expansion) in self.rules: if nt -- nonterm: # this rule matches copy - list(expansion) expansions.append(copy) return expansions 5. Write a description of how Grammar.py works.
Jun 09, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here