0

I am wondering if I can generate ANTLR grammar from java source code. I want to do some kind of research project, but I am just exploring different open sources to see which one is best.

For ANTLR, do I always have to write a grammar and pass it to the ANTLR?

Is there a way to generate grammar from an existing Java source code?

1 Answer 1

1

Not easily. ANTLR generate a recursive descent parser from your grammar, encoding the tests into procedural code, as well as lots of other bookkeeping stuff.

Knowing how the code is generated, you might be able to take it apart but you'll have to reach into the middle of generated statements and that isn't easy without a full parser for the generated language. (Hint: regex won't work).

I don't see a lot of point of this exercise. Why don't you just use the original grammar?

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

1 Comment

Abstract syntax tree produced from Antlr has string displayName/label. Generally transformation of abstract syntax tree to parse tree requires business context. Mapping string display names to string leads to grammer information being kept at 2 places. 1. Dsl file 2.Dsl file syntax names to business context transformer .

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.