4

Does anyone have an example of how to set up sbt to build an ANTLR file (to scala) and then compile the resulting code.

My file layout

 src/main/scala/Test.scala     // scala test rig
 src/main/scala/Test.g         // antlr grammar

 build/antlr/TestParser.scala  // antlr output files
 build/antlr/TestLexer.scala

What should my sbt contain? I know there's a plugin out there for pulling in the rules for ANTLR, but I haven't been able to make it work. (Still newbie to this world)

Steffen
  • 8,033
  • 1
  • 28
  • 34
wing
  • 171
  • 1
  • 5

1 Answers1

6

I've written a sbt plugin to generate the parser and lexer code from a provided antlr grammer file. You can download the code on my github page http://github.com/stefri/sbt-antlr. It's also listed in the sbt plugin list https://github.com/harrah/xsbt/wiki/sbt-0.10-plugins-list. The latest snapshot uses ANTLR 3.3 and is available via my github maven repository for the sbt 0.11.x series. If you need another ANTLR version it's easy to change and rebuild, I'm still working on the configuration options.

The usage is quite simple, just include the the plugin and my maven repository in ./project/plugins/build.sbt

resolvers += "stefri" at "http://stefri.github.com/repo/snapshots"

addSbtPlugin("com.github.stefri" % "sbt-antlr" % "0.2-SNAPSHOT")

then place your ANTLR3 grammar files in src/main/antlr3. They will be included in your next build.

Make sure you also include the plugins settings sbtantlr.SbtAntlrPlugin.antlrSettings in your project settings, e.g if you are using the simple configuration approach add the following line

seq(sbtantlr.SbtAntlrPlugin.antlrSettings: _*)

to your build.sbt file. Note, sbt-antlr generates the source code only once as long as your grammar file didn't change it does not re-generate the java source files.

The generated java files are spit out to target/scala-2.9.1/src_managed/main/antlr3, so make sure you inlcude that path in your IDE's build path. The plugin is still work in progress, but it already works quite nice with my grammars.

Steffen
  • 8,033
  • 1
  • 28
  • 34
  • Sorry to take to long to find this. I'm eager to move away from my Makefile approach... however, I can't grok the step where it sez to Make sure you also include the plugins settings sbtantlr.SbtAntlrPlugin.antlrSettings in your project settings Do you have an example build.sbt with above? – wing Jan 30 '12 at 00:11
  • I added some information to my answer how to add the settings to your build file hope that helps. If you are using a full build config you should add it there but the way depends on your own build file so this is hard to describe in a general way. – Steffen Jan 30 '12 at 07:53
  • @Steffen Thanks can you please tell how to change the target directory of java source generation i want to generate generated files in some package – Sandeep Purohit Nov 25 '16 at 12:49