How to parse a for-loop using Javaparser?

*爱你&永不变心* 提交于 2019-12-11 15:25:35

问题


I need using javaparser to parse uncorrect form of the for-loop to the correct form. My loop has 5 arguments:

  1. index of the loop (i);
  2. initial value of the index. It can be other value (for example, k) or int value (10);
  3. value of the loop invariant (3);
  4. condition of the invariant (>, <, >= or <=);
  5. operation, performed after each loop run (- or + will be changed to i-- or i++).

I've created two classes. The first one is with the uncorrect loop and the second one is with the correct loop (after parsing). I decided at first to write this two classes to check how should look the code before and after the parsing and then writing the code for parsing. But I'm not sure that it's a good start and I represented my for-loop correctry. Clarify: I want to parse the code from Class.java to the ClassAltered.java.

The first class with uncorrect loop:

public class Class {
public static void main(String[] args) {
    test1();
    test2();
}

public static void test1() {
    FOR(i, 10, 3, >, -);
    System.out.println("FOR(i, 10, 3, >, -) test passed");
}

public static void test2() {
    FOR(j, 0, 10, <=, +);
    System.out.println("FOR(j, 0, 10, <=, +) test passed");
}
}

The second class with the correct loop:

public class ClassAltered {
    public static void main(String[] args) {
        test1();
        test2();
    }

    public static void test1() {
        for(int i=10; i > 3; i--);
        System.out.println("FOR(i, 10, 3, >, -) test passed");
    }

    public static void test2() {
        for(int j=0; j<= 10; j++);
        System.out.println("FOR(j, 0, 10, <=, +) test passed");
    }
 }

回答1:


This is possible but well above average task.

You can't do this with "normal" javaparser because javaparser parses Java syntax and FOR(i, 10, 3, >, -); is not Java syntax. So "normal" javaparser will not be able to parse this.

What you will need to do is to create your own fork/version of javaparser and modify the java.jj grammar to include your "incorrect for" statement. Check this fragment for what a normal ForStatement looks like:

Statement ForStatement():
{
    VariableDeclarationExpr varExpr = null;
    Expression expr = null;
    NodeList<Expression> init = emptyList();
    NodeList<Expression> update = emptyList();
    Statement body;
    JavaToken begin;
}
{
  "for" {begin=token();} "("

  (
      LOOKAHEAD(VariableDeclarationExpression() ":")
      varExpr = VariableDeclarationExpression() ":" expr = Expression()
    |
     [ init = ForInit() ] ";" [ expr = Expression() ] ";" [ update = ForUpdate() ]
  )

  ")" body = Statement()

  {
    if (varExpr != null) {
        return new ForeachStmt(range(begin, token()),varExpr, expr, body);
    }
    return new ForStmt(range(begin, token()),init, expr, update, body);
  }
}

It's not too difficult, you will probably able to do this just by analogy, you won't need much of JavaCC knowledge.

Next, when the grammar is done you'll get a javaparser which will be able to parse "incorrect" for-loops. The result will be an AST containing something like IncorrectForStmt (you'll need to implement this class).

In the test you'll need to parse the source code and then analyze the resulting AST to locate IncorrectForStmt. To verify you'll need to check the sub-nodes of IncorrectForStmt.




回答2:


[I am the maintainer of JavaParser] You could use a search/replace with regular expressions, avoiding JavaParser alltogether. Not pretty, but if the syntax is simple it will work most of the time.



来源:https://stackoverflow.com/questions/48034411/how-to-parse-a-for-loop-using-javaparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!