[antlr-interest] Are ANTLR grammars space sensitive?

Edson Tirelli tirelli at post.com
Wed Apr 9 09:26:39 PDT 2008


   All,

   While playing with ANTLR during the weekend in my quest for learning
AST/Tree grammars I found an odd situation. The following rule generates
compilation errors:

key_sentence
@init {
        String text = "";
}
    : variable_definition
    | cb=key_chunk { text = $cb.text; }
    -> ^(VT_LITERAL[ $cb.start, text ] )
    ;

   Errors are:

[13:15:53] /tmp/antlrworks/DSLMapParser.java:1313: illegal start of
expression
[13:15:53]                         root_1 =
(Object)adaptor.becomeRoot(adaptor.create(VT_LITERAL,  , ((Token)cb.start),
text ), root_1);
[13:15:53]
^
[13:15:53] /tmp/antlrworks/DSLMapParser.java:1313: ')' expected
[13:15:53]                         root_1 =
(Object)adaptor.becomeRoot(adaptor.create(VT_LITERAL,  , ((Token)cb.start),
text ), root_1);
[13:15:53]
^
[13:15:53] 2 errors

   Removing the spaces from inside the [], fixes the problem:

    -> ^(VT_LITERAL[$cb.start, text] )

   Is this known behavior? bug? I found no reference about it.

   Also, on a related question, as you can see in the rule, I'm using a
dummy "text" variable to capture the text matched by the "key_chunk" rule.
That is because simply writing:

    -> ^(VT_LITERAL[$cb.start, $cb.text] )

   Also generate compilation errors:

[13:23:12] /tmp/antlrworks/DSLMapParser.java:1313: illegal start of
expression
[13:23:12]                         root_1 =
(Object)adaptor.becomeRoot(adaptor.create(VT_LITERAL, ((Token)cb.start),  ,
input.toString(cb.start,cb.stop)), root_1);
[13:23:12]
^
[13:23:12] /tmp/antlrworks/DSLMapParser.java:1313: ')' expected
[13:23:12]                         root_1 =
(Object)adaptor.becomeRoot(adaptor.create(VT_LITERAL, ((Token)cb.start),  ,
input.toString(cb.start,cb.stop)), root_1);
[13:23:12]
^
[13:23:12] 2 errors


    Any idea if it is possible to avoid creating the dummy variable? I just
prefer to keep the grammar as clean as possible...

    Thanks
           Edson

-- 
Edson Tirelli
JBoss Drools Core Development
Office: +55 11 3529-6000
Mobile: +55 11 9287-5646
JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080409/355f00f6/attachment.html 


More information about the antlr-interest mailing list