Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a RUST program, I need it by today midnight, please help! Implement a calculator for arithmetic programs written in RPN style, as specified

This is aimage text in transcribedimage text in transcribed RUST program, I need it by today midnight, please help!

Implement a calculator for arithmetic programs written in RPN style, as specified by the following grammar: Binary Operators b ::= + | - | */ Terms t := n 1/ 32-bit signed integers, e.g., -1, 2, 256, 0, ... | b // Binary operator save // Pop from the main stack, pushing the value onto an auxiliary stack | restore // Pop from the auxiliary stack, pushing the value onto the main stack RPN Programs p ::= t_ t_1 ... t_n done That is, a valid RPN program is a series of n terms, to t_1 ... t_n, followed by the keyword done. Both the example programs given above are valid according to this grammar. The special commands save and restore allow the programmer to save values from the calculator's main stack onto an auxiliary stack for later computation. Here's a program that calculates the sum of two numbers, then uses save and restore to subtract the result from 0. 7 8 + save o restore - done with stack trace: Operation Main Stack Auxiliary Stack push 7 [7] push 8 [7; 8] op + [15] save 15 0) [15] push 0 [0] [15] restore 15 [0, 15] [ op - [-15] 0 The special term done must appear at the end of every valid RPN program, and has the effect of printing out the top value on the main stack (if any), then terminating the program. It's an error if done is encountered with no values on the stack. Examples Program Result 1 2 3 done 3 1 2 + 3 + 4 + 5 + done 15 1 2 3 4 5 + + + + done 15 1 2 + save 3 restore + done 6 1 2 + done 3 4 5 done 3 10 2 / save 7 restore - done 2 Specifics 1. Write a program, /rpn/src/main.rs, that reads RPN programs on stdin and prints their results on stdout , assuming the keyword done is encountered. Your program may assume, as in the grammar above, that the terms in each RPN program are whitespace- separated. 2. When printing the result of a valid RPN program, include no newline characters (hint: use print! instead of println!). 3. On valid RPN programs, your RPN interpreter should return exit code e. On invalid RPN programs (those that attempt to perform operations without sufficient arguments for example), your interpreter should return with a non-zero exit code. 4. On or before the due date, commit your final submission to your GitHub repo for this assignment. Implement a calculator for arithmetic programs written in RPN style, as specified by the following grammar: Binary Operators b ::= + | - | */ Terms t := n 1/ 32-bit signed integers, e.g., -1, 2, 256, 0, ... | b // Binary operator save // Pop from the main stack, pushing the value onto an auxiliary stack | restore // Pop from the auxiliary stack, pushing the value onto the main stack RPN Programs p ::= t_ t_1 ... t_n done That is, a valid RPN program is a series of n terms, to t_1 ... t_n, followed by the keyword done. Both the example programs given above are valid according to this grammar. The special commands save and restore allow the programmer to save values from the calculator's main stack onto an auxiliary stack for later computation. Here's a program that calculates the sum of two numbers, then uses save and restore to subtract the result from 0. 7 8 + save o restore - done with stack trace: Operation Main Stack Auxiliary Stack push 7 [7] push 8 [7; 8] op + [15] save 15 0) [15] push 0 [0] [15] restore 15 [0, 15] [ op - [-15] 0 The special term done must appear at the end of every valid RPN program, and has the effect of printing out the top value on the main stack (if any), then terminating the program. It's an error if done is encountered with no values on the stack. Examples Program Result 1 2 3 done 3 1 2 + 3 + 4 + 5 + done 15 1 2 3 4 5 + + + + done 15 1 2 + save 3 restore + done 6 1 2 + done 3 4 5 done 3 10 2 / save 7 restore - done 2 Specifics 1. Write a program, /rpn/src/main.rs, that reads RPN programs on stdin and prints their results on stdout , assuming the keyword done is encountered. Your program may assume, as in the grammar above, that the terms in each RPN program are whitespace- separated. 2. When printing the result of a valid RPN program, include no newline characters (hint: use print! instead of println!). 3. On valid RPN programs, your RPN interpreter should return exit code e. On invalid RPN programs (those that attempt to perform operations without sufficient arguments for example), your interpreter should return with a non-zero exit code. 4. On or before the due date, commit your final submission to your GitHub repo for this assignment

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

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions