[antlr-interest] java.g parenthetical built-in type class literal expression

Ray Waldin ray at waldin.net
Fri Oct 3 11:58:17 PDT 2003


Hi everyone,

Parenthetical built-in type class literal expressions, such as the one in:

     Class c = (int.class);

break the parser.  The fix is listed below.

-Ray


--- old/java.g  2003-09-25 14:24:54.000000000 -0700
+++ java.g      2003-10-03 11:57:11.000000000 -0700
@@ -794,27 +794,20 @@
 unaryExpressionNotPlusMinus
        :       BNOT^ unaryExpression
        |       LNOT^ unaryExpression
-
-       |       (       // subrule allows option to shut off warnings
-                       options {
-                               // "(int" ambig with postfixExpr due to 
lack of sequence
-                               // info in linear approximate LL(k).  
It's ok.  Shut up.
-                               generateAmbigWarnings=false;
-                       }
-               :       // If typecast is built in type, must be numeric 
operand
-                       // Also, no reason to backtrack if type keyword 
like int, float...
-                       lpb:LPAREN^ {#lpb.setType(TYPECAST);} 
builtInTypeSpec[true] RPAREN!
-                       unaryExpression
-
-                       // Have to backtrack to see if operator 
follows.  If no operator
-                       // follows, it's a typecast.  No semantic 
checking needed to parse.
-                       // if it _looks_ like a cast, it _is_ a cast; 
else it's a "(expr)"
-               |       (LPAREN classTypeSpec[true] RPAREN 
unaryExpressionNotPlusMinus)=>
-                       lp:LPAREN^ {#lp.setType(TYPECAST);} 
classTypeSpec[true] RPAREN!
-                       unaryExpressionNotPlusMinus
-
-               |       postfixExpression
-               )
+       |       // use predicate to skip cases like:  (int.class)
+               // rewind if builtInTypeSpec is not immediately followed 
by RPAREN
+               (LPAREN builtInTypeSpec[true] RPAREN) =>
+               lpb:LPAREN^ {#lpb.setType(TYPECAST);} 
builtInTypeSpec[true] RPAREN!
+               unaryExpression
+
+       |       // Have to backtrack to see if operator follows.  If no 
operator
+               // follows, it's a typecast.  No semantic checking 
needed to parse.
+               // if it _looks_ like a cast, it _is_ a cast; else it's 
a "(expr)"
+               (LPAREN classTypeSpec[true] RPAREN 
unaryExpressionNotPlusMinus)=>
+               lp:LPAREN^ {#lp.setType(TYPECAST);} classTypeSpec[true] 
RPAREN!
+               unaryExpressionNotPlusMinus
+
+       |       postfixExpression
        ;
 
 // qualified names, array expressions, method invocation, post inc/dec
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20031003/525f7f47/attachment.html


More information about the antlr-interest mailing list