Question: // Parser.java - supplied code for expression parser // Author: ????? // Date: ????? // Class: CS165 // Email: ????? import java.util.*; import java.util.stream.Collectors; /**
// Parser.java - supplied code for expression parser // Author: ????? // Date: ????? // Class: CS165 // Email: ????? import java.util.*; import java.util.stream.Collectors; /** * The Lexer has several different versions of a parsing method for expressions. *
* The first method is called {@code scannerLexer}, and is based on using a {@link Scanner}. * The second method is called {@code splitLexer}, which uses {@link String#split(String)}. * The third method is call {@code tokenizerLexer} and is based on an object called {@link StringTokenizer}. *
* created by Chris Wilcox Spring17 * modified by {@code rbecwar} and {@code garethhalladay} Fall17 */ public class Lexer { /** * Lex a String using an instance of Scanner. *
* Initialize a {@code Scanner} object with a {@code String} argument containing the expression. * Using the {@link Scanner#hasNext()} and {@link Scanner#next()} methods: *
*
Parse the {@code String} (using whitespace as your delimiter) *
Add each element to the {@code ArrayList} *
*
* After implementing this approach, examine the results. *
* This approach seems to have trouble unless there is white space between every token. * Is this fixable or is the Scanner approach not a good idea? * @param expr a String in the form of an expression * @return a list of tokens */ public static List scannerLexer(String expr) { // Allocate list ArrayList tokens = new ArrayList<>(); // YOUR CODE HERE Scanner scnr = new Scanner(expr); for(int i = 0; i * Invoke the {@code String.split(String)} method on the String containing the expression. * Remember that split returns an array of strings that you must iterate. * Note that the method takes a regular expression, which we have not studied yet. As an introduction, * you should check out the provided links and try to understand what the following expressions are * matching. *
* Try passing the regular expression {@code "[-+*()/]"} the split method to see what happens. * It seems to match everything, but it removes the operators and parentheses. * Is the {@code String.split(String)} approach fixable? *
* Hint: Try this regular expression: {@code "(?<=[-+*()/])|(?=[-+*()/])"}. * This uses the lookahead and lookbehind operators, an advanced regex feature. * * Use the {@link String#trim()} method to remove whitespace in each token. * * @param expr a string in the form of an expression * @return a list of tokens * @see String#split(String) */ public static List splitLexer(String expr) { // Allocate list List tokens = new ArrayList<>(); // YOUR CODE HERE return tokens; } /* Consult the Regular Expressions section of the documentation and answer the questions using regular expressions. Write the pattern you used below. Show the TA the output of your grep commands for completion. * How many times have you called the `print` command (not printf or println)? * How many times have you written a single line comment (one that begins with //)? * How many times have you written a single line comment on the same line as a line of code (such as `int foo = 5 //assign 5 to foo`) * How many times have you written a for loop or a while loop? * How many times have you written a for loop or while loop and not included a space between `for` and the first `(`? * How many times have you written a for each loop (such as `for (Movie m : movies) {`)? * OPTIONAL BONUS: How many times have you written a for loop that used `i` as the incrementor (such as `for (int i = 0; i < 10; i++) {`)? */ }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
