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[]

keywordsArray = { "IF", "WRITE", "READ", "RETURN", "BEGIN","END", "MAIN", "INT", "REAL" };

Set keywords = new HashSet(); Set identifiers = new HashSet(); for (String

s : keywordsArray) keywords.add(s); FileReader reader = new FileReader(filename); BufferedReader br =

Java code

------------------------------------

public static Set getIdRegex(String filename) throws Exception{ String[] keywordsArray = { "IF", "WRITE", "READ", "RETURN", "BEGIN","END", "MAIN", "INT", "REAL" }; Set keywords = new HashSet(); Set identifiers = new HashSet(); for (String s : keywordsArray) keywords.add(s);

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; }

  1. Fill in the missing lines that are commented out.
  2. 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 getIdRegex(String filename) throws Exception { String [] keywords Array = { "IF", "WRITE", "READ", "RETURN", "BEGIN","END", "MAIN, "INT", "REAL" }; Set keywords = new HashSet(); Set identifiers = new HashSet(); for (Strings : keywords Array) keywords.add(s); File Reader reader = new File Reader (filename); 3 5 BONUS MARK Buffered Reader br = new Buffered Reader (reader); String line ; //Pattern id Pattern = ......; //Pattern quotedString Pattern = ..... while ((line = br.readLine()) != null) { Matcher m-quotedString = quoted String Pattern. matcher (line); String line Without Quoted Strings = m-quotedString.replace All(""); Matcher m = idPattern.matcher (line Without QuotedStrings); while (m. find()) { String id = line.substring(m.start(), m.end()); if (! keywords. contains (id)) identifiers.add(id); } } return identifiers; } public static void main(String[] args) throws Exception { Set ids=getIdRegex ("A1.tiny"); for (String id :ids) System.out.println(id); } } 4 Marking Scheme your Mark=0; if (A11.java, A12. java are not sent properly) return; for (each of A11, A12) if (it is compiled correctly) your Mark+=1; for (each of All, A12) { if (your java program reads Al.tiny && generates corrent results) for (each of the 6 tests cases) if (it is correct) your Mark+=0.5; if (youCode . length() among the top 6 students) your Mark+=0.5; } for (each day of your late submission) yourMark=yourMark *0.8; 5 Bonus Mark If your code is among the top six in terms of length, you will receive 0.5 point for A11 or A12

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!