Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use java to complete the missing part of the following code: A11: import java.io.FileReader; import java.io.BufferedReader; import java.util.Set; import java.util.HashSet; public class A11 { static

Use java to complete the missing part of the following code:

A11:

import 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 = 'A' && character = '0' && character  

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

image text in transcribed

image text in transcribed

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: a x, x2, xx2, x2x, 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 62 (INT X, INT y ) BEGIN z != X*X ; RETURN 2; END INT MAIN F1 BEGIN INT X; READ(x, "A41.input"); INT : READ(y, "A42. input"); INT ; 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 1, 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.1 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 2. The algorithm starts with the initial ("INIT") state, and scans the characters one by one. Once it secs 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. 5 6 1 7 8 9 10 Input: An input string r. Output: a set of identifiers in 2 1 state="INIT"; 2 token=""; 3 identifiers={}; 4 while (c=next Char())!=end-of-string-u do if c isLetter then state="ID"; token=token+c; end if state is "ID" then if c is letter or digit then state="ID"; token=tokente; end else add token to identifiers; token=""; state="INIT"; end end 20 end Algorithm 1: The algorithm for obtaining identifiers from an input string. 11 12 13 14 15 16 17 18 19

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

What factors might influence a firms price-earnings ratio?

Answered: 1 week ago

Question

Question What is a secular trust?

Answered: 1 week ago