Question
JAVA: Only change the SymTableVisitorClass. The rest I have done. Only need -T case to work. Summary In this programming assignment you will be adding
JAVA: Only change the SymTableVisitorClass. The rest I have done. Only need -T case to work.
Summary
In this programming assignment you will be adding symbol tables to your compiler to store information about classes, class members, method, parameters, and variables. Modify your MiniJava main program so that when it is executed using the command:
java MiniJava -T filename.java
As with previous parts of the compiler project, the compiler main method should return an exit or status code of 1 if any errors are detected in the source program being compiled (including errors detected by the scanner or parser, as well as semantics and type checking). If no errors are found, the compiler should terminate with a result code of 0.
Your MiniJava compiler should still be able to print out scanner tokens if the -S option is used, and the option -P should continue to print the AST in the requested format. There is no requirement for how your compiler should behave if more than one of -P, -S and -T are specified at the same time. That is up to you. You could treat that as an error, or, maybe more useful, the compiler could print an appropriate combination of tokens, trees, and symbol tables.
You should not have to modify any of the AST nodes. A set of Symbol and SymbolTable classes are provided to you to represent type information used for semantics checking in the compiler. These classes are located in the Symtab directory.
A visitor pattern is also provided to you in the SymTableVisitor.java to traverse the AST and build the Symbol Tables. This file should be included in the Visitor directory. The main part of this assignment will be to build the SymTableVisitor class and visitor functions.
it will parse the MiniJava program in the named input file, build the symbol tables, and print the
contents of the compiler symbol tables. We do not specify the detailed format of the symbol table
output, but there should be appropriate table(s) for each scope, clearly labeled to identify the scope
(class or method in most cases), and showing the names declared in that scope, their types, and
any other important information.
Java: Symbol Table Visitor
Implement ToDo Methods Most of program is already written
import Scanner.*; import Parser.parser; import Parser.sym; import java_cup.runtime.Symbol; import java_cup.runtime.ComplexSymbolFactory; import java.io.*; import java.util.List; import AST.*; import AST.Program; import AST.Visitor.PrettyPrintVisitor; import AST.Visitor.SymTableVisitor;
public class MiniJava { public static void main(String [] args) { int status = 0; String help = "Use: " + " java MiniJava -S
public static int SymbolTables(String source_file) { int errors = 0; try { ComplexSymbolFactory sf = new ComplexSymbolFactory(); Reader in = new BufferedReader(new FileReader(source_file)); scanner s = new scanner(in, sf); parser p = new parser(s, sf); Symbol root; root = p.parse(); Program program = (Program)root.value; program.accept( new SymTableVisitor() ); System.out.println(" Parsing completed"); System.out.println(errors + " errors were found."); } catch (Exception e) { System.err.println("Unexpected internal compiler error: " + e.toString()); e.printStackTrace(); } return errors == 0 ? 0 : 1; } }
package AST.Visitor;
import java.util.HashMap; import java.util.Iterator;
import AST.*; import Symtab.*;
Only Need to Fix this class public class SymTableVisitor implements Visitor {
SymbolTable st = new SymbolTable(); public void print() { st.print(0); } public String getTypeString(Type t) { /* TO DO */ return ""; } // MainClass m; // ClassDeclList cl; public void visit(Program n) { n.m.accept(this); for (int i = 0; i < n.cl.size(); i++) { n.cl.get(i).accept(this); } }
// Identifier i1,i2; // Statement s; public void visit(MainClass n) { /* TO DO */ }
// Identifier i; // VarDeclList vl; // MethodDeclList ml; public void visit(ClassDeclSimple n) { /* TO DO */ }
// Identifier i; // Identifier j; // VarDeclList vl; // MethodDeclList ml; public void visit(ClassDeclExtends n) { /* TO DO */ }
// Type t; // Identifier i; public void visit(VarDecl n) { /* TO DO */ }
// Type t; // Identifier i; // FormalList fl; // VarDeclList vl; // StatementList sl; // Exp e; public void visit(MethodDecl n) { /* TO DO */ }
// Type t; // Identifier i; public void visit(Formal n) { /* TO DO */ }
// StatementList sl; public void visit(Block n) { /* TO DO */ }
// Exp e; // Statement s1,s2; public void visit(If n) { /* TO DO */ }
// Exp e; // Statement s; public void visit(While n) { /* TO DO */ }
// Exp e; public void visit(Print n) { }
// Identifier i; // Exp e; public void visit(Assign n) { }
// Identifier i; // Exp e1,e2; public void visit(ArrayAssign n) { }
// Exp e1,e2; public void visit(And n) { }
// Exp e1,e2; public void visit(LessThan n) { }
// Exp e1,e2; public void visit(Plus n) { }
// Exp e1,e2; public void visit(Minus n) { }
// Exp e1,e2; public void visit(Times n) { }
// Exp e1,e2; public void visit(ArrayLookup n) { }
// Exp e; public void visit(ArrayLength n) { }
// Exp e; // Identifier i; // ExpList el; public void visit(Call n) { }
// int i; public void visit(IntegerLiteral n) { }
public void visit(True n) { }
public void visit(False n) { }
public void visit(This n) { }
// Exp e; public void visit(NewArray n) { }
// Identifier i = new Identifier(); public void visit(NewObject n) { }
// Exp e; public void visit(Not n) { }
// String s; public void visit(IdentifierExp n) { }
// String s; public void visit(Identifier n) { } // int[] i; public void visit(IntArrayType n) { }
// Bool b; public void visit(BooleanType n) { }
// Int i; public void visit(IntegerType n) { }
// String s; public void visit(IdentifierType n) { }
// Display added for toy example language. Not used in regular MiniJava public void visit(Display n) { } }
Example Output for BinarySearch.java is file input:
{CLASS}class BS Variables: {VAR}int[] number {VAR}int size Methods: {METHOD}int Div({VAR}int num) {METHOD}int Print() {METHOD}int Init({VAR}int sz) {METHOD}int Start({VAR}int sz) {METHOD}boolean Search({VAR}int num) {METHOD}boolean Compare({VAR}int num1, {VAR}int num2) {METHOD}int Div({VAR}int num) {VAR}int count02 {VAR}int num {VAR}int count01 {VAR}int aux03 {METHOD}int Print() {VAR}int j {METHOD}int Init({VAR}int sz) {VAR}int sz {VAR}int aux01 {VAR}int j {VAR}int k {VAR}int aux02 {METHOD}int Start({VAR}int sz) {VAR}int sz {VAR}int aux01 {VAR}int aux02 {METHOD}boolean Search({VAR}int num) {VAR}boolean var_cont {VAR}int left {VAR}int nt {VAR}int num {VAR}int aux01 {VAR}int right {VAR}int medium {VAR}boolean bs01 {METHOD}boolean Compare({VAR}int num1, {VAR}int num2) {VAR}int num1 {VAR}int aux02 {VAR}boolean retval {VAR}int num2 {VAR}int[] number {VAR}int size {CLASS}class BinarySearch Methods: {METHOD}void main({VAR}String[] a) {METHOD}void main({VAR}String[] a) {VAR}String[] a
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started