Warm-up with Java programming. Get familiar with regular expression. Learn to read grammar of a language. Understand the wide application of regular expression.
You will write a method that pick out the identifiers from a text file. Here are the sample input . Your method should return a set of identifers that consists of {f2, x,y,z,F1}. Please note that in this sample program the following are not counted as identifiers:
In this assignment you can suppose that there are no comments in the input programsi writtin in TINY language.
You will write two different programs to do this:
Your programs should be able to run by typing:
%javac A11.java %java A11 A1.tiny %javac A12.java %java A12 A1.tinyThe starter code for A11 is
The starter code for A12 isimport java.io.FileReader; import java.io.BufferedReader; import java.util.Set; import java.util.HashSet; public class A11 { static boolean isLetter(int character) { return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'); } static boolean isLetterOrDigit(int character) { return isLetter(character) || (character >= '0' && character <= '9'); } public static Set getIdentifiers(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; while ((line = br.readLine()) != null) { int i=0; while (i < line.length()) { if (line.charAt(i)=='\"'){ // throw away quoted strings if (isLetter(line.charAt(i))){ // get the identifier } return identifiers; } } } public static void main(String[] args) throws Exception{ Set ids=getIdentifiers("A1.tiny"); for (String id :ids) System.out.println(id); } }
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[] 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; //Pattern idPattern = ......; //Pattern quotedStringPattern = .....; 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; } public static void main(String[] args) throws Exception{ Set ids=getIdRegex("A1.tiny"); for (String id :ids) System.out.println(id); } }
import java.util.regex.*; public class RegexTest { public static void main(String args[]) { String pattern = "\\d{4}-(0?[1-9]|1[012])-\\d{2}"; String text = "final exam 2008-04-22, or 2008-4-22, but not 2008-22-04"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); while (m.find()) { System.out.println("valid date:"+text.substring(m.start(), m.end())); } } }
yourMark=0; if (A11.java, A12.java are not sent properly) return; for (each of A11, A12) if (it is compiled correctly) yourMark+=1; for (each of A11, A12){ if (your java program reads A1.tiny && generates corrent results) for (each of the 6 tests cases) if (it is correct) yourMark+=0.5; if (youCode.length() among the top 6 students) yourMark+=0.5; } for (each day of your late submission) yourMark=yourMark*0.8;