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: Use arrayList to collect the chars from the code input.txt file. You can then convert it to an array. Call this array codeChars. Take a string array to keep all the keywords of java. 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. Remove multiplicity. Print unique elements only. 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. Same procedure will be followed for logical operators and others. Use Pattern and Matcher classes for Numerical Values and Identifiers.
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:
- Use arrayList to collect the chars from the code input.txt file. You can then convert it to an array. Call this array codeChars.
- Take a string array to keep all the keywords of java.
- 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.
- Remove multiplicity. Print unique elements only.
- 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.
- Same procedure will be followed for logical operators and others.
- Use Pattern and Matcher classes for Numerical Values and Identifiers.
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 10 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)