[antlr-interest] Problem with semanic action

Nicola Cuomo ncuomo at gmail.com
Mon Sep 26 20:29:46 PDT 2005


Hi again i've another problem :)

I've the following grammar piece (semantic action removed)

---
structuredMessage returns[Message *rmsg]
        : bm1=basicMessage (PAIR bm2=basicMessage )*
        ;

basicMessage returns[Message *rmsg]
        : VAR_IDENT (PRIME)?
        | CONST_IDENT
        | VAR_IDENT LPAREN structuredMessage RPAREN
        ;
---

Message is a tree like structure

struct Message
{
        int type;
        
        string value;
        
        Message *op1;
        Message *op2;
};

Using  semantic  action I can make and fill a Message structure in the
basicMessage role the problem is with the structuredMessage role.

In  my idea the subrole identified by bm1 should go into the op1 field
of  the  returned  Message structure and the subrole identified by bm2
should  go into the op2 field.

For  a message like (the "." is PAIR) A1.A2.A3 the structure should be
like (ASCII art incoming :)
   |
  / \
A1  / \
  A2  A3

The problem is that the role iterate the star part making difficult to
build a tree like structure.

So i was wondering how i can fill the Message structure?

The action i've written was something like

structuredMessage returns[Message *rmsg]
{
        Message *tbasicMessage = NULL;
        Message *tbasicMessages = NULL;
        Message *tmsg = NULL;
}
        : tbasicMessage=basicMessage 
                {  
                        tmsg = new Message;
                        tmsg->op1 = tbasicMessage;
                        tmsg->op2 = NULL;
                        rmsg = tmsg;
                }
                (PAIRING tbasicMessages=basicMessage
                {  
                        tmsg = new Message;
                        tmsg->op1 = tbasicMessage;
                        tmsg->op2 = tbasicMessages;
                        rmsg = tmsg;
                } )*
        ;
        
I really don't know what to do :(
        
Regards
-- 
 Nicola                          mailto:ncuomo at gmail.com



More information about the antlr-interest mailing list