1

I am working on a school project where I need to generate Java code that adheres to the Java Language Specs. So far I have been able to generate the Java code randomly which has valid syntax that doesn't follow the language rules.

Next, I must be able to check if the language rules are obeyed by the generated code. What would be the best approach to follow this? Should I write code that checks if the language rules are adhered to? Is there an API available(Prolog?) that allows me to encode these rules and apply them to the code?

I don't think I can use the Java compiler API since it defeats the purpose of the homework. How would you approach this objective?

EDIT: The project doesn't require me to consider the entire Java Language Specs. I can consider a subset of the rules.

7
  • 2
    By definition, the only way to validate Java code is to implement a parser for the language. That is a monumental task to develop from scratch. Commented Nov 29, 2017 at 18:39
  • Jim Garrison please read my edit. Commented Nov 29, 2017 at 18:51
  • 1
    Please show an example of a program that "has valid syntax" but does not "follow the language rules"? You have to define what you mean by "language rules". If by "language rules" you mean anything to do with program semantics, you're into NP-Complete territory. Commented Nov 29, 2017 at 18:53
  • What I meant was the generated code contains the Java language constructs. The code has been generated using the Java BNF. However, the JLS has rules, for eg: final variables cannot be reinitialized. Such rules need to be checked in the generated code. Commented Nov 29, 2017 at 18:56
  • 1
    That involves doing the same flow analysis done by the compiler. You need to write a compiler for whatever subset of the language you choose. You are looking for a magic simple solution that doesn’t exist. Commented Nov 29, 2017 at 19:04

2 Answers 2

0

Let a compiler compile your files. Your homework likely does not require you to write a java parser, not even for a subset of Java.

If you really want to do it, you can look for alternative java parser, like this one: https://github.com/antlrjavaparser/antlr-java-parser

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

3 Comments

He says "I don't think I can use the Java compiler API since it defeats the purpose of the homework."
I know. 'He thinks...'. He is likely wrong. Telling him he is wrong is most likely a better answer than trying to make him write his own java parser.
In general, yes, but here they seem to have been instructed to write code as homework. If you read the other comments, he seem to be saying that he already has the syntax part of the code.
0

I'm fairly sure that there is no simpler solution than to start from your existing code, that checks/generates the syntax, and add Prolog code that verifies the additional constraints you mention. Prolog is a good language for this kind of thing.

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.