Question
Rewrite Grammar with python
2. Now we can begin on our LSystem class. First, we have to write a method that does the rewriting in the grammar. The idea h

Better pictures. this is only one question
2. Now we can begin on our Lsystem class. First, we have to write a method that does the rewriting in the grammar. The idea h
For our rewrite method well want to have stored the axiom, the production rules, and the number of generations in the LSyste
2. Now we can begin on our LSystem class. First, we have to write a method that does the rewriting in the grammar. The idea here is that you start out with an axiom which is just a string. Then you apply rewrite rules, called productions, to expand some or all of the letters in the axiom to create a string, called a derived word. You can rewrite the word again using the same productions to get further generations of words in the grammar. Here's an example: axiom :A rules : (A→AB), (B→ A) which produces: AB as the first derived word (n-1) since A is replaced by AB For n 2 we take the AB from generation 1 and apply the production rules again to get ABA since the A changes to AB and the B changes to A For n-3, we use the rules to move from ABA to ABAAB Then for n-4, it becomes ABAABABA It continues on in this way for as many generations as you like. For our rewrite method we'll want to have stored the axiom, the production rules, and the number of generations in the LSystem object during the constructor call (remember, this is the_init_ method). Then we need a method, rewrite, that produces the derivation string: def renrite(self): "" "Rewrite the word into its n-th derivation. self nord a string that is a sequence of letters from the alphabet of an L-system. self.productions a dictionary of replacement rules--the keys are letters in the L-system alphabet and the values are the replacement string for the given key self.generationsthe number of times to transform the word through the productions >> sys.rewrite) ABA 5>x sys.rewriteO ABAABABA >>s sys.rewrite) If there is no rule for a letter, you just leave the letter in place.
2. Now we can begin on our Lsystem class. First, we have to write a method that does the rewriting in the grammar. The idea here is that you start out with an axiom which is just a string. Then you apply rewrite rules, called productions, to expand some or all of the letters in the axiom to create a string, called a derived word. You can rewrite the word again using the same productions to get further generations of words in the grammar. Here's an example: axiom : A rules : (A → AB), (B → A) which produces: AB as the first derived word (n-1) since A is replaced by AB For n-2we take the AB from generation 1 and apply the production rules again to get ABA since the A changes to AB and the B changes to A For n 3, we use the rules to move from ABA to ABAAB Then for n-4, it becomes ABAABABA. It continues on in this way for as many generations as you like.
For our rewrite method we'll want to have stored the axiom, the production rules, and the number of generations in the LSystem object during the constructor call (remember, this is the_init_ method). Then we need a method, rewrite, that produces the derivation string: def rewrite(self): * Rewrite the word into its n-th derivation. self.word -- a string that is a sequence of letters from the alphabet of an L-system self.productionsa dictionary of replacement rules--the keys are letters in the L-system alphabet and the values are the replacement string for the given key. self.generations the number of times to transform the word through the productions. sys LSystem('A, 'A': 'AB', 'B': 'A', 2) >>> sys.rewriteO ABA sys LSystem('A, ('A': 'AB', 'B': 'A', 4) >> sys.rewriteO ABAABABA >>> sys LSystemC'A, 'A: AB, 'B: 'A', 0) >>> sys,rewriteC) If there is no rule for a letter, you just leave the letter in place.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation for the code is code comments:

class LSystem():
#init the word,productions,generations
def __init__(self,w,p,g):
self.word = w
self.productions = p
self.generations = g
  
def rewrite(self):
#get word into a local variable
a = self.word
#if generations=0, output word
if self.generations==0:
print(self.word)
return
else:
#run loop until generations is > 0
while self.generations>0 :
#ans stored language generated at each genearation
ans = ""
#run loop for the length of current word
for i in range(len(a)):
#if there is a production for current alphabet then expand and append it to ans
if a[i] in self.productions:
ans+=self.productions[a[i]]
#else just let the alphabet remain the same as stated in the question
else:
ans+=a[i]
#update word to current expanded language
a = ans
#decrement generations
self.generations-=1
print(a)
#driver code
sys = LSystem('A',{'A':'AB','B':'A'},4)
print("For generations : 4")
sys.rewrite()

ktuzzy Spyder (Python 3.J) Eile Edit Searh So un Debug Consales Projects Tools View Help Var able espkre r complexpy X gramarIn [44]: runfile(C:/Users/Rogue/Desktop/gramarExpansion.py, wdir-C:/Users Rogue/Desktop) For generations0 In [45]: runfile(

Add a comment
Know the answer?
Add Answer to:
Rewrite Grammar with python Better pictures. this is only one question 2. Now we can begin on our LSystem clas...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT