Search code examples
javajavascriptjavaccjavaparser

How to modularize a JavaCC grammar file (.jj)?


I am learning compiler construction and want to implement the JavaScript grammar using JavaCC.

(I have already written my own JavaScript CodeModel which allows programmatic construction of the JavaScript code, now I want to write a JavaCC-based parser counterpart for that.)

My question is, is there a way to modularize the JavaCC grammar (.jj-file) into several files?

I have very good experience with the JavaParser so I am learning from their java_1_5.jj grammar. However, this is a 3000+ LoC file which is a bit hard to comprehend.

I would like to divide the grammar file into several parts so that it's easier to hande and understand. My Google searchen on "javacc modular", "javacc include", "javacc import" brought me some cryptic results which did not help much.

To be specific, how would I move the definition of the IDENTIFIER (lines 380-1081) to another file?


Solution

  • There is no way built in to JavaCC to modularize .jj files. The best thing to do is often to use JJT, as this allows you to move all actions out of the grammar file. If you don't want to use JJT, the next best thing may be to use the builder pattern.

    If you just want an include facility, there are many preprocessors that can be used.