[antlr-interest] Help please

wael sellami wael.sellami at gmail.com
Thu Mar 24 09:22:14 PDT 2011


Hello,
My name is Wael Sellami, I am a computer science researcher in Redcad
laboratory, university of sfax, tunisia, master level.
I use ANTLR to transform xml code to PROMELA code as input language of the
model-checker SPIN.
I have a problem in defining of nested bloc in my output. In fact, I can not
organize the output code as the form of input code. Below, there is a
exemple of small grammar and the rule of translation:

grammar xml;
options {
 output = AST;
 ASTLabelType = CommonTree;
 backtrack=true;
}

tokens {
ROOTDEF; NAMEDEF; XMLTAGDEF; TITLEDEF; BOOKDEF; AUTHORDEF; }

system : root+;

root :  '<books>' (xmltag)+ '</books>'
       -> ^(ROOTDEF (xmltag)+);

xmltag : book -> ^(XMLTAGDEF book)     | title -> ^(XMLTAGDEF title)    |
author -> ^(XMLTAGDEF author);

book : '<book' name '>' (xmltag)+ '</book>'
-> ^(BOOKDEF name (xmltag)+);

title    : '<title' name '>' (ID)+ '</title>'
-> ^(TITLEDEF name (ID)+);

author    : '<author' name '>' (ID)+ '</author>'
-> ^(AUTHORDEF name (ID)+);


name     :  'name="' ID '"'
          -> ^(NAMEDEF ID);

ID  : ('a'..'z'|'A'..'Z'|'0'..'9') ('a'..'z'|'A'..'Z'|'0'..'9')*;
WS  : ( ' ' | '\t' | '\r' | '\n')* { $channel = HIDDEN; } ;


Then I have defined the tree walker:


tree grammar Translate;
options {
 tokenVocab = xml;
 ASTLabelType = CommonTree;
 output=template;

}

@members {
List<String> columnsactbpel = new ArrayList<String>();
}

system : root+;
root :  ^(ROOTDEF (xmltag)+);
xmltag : ^(XMLTAGDEF book) | ^(XMLTAGDEF title) | ^(XMLTAGDEF author);
book : ^(BOOKDEF name (xmltag)+)
{
System.out.println("Begin book tag");
System.out.println("END book tag");
};

title    returns [String tit]
    : ^(TITLEDEF name (ID)+)
{ System.out.println($name.namevar);};

author    returns [String auth]
    : ^(AUTHORDEF name (ID)+)
{ System.out.println($name.namevar);};

name     returns [String namevar]
    : ^(NAMEDEF (ID)+)
    { $namevar = $ID.text;};


After that, I have tried with this input code:

<books>
    <title name="AAA"> Data base </title>
    <author name="BBB"> XML </author>

    <book name="a">
        <title name="CCC"> Data base </title>
         <author name="DDD"> XML </author>

                <book name="a">
                    <title name="EEE"> Data base </title>
                    <author name="FFF"> XML </author>
                </book>
    </book>
</books>


So, the result of this translation is:

AAA
BBB
CCC
DDD
EEE
FFF
Begin book tag
END book tag
Begin book tag
END book tag


But normally, if we follow the input xml code the résult must be:

AAA
BBB

Begin book tag
CCC
DDD

    Begin book tag
    EEE
    FFF
    END book tag

END book tag




Please, can you help me to solve this problem.
Thank you in advance
Wael


-- 
---------------------------------------------
* Mr. Wael Sellami
* Master student
* Research Unit on Development and Control of Distributed Applications
(ReDCAD)
============================


More information about the antlr-interest mailing list