Question: Q) In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier
Q) In computer science, asymbol tableis a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, value, scope level and sometimes its location.
The compiler uses a symbol table to keep track of scope and binding information about identifiers. The symbol table is searched every time an identifier is encountered in the source text. Add a new entry if a new identifier is discovered or updates the value if an existing identifier value is changed. The symbol table mechanism must allow to add new entries and find existing entries.
Select hash data structure or array data structure to implement symbol-table processing
Consider the following program written in Java:
public class Test {
public static void main(String[] args) {
int count=3;
double sum = 0.0;
for (int i = 1; i =>
sum =sum+ i;
System.out.println (sum);
} }
A JAVA compiler that parses this code will contain at least following symbol table entries (Name (key of the table), Type , and Value). The value should be updated as you read the file. Names are uniquely identified, if an identifier is defined twice you should send an error message. For simplicity, you may assume that your identifiers can be only int, double, and char.
Sample of the Symbol Table for the previous program:
| Symbol name | Type | Value |
| Count | int | 3 |
| Sum | double | 0, 1, 3, 6 |
| I | Int | 1,2,3 |
Note for project simplicity:
1- Assume variables data types are ( int, double, float, long, short).
2- The program uses the = statement to update a variable.
Note if you use array data structure:
Use class Node{
String name ;
String type ;}
1- Break java file, save it in array word.
2- Scan array word
2.1 - if you see word int, double create new node insert in array data structure (Symbol table).
2.2 - look in the system if found update, else if not (it is a value).
Hints for programming:
Here is a simple example that opens a file, reads a text file and store the information in an array data structure
importjava.io.File;
importjava.util.Date;
importjava.util.Scanner;
importjava.io.FileNotFoundException;
publicclasstestReadfile {
publicstaticvoidmain(String[] args) {
Date timeStart =newDate();
inti = 0;
try{
String[] words =newString[5000]; //Assuming the input file has max size of 5000 words
Scanner input =newScanner(System.in); //Scanner object for keyboard input
System.out.print( \"Enter the filename: \" ); // Prompt the user for a file name
String fileName = input.nextLine(); // get a file name from the user
//If the full path is not given, then the file must be on the same folder as the source java file. //Otherwise, the user must give the full path
File inputFile =newFile(fileName ); // create a File object
//Another Scanner object for file input
Scanner fileInput =newScanner(inputFile);
while(fileInput.hasNext()) //Read from file as long as you have strings
{
words[i] = fileInput.next(); //Read a single string and store it in the array
i++;
}
//Verify that you have the words stored in the array
for(i = 0; i&&>null; i++)
if(words[i].equals(\"int\")||words[i].equals(\"double\"))
.
.
System.out.print(words[i] + \" \"+words[i+1]);
Date timeEnd =newDate();
System.out.println();
// calculate the total time and print the result
longtotalTime = timeEnd.getTime() - timeStart.getTime();
System.out.println(\"Total Time: \" + totalTime);
}catch(FileNotFoundException e) {
System.out.println(e.getMessage());
}
} }.
User should only enter the file name
the output should look like this :
Symbol Name Type Value
Count int 3
Sum double 1
i int 1
Symbol Name Type Value
Count int 3
Sum double 3
i int 2
Symbol Name Type Value
Count int 3
Sum double 6
i int 3
- State the project problem in your own words. Describe the input and output.
(Problem :Output simple table , input java source code file )
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
