3

I'm working on a project where I want to give feedback on Java code written by complete beginners. In other words, I need to be able to tell if the code is following conventions, not just if it's valid Java code.

I have looked into modules like pyparsing, PLY and pyPEG, and it looks like they would do the trick as far as parsing at least a large subset of Java - which would be fine. However I'm not very experienced with parsing, so I'm not sure if it's possible to use these tools to check against code conventions as well?

5
  • 1
    IMO, before commenting on code conventions, you'd need to make sure there are no syntax errors in the source file. I'ts (again IMO) a bit silly to comment on the fact someone is using a lowercase identifier for a class name when there are 20 errors in the file. Writing a Java grammar for pyparsing, PLY or pyPEG is (for someone new to parsing) no trivial task. Even editing an existing grammar to fit your needs would be a tall order. Commented Oct 19, 2012 at 7:12
  • I know it won't be trivial, that's ok :) As for syntax errors, javac handles that well enough I think. The idea with the project is to help complete beginners. For example screwing up the indentation makes it more difficult to notice missing or superfluous curly braces. So giving feedback on those kinds of things can help a rookie find their errors more quickly. Commented Oct 19, 2012 at 7:40
  • 1
    Hmm is it really advisable to write Java parser!? You might as well use Jython and use JDT for parsing. Commented Oct 19, 2012 at 7:56
  • Sounds interesting @specialscope - can you point me in the right direction to research that approach? Commented Oct 19, 2012 at 10:30
  • 1
    Look here for JDT Java parser. ibm.com/developerworks/opensource/library/os-ast Using Jython you can access JDT library. Commented Oct 19, 2012 at 15:15

2 Answers 2

3

It depends on what type of code conventions you are talking about.

  • If you are talking about conventions that are reflected in the source syntax, and semantics (e.g. checking identifier conventions, no assignment to parameters, resources properly closed), these can be be checked based on a Java parse tree produced by a parser generated from a normal Java grammar.

  • If you are talking about conventions in the use of indentation, white-space, comments and so on, a normal Java parser ignores these, and they don't get recorded in the parse tree. Therefore you would need to analyse using a parser (or some other kind of tool) that was designed to recognize and capture that stuff.

Sign up to request clarification or add additional context in comments.

1 Comment

Right, I'm talking about the second case. I'm wondering what tool or module (it has to be Python) I could use since I do care about things that aren't important for the formal syntax.
1

It seems like the best option will be to use Eclipse JDT, either through Jython or by writing a small Java program that will be executed as a separate process by Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.