0

I "inherited" a project that uses JJTree from JavaCC to implement a simple language parser. Since the code was over 5 years old, I decided to update all dependencies, including JavaCC, to the latest release before I do any actual development.

Therefore, I deleted all the files generated by JJTree/JavaCC and used the latest version (5.0) to rebuild them. The resulting files, however, seem to miss fields and methods that were previously present and the code does not compile any more.

I assumed that something changed in JavaCC that requires me to update the grammar file, so I tried out the Interpretter example from the current JavaCC distribution, with the same results: missing class fields that do not allow the code to compile.

For example, here is a definition from SPL.jjt of the Interpretter example:

void Id() : { Token t; } { t = <IDENTIFIER> { jjtThis.name = t.image; } } 

The folder for this example contains an ASTId.java file which originally had this content:

public class ASTId extends SimpleNode { String name; public ASTId(int id) { super(id); } public ASTId(SPLParser p, int id) { super(p, id); } public void interpret() { stack[++top] = symtab.get(name); } } 

After I regenerated the AST*.java files, the content changed:

public class ASTId extends SimpleNode { public ASTId(int id) { super(id); } public ASTId(SPLParser p, int id) { super(p, id); } } 

There is a lot missing here and as a result the SPLParser.java generated file does not compile because it uses fields that are not defined in the corresponding classes.

What am I missing? Is there a JJTree or JavaCC option that I have to use? Perhaps a modification to make in the grammar file? Or, as I don't really know if the original files have been edited, am I supposed to directly modify the generated files and add the missing bits manually?

I have no experience with JavaCC, so I would appreciate any hints to resolve this issue.

2 Answers 2

1

It's a pretty standard practice to edit the generated nodes files... the parser file and the token manager shouldn't be changed though; that's what TOKEN_MGR_DECLS and the code inside the grammar file is for.

The reason folks edit these files is that they don't change very often... although of course when they do, it's a bit of a pain.

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

Comments

1

The stuff that's missing was probably edited in by the original developers. It's not necessarily a sign that something has changed in JavaCC or JJTree. Those are relatively mature projects.

Here's hoping that the original was checked into a version control system or backed up so you can get the code back. Perhaps the decision to delete and recompile wasn't the best.

2 Comments

I comitted the original source code to my own VCS before starting. I can easily add back the missing bits, or even revert the source tree to the comitted version. I just find editing generated files a bit unsettling for my sensibilities :-)
I ended up extending the JavaCC SimpleNode class and using the NODE_CLASS option so that I don't have to edit generated files manually. The downside, of course, is that most node classes have a few extra fields that are useless - plus I have to deal with a bit of namespace pollution. As far as I can tell, per-Node base-class definitions are still in the JavaCC wishlist...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.