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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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.
Expert Solution
steps

Step by step

Solved in 10 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY