6

I want to create certain files from Java-source files (input: *.java files; output: something). Xtext seems like a good tool for this. Of course, you have to define the grammar (in this case: Java) first. But Xtext should be able to parse Java files and generate some files from them (if I write templates).

Somehow I can not image, that this has not been done before. I looked it up, but did not find anything. So the question is:

Is there a Xtext-grammar file, for Java (which defines the Java grammar)?

Usul
  • 237
  • 3
  • 12

3 Answers3

0

I had the same issue. Since Xtext uses Antlr under the hood, I was trying to find an Antlr Java grammar. I found this file from Terence Parr. I got about 1000 lines ...

I took this post into consideration and adjusted the grammar line by line. I took me while Wink I wasn't able to adapt every statement, so I just commented some of them out. I attached the grammar if someone else is trying to build a DSL on top of Java. I don't think that the grammar is completely right, it at least contains the issues listed in the original grammar.

http://www.eclipse.org/forums/index.php/t/251746/

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
0

Since Xtext uses Antlr under the hood try the follwing link: https://github.com/antlr/grammars-v4/tree/master/java.

Viktor Seifert
  • 636
  • 1
  • 7
  • 17
  • 1
    Xtext uses antlr3, so antlr4 grammars are completely useless. It also lacks several features that are needed (e.g.: semantic predicates). Porting a antlr3 grammar to Xtext is *not* as trivial as You seem to imply. – ZioByte Sep 02 '15 at 18:04
0

Another option now is to use JBase (https://github.com/LorenzoBettini/jbase). JBase was created to support Java expressions in XText languages (see https://dl.acm.org/citation.cfm?id=2972217) and can be incorporated with minor changes by extending JBase instead of XBase.

Ben Holland
  • 2,309
  • 4
  • 34
  • 50