Question

In: Computer Science

Take all the methods from this program and put it into single program and compile, and...

Take all the methods from this program and put it into single program and compile, and then test the program with some expressions.
Should have two files: Lex.java and a output.txt file


package lexicalanalyser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

enum CharacterClass{
    LETTER(0), DIGIT(1), UNKNOWN(99), EOF(-1);
    
    private int value;
    
    private CharacterClass(int value){
        this.value = value;
    }
    public int getValue(){
        return value;
    }
}

enum TokenCodes{
    INT_LIT(10), IDENT(11), ASSIGN_OP(20), ADD_OP(21),
    SUB_OP(22), MULT_OP(23), DIV_OP(24), LEFT_PAREN(25),
    RIGHT_PAREN(26), SEMI_COLUMN(27), COMP_OP_SMALL(28),
    COMP_OP_LARGE(29), COMP_OP_EQUAL(30), EOF(-1), EOL(-2);
    
    private int value;
    
    private TokenCodes(int value){
        this.value = value;
    }
    public int getValue(){
        return value;
    }
}
public class LexicalAnalyser {

    /* Global Declarations */
    /* Variables */
    static CharacterClass charClass;
    static char[] lexeme = new char[100];
    static char nextChar;
    static int count = 0;
    static int lexLen;
    static int token;
    static TokenCodes nextToken;
    static File in_fp;
    static FileReader fr;
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        /* Open the input data file and process its contents */
        try{
            in_fp = new File("front.in"); 
            fr = new FileReader(in_fp);
            getChar();
            do{
                lex();
            }while (nextToken != TokenCodes.EOF);
            System.out.println("Parsing Complete!");
        }catch (FileNotFoundException e){
            System.out.println("Error - File Not Found");
        }catch (IOException e){
            System.out.println("Error - IO Exception");
        }
        
    }
    
    /* lookup - a function to lookup operators and parentheses
                and return the token*/
    static void lookup(char ch){
        switch(ch){
            case '(':
                addChar();
                nextToken = TokenCodes.LEFT_PAREN;
                break;
            case ')':
                addChar();
                nextToken = TokenCodes.RIGHT_PAREN;
                break;
            case '+':
                addChar();
                nextToken = TokenCodes.ADD_OP;
                break;
            case '-':
                addChar();
                nextToken = TokenCodes.SUB_OP;
                break;
            case '*':
                addChar();
                nextToken = TokenCodes.MULT_OP;
                break;
            case '/':
                addChar();
                nextToken = TokenCodes.DIV_OP;
                break;
            case '=':
                addChar();
                nextToken = TokenCodes.ASSIGN_OP;
                break;
            case ';':
                addChar();
                nextToken = TokenCodes.SEMI_COLUMN;
                break;
            case '>':
                addChar();
                nextToken = TokenCodes.COMP_OP_LARGE;
                break;
            case '<':
                addChar();
                nextToken = TokenCodes.COMP_OP_SMALL;
                break;
            default:
                addChar();
                nextToken = TokenCodes.EOF;
                break;
        }
        
    }
    
    /* addChar - a function to add nextChar to lexeme */
    static void addChar(){
        if(lexLen <= 98){
            lexeme[lexLen++] = nextChar;
            lexeme[lexLen] = 0;
        }else{
            System.out.println("Error - lexeme is too long");
        }
    }
    
    /* getChar - a function to get the next character of
                 of input and determine its character class*/
    static void getChar(){
        try {
            nextChar = (char)fr.read();
            if(nextChar != 0){
                if(Character.isLetter(nextChar)){
                    charClass = CharacterClass.LETTER;
                }else if(Character.isDigit(nextChar)){
                    charClass = CharacterClass.DIGIT;
                }else{
                    charClass = CharacterClass.UNKNOWN;
                }
            }else{
                charClass = CharacterClass.EOF;
            }
        } catch (IOException ex) {
            Logger.getLogger(LexicalAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /* getNonBlank - a function to call getChar until it 
                     returns a non-whitespace character*/
    static void getNonBlank(){
        while(Character.isWhitespace(nextChar)){
            getChar();
        }
    }
    
    /* lex - a simple lexical analyzer for arithmetic
             expressions*/ 
    static void lex(){
        lexLen = 0;
        lexeme = new char[100];
        getNonBlank();
        //System.out.println("lex: c " + charClass);
        switch(charClass){
            /* Parse Identifiers */
            case LETTER:
                addChar();
                getChar();
                while(charClass == CharacterClass.LETTER ||
                        charClass == CharacterClass.DIGIT){
                    addChar();
                    getChar();
                }
                nextToken = TokenCodes.IDENT;
                break;
            /* Parse integer literals */
            case DIGIT:
                do{
                    addChar();
                    getChar();
                }while (charClass == CharacterClass.DIGIT);
                nextToken = TokenCodes.INT_LIT;
                break;
            case UNKNOWN:
                lookup(nextChar);
                //System.out.println("NextToke: " + nextToken);
                getChar();
                break;
            /* EOF */
            case EOF:
                nextToken = TokenCodes.EOF;
                lexeme[0] = 'E';
                lexeme[1] = 'O';
                lexeme[2] = 'F';
                lexeme[3] = 0;
                break;
        }
        System.out.printf("Next token is: %d, Next lexeme is %s\n",nextToken.getValue(),new String(lexeme));
    }
    
}

Solutions

Expert Solution

We are following the below approach :

1. output.txt file will be created if not exists in the current path.

2. Create FileWriter class object to write in text file.

3. If you want to append text in output file then provide true while creating FileWriter class object from output File class.

4. Change all the print statement to FileWriter_Object.write();

Please find the code snippet :

package lexicalanalyser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

enum CharacterClass{
    LETTER(0), DIGIT(1), UNKNOWN(99), EOF(-1);
    
    private int value;
    
    private CharacterClass(int value){
        this.value = value;
    }
    public int getValue(){
        return value;
    }
}

enum TokenCodes{
    INT_LIT(10), IDENT(11), ASSIGN_OP(20), ADD_OP(21),
    SUB_OP(22), MULT_OP(23), DIV_OP(24), LEFT_PAREN(25),
    RIGHT_PAREN(26), SEMI_COLUMN(27), COMP_OP_SMALL(28),
    COMP_OP_LARGE(29), COMP_OP_EQUAL(30), EOF(-1), EOL(-2);
    
    private int value;
    
    private TokenCodes(int value){
        this.value = value;
    }
    public int getValue(){
        return value;
    }
}
public class LexicalAnalyser {

    /* Global Declarations */
    /* Variables */
    static CharacterClass charClass;
    static char[] lexeme = new char[100];
    static char nextChar;
    static int count = 0;
    static int lexLen;
    static int token;
    static TokenCodes nextToken;
    static File in_fp;
    static File out_fp;
    static FileReader fr;
    static FileWriter fr_out;
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        /* Open the input data file and process its contents */
        try{
            in_fp = new File("front.in");
            out_fp = new File("output.txt");
            if (!out_fp.exists()) {
                out_fp.createNewFile();
                System.out.println("Output File created: " + out_fp.getName());
              } else {
                System.out.println("Output File already exists.");
              }
            fr = new FileReader(in_fp);
            // while creating File Writer class, provide true if you want to append in text file.
            fr_out = new FileWriter(out_fp, true);
            getChar();
            do{
                lex();
            }while (nextToken != TokenCodes.EOF);
            fr_out.write("Parsing Complete!");
        }catch (FileNotFoundException e){
                fr_out.write("Error - File Not Found");
        }catch (IOException e){
                fr_out.write("Error - IO Exception");
        }
        finally {
                fr_out.close(); 
        }
        
    }
    
    /* lookup - a function to lookup operators and parentheses
                and return the token*/
    static void lookup(char ch) throws IOException{
        switch(ch){
            case '(':
                addChar();
                nextToken = TokenCodes.LEFT_PAREN;
                break;
            case ')':
                addChar();
                nextToken = TokenCodes.RIGHT_PAREN;
                break;
            case '+':
                addChar();
                nextToken = TokenCodes.ADD_OP;
                break;
            case '-':
                addChar();
                nextToken = TokenCodes.SUB_OP;
                break;
            case '*':
                addChar();
                nextToken = TokenCodes.MULT_OP;
                break;
            case '/':
                addChar();
                nextToken = TokenCodes.DIV_OP;
                break;
            case '=':
                addChar();
                nextToken = TokenCodes.ASSIGN_OP;
                break;
            case ';':
                addChar();
                nextToken = TokenCodes.SEMI_COLUMN;
                break;
            case '>':
                addChar();
                nextToken = TokenCodes.COMP_OP_LARGE;
                break;
            case '<':
                addChar();
                nextToken = TokenCodes.COMP_OP_SMALL;
                break;
            default:
                addChar();
                nextToken = TokenCodes.EOF;
                break;
        }
        
    }
    
    /* addChar - a function to add nextChar to lexeme */
    static void addChar() throws IOException{
        if(lexLen <= 98){
            lexeme[lexLen++] = nextChar;
            lexeme[lexLen] = 0;
        }else{
                fr_out.write("Error - lexeme is too long");
        }
    }
    
    /* getChar - a function to get the next character of
                 of input and determine its character class*/
    static void getChar(){
        try {
            nextChar = (char)fr.read();
            if(nextChar != 0){
                if(Character.isLetter(nextChar)){
                    charClass = CharacterClass.LETTER;
                }else if(Character.isDigit(nextChar)){
                    charClass = CharacterClass.DIGIT;
                }else{
                    charClass = CharacterClass.UNKNOWN;
                }
            }else{
                charClass = CharacterClass.EOF;
            }
        } catch (IOException ex) {
            Logger.getLogger(LexicalAnalyser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /* getNonBlank - a function to call getChar until it 
                     returns a non-whitespace character*/
    static void getNonBlank(){
        while(Character.isWhitespace(nextChar)){
            getChar();
        }
    }
    
    /* lex - a simple lexical analyzer for arithmetic
             expressions*/ 
    static void lex() throws IOException{
        lexLen = 0;
        lexeme = new char[100];
        getNonBlank();
        //System.out.println("lex: c " + charClass);
        switch(charClass){
            /* Parse Identifiers */
            case LETTER:
                addChar();
                getChar();
                while(charClass == CharacterClass.LETTER ||
                        charClass == CharacterClass.DIGIT){
                    addChar();
                    getChar();
                }
                nextToken = TokenCodes.IDENT;
                break;
            /* Parse integer literals */
            case DIGIT:
                do{
                    addChar();
                    getChar();
                }while (charClass == CharacterClass.DIGIT);
                nextToken = TokenCodes.INT_LIT;
                break;
            case UNKNOWN:
                lookup(nextChar);
                //System.out.println("NextToke: " + nextToken);
                getChar();
                break;
            /* EOF */
            case EOF:
                nextToken = TokenCodes.EOF;
                lexeme[0] = 'E';
                lexeme[1] = 'O';
                lexeme[2] = 'F';
                lexeme[3] = 0;
                break;
        }
        fr_out.write("Next token is: "+nextToken.getValue()+", Next lexeme is "+new String(lexeme)+"\n");
    }
    
}

Still if you have any question, please feel free to post in comment box. I would be glad to help you here.

If you like my explanation and answer, please give a thumbs up, it really motivates us to provide a good quality answer.


Related Solutions

Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static...
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static option: gcc -static hello.c -o hello1 (3) Use size command to see the memory layout of these two programs (hello and hello1). (4) Write a brief summary of what you have noted here. Submit the session history (log) of this activity and a brief summary. hello.c program bottom //////////// #include <stdio.h> #include <stdlib.h> int main() { printf("Hello World\n"); // waw hello Richard and ok...
2. Create a php program to get all the values from the forms using various methods...
2. Create a php program to get all the values from the forms using various methods and control structures like switch, if else, for, foreach, while, do while The question is required to write a program
write a program that creates steps. You are expected to take in a single positive integer...
write a program that creates steps. You are expected to take in a single positive integer which will be used as the number of steps in your stair case. The program only accepts integers greater than 0 and less than 500. If 0 is entered a message stating "Your staircase has no steps." and if the user enters a value greater than or equal to 500, a message stating "I can't build a staircase that tall." For all other values...
If you take a boat from earth and put it on a planet with twice earth’s...
If you take a boat from earth and put it on a planet with twice earth’s gravity, what would happen if you tried to float it in water? Hint: Think about the buoyancy force formula. (5 points) 2. You drop a cube of mass 1 kg into a cup of water. It sinks with an acceleration of 2 m/s^2. a. What is the buoyancy force of the water? (4 points) b. What is the density of the water if the...
This programming assignment will consist of a C++ program. Your program must compile correctly and produce...
This programming assignment will consist of a C++ program. Your program must compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting described in the Required Program Development Best Practices document that has been discussed in class and is posted to the eLearning system. Please see this descriptive file on the eLearning system for more details. The name to use in the main configuration screen text box Name: [ ] in...
This program does compile. But it does not give the expected output. It is supposed to...
This program does compile. But it does not give the expected output. It is supposed to raise the value of the input to its power all the way from 0 to 10. The only output seen is the number raised to the power 0 over and over again. what did I do wrong? help please. // This program raises the user's number to the powers // of 0 through 10. #include <iostream> #include <cmath> using namespace std; int main() {...
The time it takes for a computer program to compile can be modeled with a Gamma...
The time it takes for a computer program to compile can be modeled with a Gamma distribution with a mean of 1 minute and a variance of 0.5 minutes^2. Find the probability that it takes more than 1 minute for a program to compile.
//ONLY PUT SCREENSHOTS OF THE ANSWER FROM INTELLIJ //Complete the incomplete methods //You will need to...
//ONLY PUT SCREENSHOTS OF THE ANSWER FROM INTELLIJ //Complete the incomplete methods //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT