3

I am looking for a parser written in Java capable of reading JML.

Basically I would like the parser to be able to read a JML block and know to which method it belongs to.

I've been looking at the OpenJML project, but just the project setup is too much.

Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85

1 Answers1

3

I doubt that you will find a tool that does exactly what you want, or even close to what you want.

You could write a "partial" Java grammar that scans an input file for //@ ... and /*@ ... @*/ directly followed by a method declaration. By "partial" I mean that you're not semantically parsing the input source, but perform this only on a lexical level (so tokens only). Make sure that you account for string literals: you wouldn't want the literal String s = "/*@"; to be the start of a JML spec.

Two well known parser generators for Java are:

  1. ANTLR
  2. JavaCC

Getting to grips with either one will take a bit of time, especially if you're new to parser generators, but once you get the hang of it, it really is not much work to create a small grammar that could do this reliably.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • The idea sounds interesting, however, I don't have the time to learn about parser generation. I think I can pull it off with a couple of RegEx's what is your opinion on that? – Tiago Veloso Apr 13 '11 at 22:38
  • Doing it with regex will be unreliable. You will not be able to distinguish valid JML specs from ones that are inside string literals (`".../*@..."`) and inside comments (`//...@*/...`). – Bart Kiers Apr 14 '11 at 06:09
  • My examples above might never occur, but whenever a large chunk of code is commented out (either by multiple `//`'s, or by `/* ... */`), a regex will still match JML specs inside those blocks. In short: regex might do the trick, but it will never be a "production worthy" solution, IMO (and I _am_ a real regex enthusiast!). – Bart Kiers Apr 14 '11 at 08:27