[antlr-interest] Help please
wael sellami
wael.sellami at gmail.com
Tue Mar 29 03:46:48 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; CHAPTDEF;}
grammar xml;
options {
output = AST;
ASTLabelType = CommonTree;
backtrack=true;
}
tokens {
ROOTDEF; NAMEDEF; XMLTAGDEF; TITLEDEF; BOOKDEF; AUTHORDEF; CHAPTDEF;TITAUTH;
}
system : root+;
root : '<books>' (xmltag)+ '</books>'
-> ^(ROOTDEF (xmltag)+);
xmltag : book -> ^(XMLTAGDEF book) | title -> ^(XMLTAGDEF title) | author ->
^(XMLTAGDEF author) | chapter -> ^(XMLTAGDEF chapter);
book : '<book' name '>' (xmltag)+ '</book>'
-> ^(BOOKDEF name (xmltag)+) ;
title : '<title' name '>' (ID)+ '</title>'
-> ^(TITLEDEF name (ID)+);
author : '<author' name '>' (ID)+ '</author>'
-> ^(AUTHORDEF name (ID)+);
titleorother : title ->^(TITAUTH title) | author ->^(TITAUTH author);
chapter : '<chapter' name '>' (titleorother)+ '</chapter>'
-> ^(CHAPTDEF name (titleorother)+);
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> columnstagxml = new ArrayList<String>();
}
system : root+;
root : ^(ROOTDEF (xmltag)+);
xmltag : ^(XMLTAGDEF book) | ^(XMLTAGDEF title) | ^(XMLTAGDEF author) |
^(XMLTAGDEF chapter);
book : ^(BOOKDEF name
{System.out.println("Begin book tag");}
(xmltag)+
{System.out.println("END book tag");}
)
;
title returns [String tit]
: ^(TITLEDEF name (ID)+)
{ System.out.println("The tilte of the book is:" + $name.namevar);
$tit=$name.namevar;
};
author returns [String auth]
: ^(AUTHORDEF name (ID)+)
{ System.out.println("The author of the book is:" +$name.namevar);
$auth=$name.namevar;
};
titleorother returns [String titlauth]
: ^(TITAUTH title){
$titlauth= '\n' + "The title of this chapter is:" + $title.tit;
columnstagxml.add($titlauth);
}
| ^(TITAUTH author)
{
$titlauth= '\n' + "The author of this chapter is:" + $author.auth;
columnstagxml.add($titlauth);
}
;
chapter : ^(CHAPTDEF name (titleorother)+)
{
Object[] arraycolumnstagxml = columnstagxml.toArray();
for(int i = 0; i < arraycolumnstagxml.length; i++)
{
String x2=arraycolumnstagxml[i].toString();
System.out.println(x2);
}
};
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>
<chapter name="a">
<title name="EEE"> Data base </title>
<author name="FFF"> XML </author>
<author name="GGG"> XML </author>
<title name="HHH"> Data base </title>
</chapter>
</book>
</books>
So, the result of this translation which is false because there is the
displaying of title of the book, and may be the strcuture is wrong because
when ANTLR finds
a tag title shows ("The tilte of the book is:" + $name.namevar); but I need
to how reconize the source for each called tag.
The tilte of the book is:AAA
The author of the book is:BBB
Begin book tag
The tilte of the book is:CCC
The author of the book is:DDD
The tilte of the book is:EEE
The author of the book is:FFF
The author of the book is:GGG
The tilte of the book is:HHH
The title of this chapter is:EEE
The author of this chapter is:FFF
The author of this chapter is:GGG
The title of this chapter is:HHH
END book tag
But it's wrong, because normally, if we follow the input xml code the result
must be as follows :
The tilte of the book is:AAA
The author of the book is:BBB
Begin book tag
The tilte of the book is:CCC
The author of the book is:DDD
The title of this chapter is:EEE
The author of this chapter is:FFF
The author of this chapter is:GGG
The title of this chapter is:HHH
END book tag
Please, can you help me to distinguish the source tag of called tag
Thank you in advance for solving this problem.
Wael
--
---------------------------------------------
* Mr. Wael Sellami
* Master student
* Research Unit on Development and Control of Distributed Applications
(ReDCAD)
============================
-------------- next part --------------
A non-text attachment was scrubbed...
Name: XML2PROMELA.rar
Type: application/rar
Size: 44376 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20110329/971b4d59/attachment.bin
More information about the antlr-interest
mailing list