[antlr-interest] Using custom token in rewrite

none none 6yearold at gmail.com
Thu Nov 12 11:01:10 PST 2009


Hi.
I have following input: four hex digits, representing 32-bit integer value.
It can be defined:
11 22 AA BB
or undefined:
XX XX XX XX

and here is according rule:
dword_ :  BYTE_ BYTE_ BYTE_ BYTE_;

Now i'm trying to construct AST from this rule, which looks like (INT32
0xBBAA2211) or (INT32 UNDEFINED), where INT32 and UNDEFINED - some imaginary
tokens. I'm understand, that i can't just put $text in -> ^(INT32 $text), so
i tried to construct Token object. And here is what i get:

dword_    :    BYTE_ BYTE_ BYTE_ BYTE_
        {
        //this func parses $text and makes new CommonToken
        Token t=makeDwordToken($text);
        //now i'm popping four BYTE_ tokens from stream_BYTE_
        //if i don't do that, i'll get a first byte from dword_ in my tree
        stream_BYTE_.nextNode();
        stream_BYTE_.nextNode();
        stream_BYTE_.nextNode();
        stream_BYTE_.nextNode();

        //now pushing my handmade token
        stream_BYTE_.add(t);
        }
        -> ^( INT_ {stream_BYTE_.nextNode()})
       //and finally make it a tree's child
    ;

Now i'm doubting in what i have wrote, but still can't find other way to
achieve this goal.
Any ideas?

P.S. Sorry for if my English looks terrible.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091112/9e951215/attachment.html 


More information about the antlr-interest mailing list