2

i try to run JSON2XML_ST ike this:

bab@maz:~/tpantlr2-code/code/listeners$ antlr4 JSON.g4 
bab@maz:~/tpantlr2-code/code/listeners$ javac JSON2XML_ST.java 
bab@maz:~/tpantlr2-code/code/listeners$ java JSON2XML_ST t.json 

but a i got :

(json (object { (pair "description" : (value "An imaginary server config file")) ,      
(pair "logs" : (value (object { (pair "level" : (value "verbose")) , (pair "dir" :  
(value "/var/log")) }))) , (pair "host" : (value "antlr.org")) , (pair "admin" : (value 
(array [ (value "parrt") , (value "tombu") ]))) , (pair "aliases" : (value (array [ 
]))) }))
Exception in thread "main" java.lang.IllegalArgumentException: No such group file: XML.stg
at org.stringtemplate.v4.STGroupFile.<init>(STGroupFile.java:69)
at org.stringtemplate.v4.STGroupFile.<init>(STGroupFile.java:48)
at JSON2XML_ST$XMLEmitter.<init>(JSON2XML_ST.java:45)
at JSON2XML_ST.main(JSON2XML_ST.java:140)

why ?? can anybody help me? thank you.

2 Answers2

2

This is an XML.stg that can be used with JSON2XML_ST.java. The source download still doesn't contain XML.stg. It is however a good learning exercise to come up with this file yourself. It was for me.

group XML;

empty() ::= ""

value(x) ::= "<x>"

object(fields) ::= <<

<fields; separator="\n">

>>

enclose_element(x) ::= <<
\<element><x>\</element>
>>

array(values) ::= <<

<values:enclose_element(); separator="\n">

>>

tag(name,content) ::= <<
\<<name>\><content>\</<name>\>
>>
kraz
  • 21
  • 2
1

The problem is that XML.stg is not part of the source. i.e. when you run the example the file XML.stg does not exist and therefore cannot be found, hence the error of No such group file.

Exception in thread "main" java.lang.IllegalArgumentException: No such group file: XML.stg

This seems to be a known problem and has been reported in the ANTLR errata: http://pragprog.com/titles/tpantlr2/errata

50831:

There is a reference to JSON2XML_ST.java source which uses StringTemplate for XML translation. But in the source code itself there is reference to the XML.stg file which absent in the book source archive. JSON2XML_ST.java (line 45): STGroup templates = new STGroupFile("XML.stg"); It would be nice if you put it to archive, since (IMHO) it's not very easy to find it elsewhere. Thanks.

There is a reference to xml.stg here: http://www.antlr.org/wiki/plugins/viewsource/viewpagesrc.action?pageId=16220704 which you could use.

Create a new file and call it XML.stg and put in the following contents:

group XML;

file(props) ::= <<
    \<properties>
    <props; separator="\n">
    \</properties>
>>

prop(ID,v) ::= "\<property id=\"<ID>\"><v>\</property>"

Then rerun the example as you already have and it might work.

Har
  • 3,727
  • 10
  • 41
  • 75