2

I'm currently creating a grading utility for my university that compiles, lints, run professor-defined tests, etc. In the process, I'm generating a parse tree of the student's source code so I can do things like check imports, check file structure, check for a minimum number of methods, etc. When generating the parse tree, the file must be able to compile (run time errors are a non-factor). The only problem is, not all student submissions actually compile—this is problematic.

Does anyone know of a program that fixes basic Java compilation errors (e.g. missing semi-colons, curly braces, etc.) or of a way that I could go about creating such an application?

I've done some research and the best tool I've found is walkmod, but this merely corrects poorly formatted code rather than actually correcting the issues.

migrated from programmers.stackexchange.com Jan 11 '16 at 2:53

This question came from our site for professionals, academics, and students working within the systems development life cycle.

  • 4
    @djthoms consider the simple case of an unclosed string. Where and how do you close it programmatically? String foo = "this is foo; int bar = 42; on one line. Well, you close it just before the final ; and now you've got an error on the next line System.out.println(foo + bar); that bar does not exist... and now you try to solve that error? This path leads to madness or JavaScript (they are quite similar). – user450 Jan 9 '16 at 18:53
  • 3
    If that were possible, Java compilers would already do it. – Kilian Foth Jan 9 '16 at 21:09
  • 7
    Let the students submit their code through something that refuses submissions with syntax errors immediately. – RemcoGerlich Jan 9 '16 at 21:48
  • 2
    @djthoms you might have the beginnings of a rather interesting question relating to students submitting via a CI server. – user450 Jan 9 '16 at 23:27
  • 3
    Surely if it doesn't compile you just fail it? – Mawg Jan 11 '16 at 10:01
2

This is not possible. If a semicolon or curly bracket is missing, it cannot be inserted automatically, since there is no way to know the correct place to insert it. Consider this fragment:

if (x) {
doSomething1()
doSomething2()

The closing curly bracket is missing, but where would the tool insert it in order to fix the program? In short, compile errors typically indicate missing information, and you cannot fix that automatically. (After all, if it was possible to automatically insert say missing semicolons, they you wouldn't need them in the language in the first place.)

(I see this question have been migrated to "software recommendations", but be aware that such a tool cannot exist)

  • It is always possible to repair a broken program if you allow a large enough patch. (Joke) Proof: delete all the text in a file, insert "main(){}" and compile. (There have been a few student-compilers constructed over the years that did a better job of agressive-repair-until-compilable as empirical proof points). The hard part is not even fixing the syntax; you still have to get the types right. Still possible. I'll agree you likely can't repair a student's program and have any hope of executing the student's intent, but you can't prove that because you don't know the student's intent. – Ira Baxter Feb 11 '16 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.