Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im having issues with my coding assignment, the input is bigvalue = ( ( ( 7 9 + ( 6 9 ) ) * (

Im having issues with my coding assignment, the input is
bigvalue =(((79+(69))*(32+(9)*36+46))+67+3-38*((32+56))+69-45*62)+(19*53+(11)*2+52)*36+(87)+19-65*((79+(28-14*28-94*17*20)));
print bigvalue;
end
where my code returns
PUSH bigvalue
PUSH 79
PUSH 69
ADD
PUSH 32
PUSH 9
PUSH 36
MULT
PUSH 46
ADD
ADD
MULT
PUSH 67
PUSH 3
PUSH 38
PUSH 32
PUSH 56
ADD
MULT
PUSH 69
PUSH 45
PUSH 62
MULT
ADD
ADD
SUB
ADD
SUB
PUSH 19
PUSH 53
MULT
PUSH 11
PUSH 2
MULT
PUSH 52
ADD
ADD
PUSH 36
MULT
PUSH 87
PUSH 19
PUSH 65
PUSH 79
PUSH 28
PUSH 14
PUSH 28
MULT
PUSH 94
PUSH 17
PUSH 20
MULT
MULT
SUB
SUB
ADD
MULT
ADD
ADD
ADD
SUB
ASSIGN
PUSH bigvalue
PRINT
but the correct output should be
PUSH bigvalue
PUSH 79
PUSH 69
ADD
PUSH 32
PUSH 9
PUSH 36
MULT
PUSH 46
ADD
ADD
MULT
PUSH 67
PUSH 3
PUSH 38
PUSH 32
PUSH 56
ADD
MULT
PUSH 69
PUSH 45
PUSH 62
MULT
SUB
ADD
SUB
ADD
ADD
PUSH 19
PUSH 53
MULT
PUSH 11
PUSH 2
MULT
PUSH 52
ADD
ADD
PUSH 36
MULT
PUSH 87
PUSH 19
PUSH 65
PUSH 79
PUSH 28
PUSH 14
PUSH 28
MULT
PUSH 94
PUSH 17
PUSH 20
MULT
MULT
SUB
SUB
ADD
MULT
SUB
ADD
ADD
ADD
ASSIGN
PUSH bigvalue
PRINT
here is the file im working on that manipulates this
import sys
from etoken import *
class EParser:
def __init__(self, lexer):
self.lexer = lexer
self.current_token = None
self.next_token()
self.has_error = False
# Initialize with a list containing an empty list for global scope operations
self.operations =[[]]
def next_token(self):
self.current_token = self.lexer.get_next_token()
def parse(self):
while self.current_token.token_code != EToken.END and not self.has_error:
self.statements()
if self.current_token.token_code == EToken.END:
print("")
sys.exit(0)
elif self.current_token.token_code == EToken.ERROR:
self.error()
def statements(self):
while self.current_token is not None and self.current_token.token_code != EToken.END:
self.statement()
if self.has_error:
break
if self.current_token.token_code == EToken.SEMICOL:
self.next_token()
# Flush operations at the global scope when a statement ends
self.flush_operations(self.operations[0])
def flush_operations(self, ops_list):
while ops_list:
op = ops_list.pop(0)
print("ADD" if op == EToken.PLUS else "SUB" if op == EToken.MINUS else "MULT" if op == EToken.MULT else "ASSIGN")
def statement(self):
if self.current_token.token_code == EToken.ID:
var_name = self.current_token.lexeme
self.next_token()
if self.current_token.token_code == EToken.ASSIGN:
self.next_token()
print(f"PUSH {var_name}")
self.expr()
if not self.has_error:
# Append ASSIGN operation to the global scope list
self.operations[0].append(EToken.ASSIGN)
else:
self.error()
elif self.current_token.token_code == EToken.PRINT:
self.next_token()
if self.current_token.token_code == EToken.ID:
print(f"PUSH {self.current_token.lexeme}")
print("PRINT")
self.next_token()
else:
self.error()
else:
self.error()
def expr(self):
self.term()
while self.current_token.token_code in [EToken.PLUS, EToken.MINUS]:
operator = self.current_token.token_code
self.next_token()
self.term()
if not self.has_error:
# Append operation to the most recent list (current scope)
self.operations[-1].append(operator)
def term(self):
self.factor()
while self.current_token.token_code == EToken.MULT:
self.next_token()
self.factor()
if not self.has_error:
print("MULT") # MULT operations are printed immediately
def factor(self):
if self.current_token.token_code in [EToken.INT, EToken.ID]:
print(f"PUSH {self.current_token.lexeme}")
self.next_token()
elif self.current_token.token_code == EToken.LPAREN:
self.next_token()
# Start a new list for the new level of parentheses
self.operations.append([])
self.expr()
if self.current_token.token_code == EToken.RPAREN:
self.next_token()
# Flush operations for the current level of parentheses and remove the list
self.flush_operations(self.operations.pop())
else:
self.error()
else:
self.error()
def error(self, message="Syntax error"):
if not self.has_error:
self.has_error = True
print(message)
sys.exit(1)

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

Database And Expert Systems Applications Dexa 2021 Workshops Biokdd Iwcfs Mlkgraphs Al Cares Protime Alsys 2021 Virtual Event September 27 30 2021 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Anna Fensel ,Jorge Martinez-Gil ,Lukas Fischer

1st Edition

3030871002, 978-3030871000

More Books

Students also viewed these Databases questions

Question

What could government do to reduce structural unemployment?

Answered: 1 week ago

Question

What region in IR3 is represented by the following inequalities?

Answered: 1 week ago