COP5555 Parser Program Help

1. Make sure your HW submission has "cop5555.jar" files in it.
    It should be extracted into cop5555/Parser.java, cop5555/Scanner.java, cop5555/SyntaxException.java
    (In addition, all your other java files should be under cop5555 directory...)
2. Don't change cop5555_tokens and cop5555_ast package.
3. Don't submit your class files.(java files only)
4. Make sure the next() method in Scanner and parse() method in Parser are public.
5. This is the code for simple test. Following table shows expected output.

import java.io.*;

public class TestParser{
  public static void main(String args[]){
    cop5555.Parser p = null;
    Reader r = null;

    try { r = new BufferedReader(new FileReader(args[0])); }
    catch(FileNotFoundException e){ System.exit(-1); }

    try{ p = new cop5555.Parser(r); }
    catch(IOException e){ System.exit(-1); }

    try{
      cop5555_ast.AST tree = p.parse();
      cop5555_ast.ToStringVisitor v = new cop5555_ast.ToStringVisitor();
      tree.visit(v, "");
      System.out.println(v.getString());
    }
    catch(cop5555.SyntaxException e){System.out.println(e);}
    catch(Exception e){System.out.println(e);}
  }
}

 

Example Input

Output

program errorTest extends error {}

cop5555.SyntaxException: expected token LBRACE
IDENT extends:0,18

program test { }

  Program:
    ident=test
    block=Block:0,13
      items=empty

program t0 { read ABC [a][b][c]; }

  Program:
    ident=t0
    block=Block:0,11
      items=
        ReadStatement:0,13
          var=ListVar:0,18
            ident=ABC
            exprList=
              VarExpr:0,23
                var=Var:0,23
                  ident=a
              VarExpr:0,26
                var=Var:0,26
                  ident=b
              VarExpr:0,29
                var=Var:0,29
                  ident=c

current version of ToStringVisitor does't print ident of ListVar.
You should add three more lines to ToStringVisitor

  public Object visitListVar(ListVar node, Object arg) throws Exception
  {
    String indent = (String) arg;
    String fi = indent + sh;
    s.append("ListVar:" + node.firstToken.line + ","
      + node.firstToken.char_num);
    ln();
    indent(fi);

    s.append("ident=" + ((IdentToken) (node.firstToken)).spelling);
    ln();
    indent(fi);

    s.append("exprList=");
    if (node.args.isEmpty())
    {
      s.append("empty");
    }
    else
    {
      String li = fi + sh;
      for (Expr e : node.args)
      {
        ln();
        indent(li);
        e.visit(this, li);
      }
    }
    return null;
  }