Answered step by step
Verified Expert Solution
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 ;
print bigvalue;
end
where my code returns
PUSH bigvalue
PUSH
PUSH
ADD
PUSH
PUSH
PUSH
MULT
PUSH
ADD
ADD
MULT
PUSH
PUSH
PUSH
PUSH
PUSH
ADD
MULT
PUSH
PUSH
PUSH
MULT
ADD
ADD
SUB
ADD
SUB
PUSH
PUSH
MULT
PUSH
PUSH
MULT
PUSH
ADD
ADD
PUSH
MULT
PUSH
PUSH
PUSH
PUSH
PUSH
PUSH
PUSH
MULT
PUSH
PUSH
PUSH
MULT
MULT
SUB
SUB
ADD
MULT
ADD
ADD
ADD
SUB
ASSIGN
PUSH bigvalue
PRINT
but the correct output should be
PUSH bigvalue
PUSH
PUSH
ADD
PUSH
PUSH
PUSH
MULT
PUSH
ADD
ADD
MULT
PUSH
PUSH
PUSH
PUSH
PUSH
ADD
MULT
PUSH
PUSH
PUSH
MULT
SUB
ADD
SUB
ADD
ADD
PUSH
PUSH
MULT
PUSH
PUSH
MULT
PUSH
ADD
ADD
PUSH
MULT
PUSH
PUSH
PUSH
PUSH
PUSH
PUSH
PUSH
MULT
PUSH
PUSH
PUSH
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 initself lexer:
self.lexer lexer
self.currenttoken None
self.nexttoken
self.haserror False
# Initialize with a list containing an empty list for global scope operations
self.operations
def nexttokenself:
self.currenttoken self.lexer.getnexttoken
def parseself:
while self.currenttoken.tokencode EToken.END and not self.haserror:
self.statements
if self.currenttoken.tokencode EToken.END:
print
sysexit
elif self.currenttoken.tokencode EToken.ERROR:
self.error
def statementsself:
while self.currenttoken is not None and self.currenttoken.tokencode EToken.END:
self.statement
if self.haserror:
break
if self.currenttoken.tokencode EToken.SEMICOL:
self.nexttoken
# Flush operations at the global scope when a statement ends
self.flushoperationsselfoperations
def flushoperationsself opslist:
while opslist:
op opslist.pop
printADD if op EToken.PLUS else "SUB" if op EToken.MINUS else "MULT" if op EToken.MULT else "ASSIGN"
def statementself:
if self.currenttoken.tokencode EToken.ID:
varname self.currenttoken.lexeme
self.nexttoken
if self.currenttoken.tokencode EToken.ASSIGN:
self.nexttoken
printfPUSH varname
self.expr
if not self.haserror:
# Append ASSIGN operation to the global scope list
self.operationsappendETokenASSIGN
else:
self.error
elif self.currenttoken.tokencode EToken.PRINT:
self.nexttoken
if self.currenttoken.tokencode EToken.ID:
printfPUSH selfcurrenttoken.lexeme
printPRINT
self.nexttoken
else:
self.error
else:
self.error
def exprself:
self.term
while self.currenttoken.tokencode in ETokenPLUS, EToken.MINUS:
operator self.currenttoken.tokencode
self.nexttoken
self.term
if not self.haserror:
# Append operation to the most recent list current scope
self.operationsappendoperator
def termself:
self.factor
while self.currenttoken.tokencode EToken.MULT:
self.nexttoken
self.factor
if not self.haserror:
printMULT # MULT operations are printed immediately
def factorself:
if self.currenttoken.tokencode in ETokenINT, EToken.ID:
printfPUSH selfcurrenttoken.lexeme
self.nexttoken
elif self.currenttoken.tokencode EToken.LPAREN:
self.nexttoken
# Start a new list for the new level of parentheses
self.operations.append
self.expr
if self.currenttoken.tokencode EToken.RPAREN:
self.nexttoken
# Flush operations for the current level of parentheses and remove the list
self.flushoperationsselfoperations.pop
else:
self.error
else:
self.error
def errorself message"Syntax error":
if not self.haserror:
self.haserror True
printmessage
sysexit
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started