5

I am looking for a parser generator that accepts a Javacc grammar file (.jj) and generates a parser in Javascript (instead of Java). Does such a thing exist? Alternately how difficult would it be to convert the .jj file to something that ANTLR (which can output Javascript) can understand?

David Tinker
  • 9,383
  • 9
  • 66
  • 98
  • Check out this post http://stackoverflow.com/questions/6211111/javascript-parser-generator – Sergii Zagriichuk Oct 24 '12 at 11:08
  • Also see [this Wiki page](http://en.wikipedia.org/wiki/Comparison_of_parser_generators) that lists many parser generators (including their output-language(s)) – Bart Kiers Oct 24 '12 at 11:46

4 Answers4

3

I am looking for a parser generator that accepts a Javacc grammar file (.jj) and generates a parser in Javascript (instead of Java). Does such a thing exist?

Apparently, no.

Alternately how difficult would it be to convert the .jj file to something that ANTLR (which can output Javascript) can understand?

It would be non-trivial, and you'd need to do it by hand. But the actual degree of difficulty probably depends on the grammar you are trying to convert.


It is worth noting that there are other parser generators that output parsers in Javascript. (And, no, I'm not going to list them or recommend one!)

Obviously, the input language will be different to Javacc input.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Building parsers for the web with JavaCC & GWT : http://consoliii.blogspot.co.uk/2014/04/creating-gwt-compatible-parser-using.html – Chris Apr 22 '14 at 00:04
1

Google's GWT compiles java code to JavaScript, as long as only a specific subset of the JDKis used. I'm also not sure how much GWT-specific cruft it brings along. But that might be a possibility: using javacc to generate java code, and then GWT to translate it to JavaScript.

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • Interesting solution. I have done a lot of GWT work in the past so its a possibility. However generating a "native" Javascript parser would be nicer. Will have to see how hard it is to convert to ANRLR. – David Tinker Oct 24 '12 at 13:37
  • @DavidTinker Yeah, I would only use this as a last-ditch or temporary effort. But I thought I'd at least mention it! :) – yshavit Oct 24 '12 at 16:05
  • Indeed, a creative idea! However, I'd imagine JavaCC (or any other parser generator) would use a large amount of classes from the `java.io` package, and I guess GWT cannot cope with those classes, right? – Bart Kiers Oct 26 '12 at 08:07
  • Probably, though depending on the abstractions it may be somewhat salvageable? Dunno. – yshavit Oct 26 '12 at 14:00
  • @BartKiers GWT/JavaCC is able to work with IO abstractions. Prior to version 6.1 this was not supported (without hacks). In 6.1, the IO layer is wrapped so you can parse via a stream with a custom interface or from a string. – Chris Feb 24 '17 at 00:46
0

I recently added support for a GWT compatible parser target to JavaCC (no dependencies on Java IO classes). I recently wrote a pair of blogs on how to use JavaCC so that a JavaScript parser can be generated via GWT. Link below:

Building parsers for the web with JavaCC & GWT (My blog post)

JavaCC 6.1 Development Build

Chris
  • 4,450
  • 3
  • 38
  • 49
0

If you don't mind rewriting your grammar, I have written a parser builder in Kotlin common code, so it will run on a JavaScript platform.

The grammar syntax is fairly simple and EBNF like.

You can see details here, and there is a link to a demo running in the browser if you want to try it out.

https://medium.com/@dr.david.h.akehurst/a-kotlin-multi-platform-parser-usable-from-a-jvm-or-javascript-59e870832a79

user2228895
  • 167
  • 7