#1530 [java] Parser exception on Java code

PMD-5.4.3
closed
PMD
3-Major
Bug
5.5.1, 5.4.2
2016-10-16
2016-10-11
Juan Sotuyo
No

I'm running PMD over PMD-Core's sourcecode, and I'm coming accross a parse exception on DAAPathFinder

pmd-core/src/main/java/net/sourceforge/pmd/lang/dfa/pathfinder/DAAPathFinder.java - Error while parsing src/main/java/net/sourceforge/pmd/lang/dfa/pathfinder/DAAPathFinder.java

The offending line is line 336:

((PathElement) stack.getLastLeaf().getUserObject()).currentChild++;

The cause, is that "++" is unexpected.

I've been debugging the JavaParser class, and the problem seems to arise from it not being able to detect the precense of the post-increment.

The syntax file seems about right:

void StatementExpression() :
{}
{
  PreIncrementExpression()
|
  PreDecrementExpression()
|
  LOOKAHEAD( PrimaryExpression() ("++" | "--") ) PostfixExpression()
|
  PrimaryExpression()
  [
  AssignmentOperator() Expression()
  ]
}

void PostfixExpression() #PostfixExpression((jjtn000.getImage() != null)):
{}
{
  PrimaryExpression() [ "++" {jjtThis.setImage("++");} | "--" {jjtThis.setImage("--");} ]
}

However, on the generated code:

    try {
      switch (jj_nt.kind) {
      case INCR:
        PreIncrementExpression();
        break;
      case DECR:
        PreDecrementExpression();
        break;
      default:
        jj_la1[121] = jj_gen;
        if (jj_2_73(2147483647)) {
          PostfixExpression();
        } else {
          ...
        }

jj_2_73(2147483647) always evaluates to false. Something seems wrong in the lookahead, but I can't figure it out, and my Antlr2 knowledge is somewhat limited.

I'm still tinkering with it, but at this point, it's probably better off in someone else's hands.

Discussion

  • Juan Sotuyo

    Juan Sotuyo - 2016-10-11

    I've been looking around javacc's OS grammar repository, and all 4 Java grammas define StatementExpression with post operators inline, not using PostfixExpression.

    In all cases (for instance this one), the following comment can be found:

    /*
     * The last expansion of this production accepts more than the legal
     * Java expansions for StatementExpression.  This expansion does not
     * use PostfixExpression for performance reasons.
     */
    

    Defining it as follows seems to work as expected, but doesn't feel right:

     void StatementExpression() :
    {}
    {
      PreIncrementExpression()
    |
      PreDecrementExpression()
    |
      PrimaryExpression()
      [
        "++" {jjtThis.setImage("++");}
      |
        "--" {jjtThis.setImage("--");}
      |
        AssignmentOperator() Expression()
      ]
    }
    

    I'm still not sure what the problem is with the LOOKAHEAD though...

     
  • Juan Sotuyo

    Juan Sotuyo - 2016-10-11

    This change to StatementExpression does not only feel wrong, but makes some tests and rules fail, since nodes will be different....

    It seems we really need to get the lookahead to work somehow.

     
  • Juan Sotuyo

    Juan Sotuyo - 2016-10-12

    I've no idea why, but if we edit UnaryExpressionNotPlusMinus to rewrite:

    | LOOKAHEAD("(" <IDENTIFIER> ")" "+") PostfixExpression()
    

    as a semantic lookahead, insted of a syntactic one (yeah, I've been reading a lot on JavaCC):

    | LOOKAHEAD( { getToken(1).kind == LPAREN && getToken(2).kind == IDENTIFIER && getToken(3).kind == RPAREN && getToken(4).kind == PLUS } ) PostfixExpression()
    

    It will work on all test scenarios, including #1484 and this one. I've submited a PR, although I'd appreaciate if someone was able to explain the root of the issue...

    Sorry for the super spammy thread, thanks for bearing with me :)

     
    Last edit: Juan Sotuyo 2016-10-12
  • Andreas Dangel

    Andreas Dangel - 2016-10-12

    Hi - many thanks for your effort!!! I'll integrate your PR this weekend. Thanks again!

     
  • Andreas Dangel

    Andreas Dangel - 2016-10-16
    • labels: --> java, parser
    • summary: Parser exception on Java code --> [java] Parser exception on Java code
    • Milestone: New Tickets --> PMD-5.4.3
     
  • Andreas Dangel

    Andreas Dangel - 2016-10-16
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Affects version: 5.5.1 --> 5.5.1, 5.4.2
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks