[antlr-interest] Unexpected text

Bart Kiers bkiers at gmail.com
Thu Sep 22 00:19:53 PDT 2011


Hi Graham,

Inside your parser-grammar, $typeSpec is the object that is returned by the
parser rule (RuleReturnScope). It includes, among others, the start- and
end-token and the tree of the rule. Using `.text`, which is short for
`getText()` will get the source of your input from `start` to `end` for that
rule. If you'd print `$typeSpec.tree` instead, you'd see the AST without
the "as" text.

Regards,

Bart.


On Thu, Sep 22, 2011 at 6:41 AM, Graham Mer <gd.antlr at gmail.com> wrote:

> "Hi everybody!"
>  - Dr. Nick.
>
> I have a larger grammar, a tiny portion of which is attached below.
> Everything works as expected, except for one thing. In the tree
> grammar, note the following rule:
>
> decl: ID typeSpec { System.out.println( "decl id=" + $ID + ";type=" +
> $typeSpec.text );}
>
> Given the following input:
>  Dim foo as String
>  Dim bar as Int
>
> I expect it to print:
>  decl id=foo;type=String
>  decl id=bar;type=Int
>
> But instead I get:
>  decl id=foo;type=as String
>  decl id=bar;type=as Int
>
> The string trees all look as expected, like "(Dim foo String) (Dim bar
> Int)", but I get the extra "as" in the type node, even though I
> exclude the "AS" node when building the tree in the following rule:
>
> asType: AS! type;
>
>
> I get the same results when the parser is generated by ANTLR 3.3 and
> 3.4. What am I doing wrong?
>
>
> Here are the grammars:
>
> grammar test;
>
> options{ output=AST; }
>
> start   :       varDef+ EOF;
>
> varDef  :       DIM^ ID asType;
>
> asType  :       AS! type;
>
> type    :       STRING_T | INT_T;
>
>
> AS      :       ('A'|'a')('S'|'s');
>
> DIM     :       ('D'|'d')('I'|'i')('M'|'m');
>
> STRING_T:       ('S'|'s')('T'|'t')('R'|'r')('I'|'i')('N'|'n')('G'|'g');
>
> INT_T   :       ('I'|'i')('N'|'n')('T'|'t');
>
> ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
>
> WS  :   ( ' '
>        | '\t'
>        | '\r'
>        | '\n'
>        ) {$channel=HIDDEN;}
>    ;
>
>
>
> tree grammar testTree;
>
> options
> {
>        tokenVocab=test;
>        ASTLabelType=CommonTree;
>        output=AST;
> }
>
> start   :       statement+
>        ;
>
> statement
>        :       ^(DIM decl)
>        ;
>
> decl    :       ID typeSpec { System.out.println( "decl id=" + $ID +
> ";type=" +
> $typeSpec.text );}
>        ;
>
> typeSpec:       STRING_T | INT_T
>        ;
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list