Question: A11 code: import java.io.FileReader; import java.io.BufferedReader; import java.util.Set; import java.util.HashSet; public class A11 { //check whether the char is a letter static boolean isLetter(int character)

 A11 code: import java.io.FileReader; import java.io.BufferedReader; import java.util.Set; import java.util.HashSet; public

A11 code:

import java.io.FileReader;

import java.io.BufferedReader;

import java.util.Set;

import java.util.HashSet;

public class A11 {

//check whether the char is a letter

static boolean isLetter(int character) {

return (character >= 'a' && character = 'A' && character

}

// check whether the char is a letter or digit

static boolean isLetterOrDigit(int character) {

return isLetter(character) || (character >= '0' && character

}

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

}

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

char next_char=code.charAt(i);

if (state.contentEquals("INIT")){

if (isLetter(next_char)){

state="ID"; // go to the ID state

token=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 state

token=token+next_char;

} else { // end of ID state

identifiers.add(token);

token="";

state="INIT";

}

}

}

return identifiers;

}

public static void main(String[] args) throws Exception{

Set ids=getIdentifiers("A1.tiny");

for (String id:ids) System.out.println(id);

}

}

Test case1:

INT f2(INT x, INT y ) BEGIN z := x*x - y*y; RETURN z; END INT MAIN F1() BEGIN INT x; READ(x, "A41.input"); INT y; READ(y, "A42.input"); INT z; z := f2(x,y) + f2(y,x); WRITE (z, "A4.output"); END
You will write a method that picks out the identifiers from a text file. For example, given a sample input: INT f2 ( INT x , INT y ) BEGIN z:=xxyy; RETURN z ; END INT MAIN F1 ( ) BEGIN INT x; READ(x,"A41. in p ut" ) INT y ; READ ( y ,"A42 . in p ut") 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. 4 A11: 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 A11.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 letters or digits until it sees a character other than a 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

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!