Question: Java code ------------------------------------ public static Set getIdRegex(String filename) throws Exception{ String[] keywordsArray = { IF, WRITE, READ, RETURN, BEGIN,END, MAIN, INT, REAL }; Set keywords
![Java code ------------------------------------ public static Set getIdRegex(String filename) throws Exception{ String[]](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f961c71d0eb_52666f961c677442.jpg)



Java code
------------------------------------
public static Set
FileReader reader = new FileReader(filename); BufferedReader br = new BufferedReader(reader); String line; // complete the regular exp here // Pattern idPattern = Pattern.compile(....); // complete the regular expression here // Pattern quotedStringPattern = Pattern.compile(....); while ((line = br.readLine()) != null) { Matcher m_quotedString = quotedStringPattern.matcher(line); String lineWithoutQuotedStrings = m_quotedString.replaceAll(""); Matcher m = idPattern.matcher(lineWithoutQuotedStrings); while (m.find()) { String id = line.substring(m.start(), m.end()); if (!keywords.contains(id)) identifiers.add(id); } } return identifiers; }
- Fill in the missing lines that are commented out.
- Make sure the method name is getIdRegex
3 Assignment specification Your task is to pick-up identifiers in programs written in our TINY language. Click on the link (the underlined) for the grammar of the Tiny language. Identifier An identifier consists of a letter followed by any number of letters or digits. The following are examples of identifiers: x, x2, xx2, 2x, End, END2. Note that End is an identifier while END is a keyword in the Tiny language. The following are not identifiers: IF, WRITE, READ, .... (keywords are not counted as identifiers) 2x (identifier can not start with a digit) Strings in comments are not identifiers. You will write a method that pick out the identifiers from a text file. For example, given a sample input: INT f2 (INT X, INT y ) BEGIN z := X*X - y*y; RETURN z; END INT MAIN F1() BEGIN INT ; READ(x, "A41.input"); INT y; READ(y, "A42. input"); INT Z; z := f2(x,y) + f2(y,x); WRITE (z, "A4.output"); END Your code should return a set of identifiers that consists of {f2, x, y, z, F1}. Please note that in this sample input program, the following are not counted as identifiers: A41, input, output: they are quoted hence they are not treated as identifiers; . INT, READ etc.: They are keywords used in our TINY language hence they should not be picked up. Here are a few test cases for the assignment: case 1, case 2, case 3, case 4, case 5, case 6. For those cases, their corresponding ID counts are 5, 4, 6, 7, 8, 9, respectively. To make your task simpler, In this assignment you can suppose that there are no comments in the input program. You will write two different programs to do this as specified below. 3.2 A12 Program A12.java will use regular expression in java.util.regex. Here is a tutorial for Java regex. There are many ways to solve the problem. One approach is described in the starter code below, it read the code line by line. In each line it find quoted strings and replace them with empty string. Then find identifiers and put them into a set if they are not keywords. import java.io.*; import java.util. HashSet; import java.util.Set; import java.util.regex .*; public class A12 { public static Set
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
