Use JAVA to code the following. Program a simple lexical analyzer that will build a symbol table from given stream of chars. You will need to read a file named "input.txt" to collect all chars. For...


Use JAVA to code the following.


Program a simple lexical analyzer that will build a symbol table from given stream of chars. You will need to read a file named "input.txt" to collect all chars. For simplicity, input file will be a C program without headers and methods. Then you will identify all the numerical values, identifiers, keywords, math operators, logical operators and others. See the example for more details. You can assume that, there will be a space after each keywords. But, removal of space will add bonus point.




Input:


int a, b, c;


float d, e;


a = b = 5;


c = 6;


if ( a > b)


{


c = a - b;


e = d - 2.0;


}


else


{


d = e + 6.0;


b = a + c;


}




Output:


Keywords: int, float, if, else


Identifiers: a, b, c, d, e


Math Operators: +, -, =


Logical Operators: >


Numerical Values: 5, 6, 2.0, 6.0


Others: , ; ( ) { }






Suggestions:



  1. Use arrayList to collect the chars from the code input.txt file. You can then convert it to an array. Call this array codeChars.

  2. Take a string array to keep all the keywords of java.

  3. Run a nested loop with the inner loop being the keywords array and the outer loop being the codeChars array. Use .contains() method to find which tokens in the codeChars array exist in the keywords array. Append the token found in another array/ arrayList.

  4. Remove multiplicity. Print unique elements only.

  5. We will follow the same procedure for math operators. We will run a nested loop with the outer loop being codeChars and the inner one with all the math operators array, and so on.

  6. Same procedure will be followed for logical operators and others.

  7. Use Pattern and Matcher classes for Numerical Values and Identifiers.

Jun 09, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here