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.
Step by step
Solved in 10 steps with 1 images