Question

Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

Homework description::::: Write JAVA program with following description. Sample output with code will be helful...

A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code.

To do this, you should make use of a dictionary. The dictionary will hold all the identifiers that you find in the Java program and the number of times the identifier appears in the program.

In addition, your program will need a data structure containing all the Java reserved words and operators. Write Java code that will create a balanced binary tree of the reserved words and operators in the Java language. A listing of reserved words and operators is to be read from the file, JavaKeywords.txt; the name of the program to be analyed is supplied as a command line argument.

Whenever your program encounters a token, it should search the binary search tree of reserved words. If the token is not a reserved word, then search the dictionary of identifiers. If the token is not in either dictionary, add it to the dictionary of identifiers. If the token appears in the identifiers dictionary, increment the frequency counter for that entry.

Use object-oriented program design for this project. Follow style and documenting examples given in class for earlier projects. Each file must begin with a comment block with your name, program name, course number, and due date. The block must also include the purpose, input, and output of the program.

Input Your program must be able to retrieve the names of the input files from the command line. The first file name is JavaKeywords.txt and the second is the name of the program to be parsed for identifiers.

Output Your program is to display an inorder traversal of the tree containing the Java reserved words and operators (one per line) along with labeled information regarding the height and number of nodes in the tree; followed by a list of the identifiers and the number of times each of them appears in the test program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Here we are importing files that we need for our program

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class Identifiers {
  
public static void main(String[] args) throws IOException {

// reading file here
BufferedReader reader = new BufferedReader(new FileReader(new File("javaKeywords.txt")));
String presentLine = null;
Map ReservedDictionary = new HashMap();
  
while((presentLine = reader.readLine()) != null) {reading each line and splitting and checking
// Split the input line.
String[] tokens = presentLine.split("\\s+");
  
// Ignore empty lines.
if(presentLine.equals(""))
continue;
String[] identifiers = {"char", "boolean", "byte", "short","int","long","float","double"};

for(String token: tokens) {
token = token.replace(".", "");
token = token.replace(",", "");
for(int z=0;z<identifiers.length;z++){
if(token==identifiers[z]){ // checking whether identifier or not
if(ReservedDictionary.containsKey(token)) { //checking token
Integer val = ReservedDictionary.get(token);
ReservedDictionary.put(token, val + 1); //otherwise we are pushing to reserved dictionary
}
}
}
}
}
  
// Printing all identifiers stored in the ReservedDictionary
for(String key: ReservedDictionary.keySet())
System.out.println(key + ": " + ReservedDictionary.get(key));
reader.close(); //closing the file
}
}

output

key : public 4

key :class 1

key: static 1

Add a comment
Know the answer?
Add Answer to:
Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase...

    Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase of program compilation Assignment: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a Java, program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described...

  • Write a Java program that creates and manipulates a directory of names, telephone numbers. The following...

    Write a Java program that creates and manipulates a directory of names, telephone numbers. The following information will be stored for each person in the directory: - Name (Last, First) - Home telephone number You should keep the entire collection ordered by key value (the combination of last and first names). Your program should be able to perform the following basic functions: - Search and display the contents of a particular entry - Display the entire directory - Delete an...

  • Would you please do Pseudocode for this progarm in Java?? Using scanner algorithm, The scanner takes...

    Would you please do Pseudocode for this progarm in Java?? Using scanner algorithm, The scanner takes the name of a text file from the command line and prints out the list of tokens. If there is non-valid token in the input file, print out “error”. please help me to write Pseudocode! thank you!! The scanner takes the name of a text file from the command line. It outputs to the console error if there is any non-valid token in the...

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

  • *Java* Given the attached Question class and quiz text, write a driver program to input the...

    *Java* Given the attached Question class and quiz text, write a driver program to input the questions from the text file, print each quiz question, input the character for the answer, and, after all questions, report the results. Write a Quiz class for the driver, with a main method. There should be a Scanner field for the input file, a field for the array of Questions (initialized to 100 possible questions), and an int field for the actual number of...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT