[antlr-interest] breaking the Java code out of a grammar

Pete Siemsen siemsen at ucar.edu
Fri May 8 14:02:58 PDT 2009


I have a 800-line ANTLR 3 grammar that works fine.  I use an editor  
TextMate to edit it.  This works, but TextMate doesn't know how to  
syntax-highlight the code.  TextMate can be trained to do so, but it's  
hard because ANTLR grammar files contain 2 mixed languages, ANTLR and  
Java.  Obviously I can live with unhighlighted code, but I notice that  
when editing several files at once with TextMate, I see some Java code  
highlighted and some not.

I know I can use ANTLRWorks, but I'd rather use a single editing tool  
than juggle multiple GUIs.  Besides, my grammar is fairly static -  
it's the interspersed Java that changes.

A while back, someone on this mailing list suggested that my grammar  
might benefit if I were to move all the Java into a separate .java  
file.  In other words, replace each occurrence of a block of Java with  
a simple Java function call, so that the bulk of the Java would move  
to the .java file.

This seems like an interesting idea.

Pros:
It would make the grammar more compact, so it would be much easier to  
"see" the grammar rules and StringTemplate invocations.
I'd be more tempted to train TextMate to highlight ANTLR grammars.
It would keep most of the Java in a .java file, where it can be  
highlighted correctly.

Cons:
It would be yet another source file.
It would add a bunch of argument passing.
It wouldn't deal with grammar rules that contain Java fragments, such  
as this, from my grammar:

methodDeclaration
     : dataType methodName LPAREN  
(methodParameterList{mpexists=true;})? RPAREN SEMICOLON


Anyway, does this idea have any merit?

-- Pete




More information about the antlr-interest mailing list