0

This problem is sort of a continuation of How to write visitor classes for collections? - I tried that answer, but I find that the code works in eclipse, but has a null pointer problem in unix or windows. So now it looks like a different problem, so I created a new question.

I have uploaded the full code at https://sites.google.com/site/rogergdmn/ , Below is the summary.

Here are the details (the code is a variation of the LabeledExpr.g4 from the book) - I am trying to create an intermediate data structure by using the visitor classes. When I run in command line (in unix or windows), the line "why null e" is printed, but this line is not printed when I run in eclipse. How do I fix this bug?

This is the grammar:

prog:   stat+ ;
stat:   expr NEWLINE                # printExpr
        |   NEWLINE                 # blank
        ;
expr:      INT                         # int
           ;

These are the functions in EvalVisitor.java:

public Object visitInt(ExprParser.IntContext ctx) {
System.out.printf("visited----- 1\n");
    int value = Integer.valueOf(ctx.INT().getText());
    return new E(1, value);
}
public Object visitPrintExpr(ExprParser.PrintExprContext ctx) {
System.out.printf("visited----- 2\n");
    E e = (E) visit(ctx.expr()); // evaluate the expr child
    return new E(2, e);
}
public Object visitProg(ExprParser.ProgContext ctx) {
    System.out.printf("visited----- 3\n");
    List<ExprParser.StatContext> sL = ctx.stat();
    List<E> eL = new ArrayList<E>();
    for(ExprParser.StatContext s : sL) {
        E e = (E) visit(s);
        if(e==null) System.out.printf("why null e??\n");
        eL.add(e);
    }
    return new E(7, eL);
}

This is the E class (only constructors present, to demonstrate the error):

public class E {
    int typ;
    int val;
    E e1;
    List<E> eL;
    public E(int _typ, int _v) { typ = _typ; val = _v; }
    public E(int _typ, E _e1) { typ = _typ; e1 = _e1; }
    public E(int _typ, List<E> _eL) { typ = _typ; eL = _eL; }
}

The other codes are directly from the book example (http://pragprog.com/titles/tpantlr2/source_code , directory "starter").

BTW, a similar visitor code is shown at If/else statements in ANTLR using listeners and https://github.com/bkiers/Mu, and that code works fine for me. My code is pretty similar to that code, so I am not sure whats going wrong here.

Community
  • 1
  • 1
R71
  • 4,283
  • 7
  • 32
  • 60
  • Why null e? Because the visit() method returns null. But we don't have any idea what the visit() method does. Use your debugger. – JB Nizet Nov 22 '13 at 07:07
  • Thats what I did - I tried debugging in eclipse, and all the data there was as expected - an E(2) enclosing an E(1). My question is: why is it misbehaving when I run the same program from command line? – R71 Nov 22 '13 at 08:21
  • We can't know without seeing the code. Maybe the classes you execute in command line are not the same as the ones you execute in eclipse. Add traces in the relevant parts of the code and see what happens. – JB Nizet Nov 22 '13 at 08:26
  • I have shown all the code that I have changed from the book example. The visit() method is part of antlr4 jar. Maybe this question is better answered by the antlr4 experts. – R71 Nov 22 '13 at 09:19
  • I have now also uploaded the full code. See link at the top of the question. – R71 Nov 22 '13 at 11:53

1 Answers1

3

You didn't override visitBlank, so any blank statement will return null from the visit method.

Edit: The difference between Eclipse and the other case is one of the following:

  1. You could be using different input in the two cases. You'll need to examine the raw contents of the TokenStream to be sure.
  2. You didn't include an explicit EOF at the end of your prog rule, so there is a possibility that your parser is ignoring some tokens of the input. To ensure all tokens are considered in all cases, add a reference to EOF at the end of the prog rule.
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Interesting. But then how is it working when I run the same code inside eclipse? – R71 Nov 22 '13 at 15:28
  • Yes, your suggestion works, the error vanishes when I delete the blank line from the input file. But I still dont understand why it works in eclipse??? (In eclipse, the error occurs only if there are 2 blank lines!) – R71 Nov 22 '13 at 15:35
  • @Rog you are using different input when you run in Eclipse and when you run outside Eclipse. – Sam Harwell Nov 22 '13 at 18:01
  • No, when I use the same input file, it works in eclipse and not outside. When I add an extra line in the eclipse input, then it demonstrates the same error. – R71 Nov 22 '13 at 18:33
  • I am accepting this answer, because it solved my problem. But I wish somebody can also explain the eclipse behavior. – R71 Nov 23 '13 at 02:26