Question
Your problem is to convert a given infix expression into a sequence of assembly instructions that evaluates the expression and leaves the result in the
Your problem is to convert a given infix expression into a sequence of assembly instructions that evaluates the expression and leaves the result in the register. You will do this by using postfix expressions. First, you will convert a given infix expression into the corresponding postfix expression. In the second step, you will convert postfix to the required sequence of assembly instructions. You will read the infix expression from a file, and write the result to another file. For example, given the following infix expression: ( ( A + ( B * C ) ) / ( D - E ) ) we get the postfix expression: A B C * + D E - / This results in an assembly program that looks like:
Opcode | Operand | Comment |
LOAD | B | Load in B. |
MULR | C | B * C. |
STOR | TMP1 | Save results of B * C. |
LOAD | A | Load A. |
ADDR | TMP1 | Add A to B * C. |
STOR | TMP2 | Save result. |
LOAD | D | Load D. |
SUBR | E | D - E. |
STOR | TMP3 | Save result. |
LOAD | TMP2 | Get A + B * C. |
DIVR | TMP3 | Divide it by D - E. |
STOR | TMP4 | Save result, also still in register. |
Your program output should be:
Infix Expression: ( ( AX + ( BY * C ) ) / ( D4 - E ) ) Postfix Expression: AX BY C * + D4 E - / LOAD BY MULR C STOR TMP1 LOAD AX ADDR TMP1 STOR TMP2 LOAD D4 SUBR E STOR TMP3 LOAD TMP2 DIVR TMP3 STOR TMP4
Requirements:
You CANNOT use std::string and must construct your stack using a linked list you build.
Implementation:
Create a generic (template) ADT stack class.
Implement using a linked list.
You must implement a destructor, copy constructor, constant time swap, and assignment.
You will need to have a stack of strings.
Use the following files as a starting point:
-----------------------------------------------------------------------
stack.hpp
-----------------------------------------------------------------------
#ifndef STACK_HPP #define STACK_HPP #includeconst int MAX_VALUES = 256; template class stack { private: int tos; type values[MAX_VALUES]; public: stack() : tos(-1) {} /** @pre non-full stack */ void push(const type & object) { assert(!full()); ++tos; values[tos] = object; } /** @pre non-empty stack */ void pop() { assert(!empty()); --tos; } /** @pre non-empty stack */ type top() const { assert(!empty()); return values[tos]; } /** @pre non-empty stack */ type & top() { assert(!empty()); return values[tos]; } bool empty() const { return tos == -1; } bool full() const { return tos == (MAX_VALUES - 1); } }; #endif
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