Question

Write a python parser program that uses the following context free grammar <S> → ‘a’ <S>...

Write a python parser program that uses the following context free grammar

<S> → ‘a’ <S> ‘b’

<S> → ‘c’

Test your parser with c, acb, aacbb, λ, ca, ab,acbb, aacb, and bca(λ represents a null string)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

s = input('Enter String: ')
s = list(s)
buff = ['S']
print('Input',s,' Buffer',buff)
while s!=[]:
if buff==[]:
break
if s[0]==buff[0]:
print('Match ',s.pop(0))
buff.pop(0)
elif buff[0]=='S':
buff.pop(0)
if s[0]=='a':
buff=['a','S','b']+buff
print('Using Production S->aSb')
elif s[0]=='c':
buff=['c']+buff
print('Using Production S->c')
else:
print('Error in parsing!! No Production found!!')
break;
else:
print('Error in parsing!! Mismatch found')
break;
print('Input',s,' Buffer',buff)
if s==[] and buff==[]:
print('Parsing complete!!')
else:
print('Parsing failed!!')

Add a comment
Know the answer?
Add Answer to:
Write a python parser program that uses the following context free grammar <S> → ‘a’ <S>...
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