[antlr-interest] Unnecessary Java output file diffs

Tom Ball tball at google.com
Wed May 20 12:06:33 PDT 2009


Our distributed build system compares output files and only copies back the
ones that have changed since the last build.  We've noticed that our
Antlr->Java step always gets copied, for two reasons.  First, because a
timestamp is in the output file's header -- is that necessary as a default?
More useful might be a timestamp variable that can be put a .g file if
desired, and expanded during generation.

We think the second reason might be a bug, as here is some example output
from a common, unchanged source file:

@@ -2531,7 +2531,7 @@
                        int index13_13 = input.index();
                        input.rewind();
                        s = -1;
-                        if (
(((input.LT(1).getText().equals("gte"))||(input.LT(1).getText().equals("lte"))||(input.LT(1).getText().equals("gt"))||(input.LT(1).getText().equals("lt"))||(input.LT(1).getText().equals("eq"))||(input.LT(1).getText().equals("ne"))))
) {s = 2;}
+                        if (
(((input.LT(1).getText().equals("gte"))||(input.LT(1).getText().equals("lte"))||(input.LT(1).getText().equals("lt"))||(input.LT(1).getText().equals("gt"))||(input.LT(1).getText().equals("eq"))||(input.LT(1).getText().equals("ne"))))
) {s = 2;}

                        else if ( (true) ) {s = 8;}

Shouldn't the order of the token comparisons be the same for every
generation?  I guess it doesn't matter in this example since the string can
only have one match, but my hunch is that with other rules comparison order
matters.  Here is the rule which generated the above:

comparator returns [Operation value]
  : ((eqToken | '=') { $value = Operation.EQ; })
  | ((neToken | '!=') { $value = Operation.NEQ; })
  | ((gtToken | '>') { $value = Operation.GT; })
  | ((gteToken | '>=') { $value = Operation.GTE; })
  | ((ltToken | '<') { $value = Operation.LT; })
  | ((lteToken | '<=') { $value = Operation.LTE; })
  ;

// Pseudo-tokens
eqToken : {input.LT(1).getText().equals("eq")}? NCNAME ;
neToken : {input.LT(1).getText().equals("ne")}? NCNAME ;
ltToken : {input.LT(1).getText().equals("lt")}? NCNAME ;
lteToken : {input.LT(1).getText().equals("lte")}? NCNAME ;
gtToken : {input.LT(1).getText().equals("gt")}? NCNAME ;
gteToken : {input.LT(1).getText().equals("gte")}? NCNAME ;

NCNAME
  : (LETTER | '_') (LETTER | NONLETTER | '.' | '-' | '_')*
  ;

Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090520/efcbb457/attachment.html 


More information about the antlr-interest mailing list