1

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?

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • There is really no way 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 just want an include facility, there are many preprocessors that can be used. – Theodore Norvell Nov 03 '14 at 19:21
  • @TheodoreNorvell Would you please post your as an answer? Thank you. – lexicore Nov 03 '14 at 19:48
  • If you don't want to use JJT, the next best thing may be to use the builder pattern. – Theodore Norvell Nov 03 '14 at 20:11

3 Answers3

1

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.

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
  • You're reading my mind. I am using the builder battern. https://github.com/highsource/javascript-codemodel/blob/master/parser/src/main/java/org/hisrc/jscm/parser/JSCodeModelBuilder.java – lexicore Nov 03 '14 at 20:19
  • Sorry to ask again - could you please point me to a preprocessor which would support inclusion? Somehow I don't manage to find one... – lexicore Nov 19 '14 at 05:35
  • cpp or m4 both do file inclusion. You could also just concatenate a number of files together using cat or ANT's concat task. – Theodore Norvell Nov 20 '14 at 01:28
0

Yes, you can create various classes and pass parameters by creating objects and send to the objects in try catch inside javacc file by which it will look modular.

Dipcoder
  • 15
  • 5
  • So how exactly do I modularize the JavaCC grammar (.jj-file) into several files? – lexicore Feb 22 '19 at 12:59
  • Sample.jj---> //begin class xyz{ modularclass t = new modularclass(......); { try{ S.o.pln("Starting..."); abc(); } catch(){ } } } PARSER_END (xyz) SKIP : { ... } TOKEN: { ... } void abc(): { } { (a1())* } void a1() : { } { { modularclass_1 t1 = new modularclass_1(...); } } //end seperate files for---> modularclass.java--> modularclass_1.java-> – Dipcoder Feb 23 '19 at 13:21
  • 1
    I am asking how to split a huge `Sample.jj` in several smaller `Sample01.jj`, `Sample02.jj` etc. – lexicore Feb 25 '19 at 10:32
0

I'm aware that the question was posed nearly six years ago, but I'll answer even so, since people will be looking for an answer to this now and again.

The most advanced version of JavaCC is JavaCC 21and JavaCC 21 does have (among other things) an INCLUDE directive that, as best I can guess, is exactly what you are looking for.

There are actually quite a few other features that JavaCC21 that are not present in the legacy JavaCC project. Here is a biggie: the longstanding bug in which nested syntactic lookahead does not work correctly has been fixed. See here.

revusky
  • 23
  • 2