Question: Write a program that recursively parses expressions, input as strings, from the following recursively defined language and calculates and prints out the answer to

Write a program that recursively parses expressions, input as strings, from the following recursively defined

Write a program that recursively parses expressions, input as strings, from the following recursively defined language and calculates and prints out the answer to the calculations. Legal expressions in this language involve putting the operator before its arguments (this is called Polish notation). = * |T 0|1|2|3|4| 5 | 6 | 7 | 8 | 9 |A|B|C = Instead of writing 3*2, in this language you write *32 (which evaluates to 6). EXP stands for expressions. * means multiply the two digits that follow (after evaluation) so *24 is 8 and *5*34 is 60 as it first multiples 3 and 4 to get answer 12 then multiplies 5 and 12 to get 60. Tn means add the next 3 numbers from n (so eg T3 means 3+4+5=12, T7 means 7+8+9-24), DIGIT gives a way to express numbers up to 12 as a single digit (so A means 10, B means 11, C means 12). Only single digits are allowed and spaces are not allowed. Further legal expressions can be seen below. An example run of the program (characters in bold are typed in by the user and pop-up boxes may be used for input and output): Please input the expression B The answer is 11 Another example run: Please input the expression T4 The answer is 15 Another example run: Please input the expression T*23 The answer is 21 Another example run: Please input the expression T*2*1T2 The answer is 57. Another example run: Please input the expression *2*3*A5 The answer is 300 Make sure you split the program in to multiple methods, and use a series of recursive methods following the structure of the recursive definition about expressions. You must not use an explicit loop at all in your program. Comment your program with useful comments that give useful information, with every method commented with what it does, use indentation consistently and ensure that your variable names convey useful information. Your variables should be positioned so that their scope is small.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Below is an example program in C that implements the recursive parsing and calculation of expressions in the specified language include include Function prototypes int parseEXPconst char input int parseDIGITconst char input Method to handle addition T int parseTconst char input Skip the T character input Parse and sum the next three operands int result parseEXPinput parseEXPinput parseEXPinput Return the result return result Method to handle multiplication int parseEXPconst char input char op input if op Multiply the two operands return parseDIGITinput parseEXPinput else if op T Handle addition T return parseTinput else Handle single digit operand return op 0 Method to handle single digit operands 09 AC int parseDIGITconst char input char digit input if isdigitdigit return digit 0 else return digit A 10 A is 10 B is 11 C is 12 Main program int main Get input from the user stdcout Please input the expression ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!