Question
Java Code -------------------------------------------------------------------------------------------- static Set getIdentifiers(String filename) throws Exception{Set identifiers = new HashSet();String state=INIT; // Initially it is in the INIT state.StringBuilder code = new
Java Code
--------------------------------------------------------------------------------------------
static Set getIdentifiers(String filename) throws Exception{Set identifiers = new HashSet();String state="INIT"; // Initially it is in the INIT state.StringBuilder code = new StringBuilder();BufferedReader br = new BufferedReader(new FileReader(filename));String line;while ((line = br.readLine()) != null) {code=code.append(line+"");} // read the text line by line.code =code.append('$'); //add a special symbol to indicate the end of file.
int len=code.length();String token="";for (int i=0; i
if (state.contentEquals("INIT")){if (isLetter(next_char)){state="ID"; // go to the ID statetoken=token+next_char;} //ignore everything if it is not a letter}else if (state.equals("ID")) {if (isLetterOrDigit(next_char)) { //take letter or digit if it is in ID statetoken=token+next_char;} else { // end of ID stateidentifiers.add(token);token="";state="INIT";}
}
}return identifiers;}
????
3.1 All: coding from scratch The first approach is the accomplish the task from scratch without using any tools. This approach also motivates the introduction of DFA in Assignment 2. Program Al1.java is not supposed to use regular expressions, not regex package, not any methods involving regular expression in String class or other classes. Your program should use the most primitive method, i.e. look at characters one by one, and write a loop to check whether they are quoted strings, identifiers, etc. A simplified version of the algorithm can be depicted by Algorithm 20. It gets a set of identifiers from an input string x. The algorithm starts with the initial ("INIT") state, and scans the characters one by one. Once it sees a letter, it goes to the "ID" state. In the "ID" state, it expects to see more letter or digits, until it sees a character other than letter or digit. At this point, it exits the "ID" states, and goes back to the initial state "INIT". The algorithm needs to be expanded to deal with quoted strings and keywords. For quoted strings, you can remove them first before you pick the identifiers. For keywords, you can check whether a token belongs to the keyword set before adding into the identifiers set. Input: An input string r. Output: a set of identifiers in x 1 state="INIT"; 2 token="" ; 3 identifiers={} ; 4 while (c=nextChar())!=end.of_stringx do if c isLetter then state="ID"; 7 token =token+c; 8 end 9 if state is "ID" then if c is letter or digit then state="ID"; token=token+c; 10 11 12 end else 13 14 add token to identifiers; 15 16 token="": 17 state="INIT" ; 18 end 19 end 20 end Algorithm 1: The algorithm for obtaining identifiers from an input string. We provide the starter code for All as follows. You need to expand it to deal with quoted string and keywords. import java.io. FileReader; import java.io.Buffered Reader; import java.util. Set; import java.util. HashSet; public class Al1 { //check whether the char is a letter static boolean isLetter (int character) { return (character >= } a && character = 'A' && character = '0' && character
Step by Step Solution
3.35 Rating (161 Votes )
There are 3 Steps involved in it
Step: 1
The solutio...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started