Tuesday, 15 April 2014

Building parsers for the web with JavaCC & GWT (Part two)

This is the second part of a tutorial on how to create a basic parser for use in client-side GWT code (this tutorial uses Eclipse Kepler). A github repo has been created containing the files corresponding to both parts of this tutorial.

In the first part of the tutorial, we created a GWT compatible parser module using JavaCC. In this second tutorial, we will wire up the GWT module to a basic GWT project.

This tutorial makes heavy use of animated gifs, which I make no apologies for - but sorry.

Part Two

Step 7 - Create a Web Application (GWT) Project (using Eclipse)

Step 8 - Strip out the sample HTML from the generated project (Example1UI,html).\

Step 9 - Add the parser GWT module jarfile OR the pre-requisite parser project (** shown in the gif below) to the Java Build path of the GWT Project

Step 10 - Reference the parser GWT module from the newly created GWT UI project .gwt.xml file.

Step 11 - Write code in Example1UI project to use the parser on the client side. The code can be found here.

Step 12 - Compile GWT Project

To compile, simple right click the Eclipse1UI project, go to "Google" then click "GWT Compile" and click the "Compile" button.

Step 13 - A Parser in JavaScript

Finally, here is the parser running entirely in client-side JavaScript via the magic of the GWT compiler.


JavaCC is certainly not the best parser generator looking at the quality of the generated code but it is (almost uniquely) excellent at generating small parsers with no external dependencies therefore it was the ideal candidate to adapt for GWT.

These tutorials demonstrate the steps required so that JavaCC and GWT together can be leveraged so that JavaScript can be targeted.

Building a runtime that avoids non-emulated GWT classes can be a challenge, but it is achievable. Also achievable are C# parsers generated from the same codebase translated via sharpen, but that is a significantly more complex tutorial.

Github Location
  • Source corresponding to this tutorial has been posted on github here. This includes 6.1.0 Release Candidate 1 of JavaCC and corresponding license files for the sake of convenience.