[antlr-interest] [newbie] Creating a new node in a grammar

Eric Craft e.craft at shibasoku.co.jp
Wed Feb 23 17:34:47 PST 2011


Hello all,
     I have modified the C++ grammar to support numbers with units.
     float j = 3.0mV;
     I created an action to replace the 3.0mV with 0.003 using the 
following code in the grammar:
@init
{
   string newNum(" ");
}

inside a rule alternative:
     |       un=UNITNUM //EAC here is where we replace the prefix with 
multiplier
                           {
                           ConvertToNum($un, newNum);
                           printf("UnitNum= %s %s\n", $un.text->chars, 
newNum.c_str());
                           $un.text = newNum;
                           } -> {newNum} // this is where I get lost

So how do I create the new pANTLR_BASE_TREE node and assign it the 
string of interest?

I was thinking something like the following, but I don't know how to 
assign the new token to the tree node.

            pANTLR3_COMMON_TOKEN  newTok = new ANTLR3_COMMON_TOKEN(un);
            pANTLR3_STRING newString = new ANTLR3_STRING();
            newString->set(newString, newNum.c_str());
             newTok->setText(newTok, newString);
            pANTLR3_BASE_TREE newNumTree = new ANTLR3_COMMON_TREE(<put 
in the token?>);

Any help would be greatly appreciated!

-- 
Eric Craft
ShibaSoku Co., Ltd.
Software Engineer
848 Wakamatsu-cho
Tokorozawa, Saitama 359-0032 Japan
TEL 81-4-2994-1154
e.craft at shibasoku.co.jp



More information about the antlr-interest mailing list