Grammar compiler compiler for Java

My company is trying to write some kind of Android software. We would like to work with Java, and there is a company software component that is C ++, and therefore it needs to be ported (or at least you need to transfer the requirements before trying the NDK stuff). This code was created using Accent , and it defines grammar grammar. As far as I can tell, the original author (now gone) wrote a grammar to indicate how to specify the grammar, and then compiled the compiler-compiler with that grammar and Accent. The compiler-compiler takes a grammar of the specified format and creates binary code to parse the strings corresponding to this grammar. Here is an example of a piece of grammar:

//include rules from from this file (such as <alpha>) include "alphabet.bnf" <<topSymbol>> = <alpha> <alpha> <alpha>? .//two letters with an optional third //square brackets enclose an XML statement clarifying semantics of the rule [ <topSymbol> <letter> <command val="doSomethingToLetter"/> </letter> <!--etc.--> </topSymbol> ] 

My question is how to do this using Java using Antlr or some other tool. The compiler-compiler compiler seems pretty complicated to me. Alternatively, I would like to know how easy it is to compile / parse this type of grammar, which contains grammatical and semantic XML information.

+4
source share
3 answers

If the original designer knew what he was doing, and it was justified, then you want to keep this concept. The right approach is with another parser generator (or at least with a syntax scheme). Any JavaCC or ANTLR will be good as parser generators; You will have to manually translate the grammar. You can hand code a recursive descent parser if the grammar is simple enough.

If the original designer was just on top, then you can probably replace the aspect based on grammar, but you cannot do this without understanding what he is achieving. The fact that this โ€œseems pretty complicated to meโ€ means that you really donโ€™t understand the technology of the parser generator and the parser, and you are guided by the desire to do something that you understand, than to save something that you donโ€™t do. But its a bad idea to break something that is well designed / implemented just because you don't understand it. I highly recommend that you learn more about these technologies and ask why it was implemented in this way? Ultimately, you may be right and should replace his approach with something else, but make this choice based on knowledge, not fear.

+3
source

My question is how to do this using Java using Antlr or some other tool. The compiler-compiler compiler seems pretty complicated to me.

It seems complicated to me too!

Alternatively, I would like to know how easy it is to compile / parse this type of grammar, which contains grammatical and semantic XML information.

No ... there is no simple answer. It looks like your former colleague has moved to the top of the difficulty. You will need:

  • either plunge into what his code does, and how he does it, find out how Antlr works, and translate it,
  • or cut his code AND design and find an easier way to do what he does.

Good luck


(Actually, there is a good chance that the code is not as complex as it seems ... after you consider it and compiler-compiler technology.)

+1
source

It is best to translate the grammar into ANTLR or Java CC or some other tool.

Another possibility is to invoke C ++ code using JNI, but this is fraught with danger.

I do not know anything that can help. You just need to get a shovel and start digging.

+1
source

All Articles