Question: Write a recursive program in Java that can interpret an imaginary markup language. The input to the program will be a text file that contains
Write a recursive program in Java that can interpret an imaginary markup language. The input to the program will be a text file that contains text embedded with several markup tags. The output from the program will be a text file that contains the formatted text resulting from the application of the markup tags. The syntax of the markup language is straightforward. Each command starts with a number sign (#) in order to differentiate it from the actual text. You may assume no number signs will be part of the actual text. Each command specifies a format that should be applied to the actual text until the command #END is encountered. Trim any leading or trailing spaces in the text when applying a markup command to it. Note that multiple markup commands may be applied at the same time with the innermost markup applied first. All active markup commands are terminated whenever a #END is encountered. The markup commands are as follows: Markup Definition Input Text Formatted Text #REV Reverse the text Apple elppA #QUO Surround the text with quotation marks Apple "Apple" #NXT Replace each letter in the text with the next letter of the alphabet. Keep the case the same, and ignore digits and special characters. Wrap Z to A. Apple Bqqmf #DBL Remove the second letter in any set of double letters Apple Aple #END Stop applying all active markup commands To illustrate the use of the markups, consider the following example input line. It contains several markup commands. This #REV is #QUO a #NXT sample #END line #DBL #NXT Tennessee #END. Processing the markups in the input text would cause the following output to be generated. This "fmqnbt a" si line Ufoftf. Your program should read a text file containing several lines similar in format to the previous example. Your program should produce a text file containing the correct output for the text. As stated earlier, this program must use recursion. You should have a recursive method that accepts a string argument and returns a string argument. One approach to solving this problem is to tokenize the input string and print each token until one is encountered that starts with a #. From there, search for the next #END command. Apply the command to all of the text between the command and the #END recursively (up to and including the #END command). Also, recursively process the text after the #END command using all of the remaining text after the #END command. See the following pseudocode for more information.
String recursion (String text) String output = Find first command, cmd, in text by searching for next number sign If there is no number sign output = text Else output = substring from beginning of text up to cmd Find first stop command, stopcmd, after next number sign Let cmdtext be the substring between cmd and stopcmd (including stopcmd itself) Let remainingtext be the substring after stopcmd //Decode cmd If (cmd is #REV) output = output + reverse (recursion(cmdtext)) else if (cmd is #QUO) output = output + quote(recursion(cmdtext)) else if (cmd is #NXT) output = output + next(recursion(cmdtext)) else if (cmd is #DBL) output = output + double(recursion(cmdtext)) End if output = output + recursion(remainingtext) End if Return output Finally, the name of your main class must be Assignment3 which is contained in a file called Assignment3.java. Your program should prompt for and input the names of the input and output text files. It is okay if Canvas appends characters to your file name (e.g. Assignment3-1.java).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
