Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program implements a recursive descent parser for grammarrules 5, 6, 7 below.Implement rules 1, 2, 3, 4.Note that the relational operators in rule 4

This program implements a recursive descent parser for grammarrules 5, 6, 7 below.Implement rules 1, 2, 3, 4.Note that the relational operators in rule 4 and the TrueFalse literals in rule 3 require spaces.around the operator.Single quote marks are used to indicate terminal symbols, so'|' means the | is a terminal symbol, while | alone is ameta-symbol seperating different re-writing rules.1 { '|' }2 { '&' }3 ( ) | '~' | ' True ' | ' False ' | 4 ' ' | ' = ' | ' == '5 { '+' | '-' }6 { '*' | '/' }7 () | | 'pi'

image text in transcribed

image text in transcribed

image text in transcribed

This program implements a recursive descent parser for grammar rules 5, 6, 7 below. Implement rules 1, 2, 3, 4. Note that the relational operators in rule 4 and the True False literals in rule 3 require spaces. around the operator. Single quote marks are used to indicate terminal symbols, so T means the is a terminal symbol, while | alone is a meta-symbol seperating different re-writing rules. 1 - { 'T' } 2 { '&' } 3 | True + False ! | 4 - { '+' '.' } 6 - { "*' 1.'/' } 7 - ( | l 'pi' 1 1 {+ -cterm>} def exp(): global i, err value = term() while True: if w[i] i += 1 value = binary op('+', value, term()) elif w[i] i += 1 value = binary_op('-', value, term()) else: break == # return value # Parse a Term -- {+ | } # def term(): global i, err value = factor while True: if w[i] i += 1 value = binary_op("*', value, factor) elif w[i] == '/' : i += 1 value = binary_op('/', value, factor() else: break return value # # Parse a Factor - ( | # def factor(): global i, err value = None if w[i] == i += 1 # read the next character value = exp if w[i] i += 1 return value else: print('missing )') raise ParseError elif w[i] 'pi': i += 1 return math.pi else: try: value = atomic(w[i]) i += 1 # read the next character except ValueError: print('number expected') value = None #print('factor returning', value) if value == None: raise ParseError return value # BACK END PARSER (ACTION RULES) def binary_op(op, lhs, rhs): if op =='+': return ths + rhs elif op == '.': return ths - rhs elif op == *': return ths * rhs elif op == '/'; return lhs / rhs else: return None def atomic(x): return float(x) # User Interface # W = input(" Enter expression: ') while w !": # Split string into token list. for c in '()+-*/': W = w.replace(c, '+c+'') w = w.split() w.append('$') # EOF marker print(" Token Stream: end for t in w: print(t, end = end print(' ') i = 0 try: print("Value: ', exp() # call the parser except: print (parse error') print if wij != 'S': print('Syntax error:') print read un-read: end = '') for c in w[:i]: print(c, end = print i, end = "') for c in w[i:]: print(c, end = ') print() w = input(' Enter expression: ') 1 1 Example test run: Enter expression: 2*(3 + 4) - { 'T' } 2 { '&' } 3 | True + False ! | 4 - { '+' '.' } 6 - { "*' 1.'/' } 7 - ( | l 'pi' 1 1 {+ -cterm>} def exp(): global i, err value = term() while True: if w[i] i += 1 value = binary op('+', value, term()) elif w[i] i += 1 value = binary_op('-', value, term()) else: break == # return value # Parse a Term -- {+ | } # def term(): global i, err value = factor while True: if w[i] i += 1 value = binary_op("*', value, factor) elif w[i] == '/' : i += 1 value = binary_op('/', value, factor() else: break return value # # Parse a Factor - ( | # def factor(): global i, err value = None if w[i] == i += 1 # read the next character value = exp if w[i] i += 1 return value else: print('missing )') raise ParseError elif w[i] 'pi': i += 1 return math.pi else: try: value = atomic(w[i]) i += 1 # read the next character except ValueError: print('number expected') value = None #print('factor returning', value) if value == None: raise ParseError return value # BACK END PARSER (ACTION RULES) def binary_op(op, lhs, rhs): if op =='+': return ths + rhs elif op == '.': return ths - rhs elif op == *': return ths * rhs elif op == '/'; return lhs / rhs else: return None def atomic(x): return float(x) # User Interface # W = input(" Enter expression: ') while w !": # Split string into token list. for c in '()+-*/': W = w.replace(c, '+c+'') w = w.split() w.append('$') # EOF marker print(" Token Stream: end for t in w: print(t, end = end print(' ') i = 0 try: print("Value: ', exp() # call the parser except: print (parse error') print if wij != 'S': print('Syntax error:') print read un-read: end = '') for c in w[:i]: print(c, end = print i, end = "') for c in w[i:]: print(c, end = ') print() w = input(' Enter expression: ') 1 1 Example test run: Enter expression: 2*(3 + 4)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions