[antlr-interest] Translating recurring input with python target and stringtemplate

Sharath sharathms at gmail.com
Wed Oct 22 10:37:31 PDT 2008


Jim Idle wrote:
> On Wed, 2008-10-22 at 20:39 +0530, Sharath MS wrote:
>> Hi,
>>
>> I'm using antlr-3.1b2 with Python as my target language along with
>> stringtemplate. I'm trying to translate the following text:
>>
>> a1,aa2,aa3,b1
>>
>> to
>>
>> a1 | aa2 | aa3 | b1
>>
>> basically replacing the commas with pipes.
>>
>> However, the output that I'm getting is something like this:
>>
>> [@0,0:1=u'a1',<4>,1:0] | [@2,3:5=u'aa2',<4>,1:3] |
>> [@4,7:9=u'aa3',<4>,1:7] | [@6,11:12=u'b1',<4>,1:11]
>>     
>
>
> THis looks like a text serialization of the entire token..
>> Seems that the list being passed to the template contains more than
>> just the text. Also, I'm using the rewrite option  and a
>> TokenRewriteStream as i need to add to an existing grammar. Any help
>> in translating this text in this setup, would be greatly appreciated.
>> Thanks!
>>
>> Here is the .g, .stg and .py files for your reference:
>>
>> --------------------------------------------------
>>
>> Foo.g:
>>
>> grammar Foo;
>> options {
>>   language=Python;
>>   output=template;
>>   rewrite = true;
>> }
>>
>> prog: stat+;
>>
>> stat:
>> p+=SYM (',' p+=SYM )*
>>         -> printfoo(symbols={$p})
>>     
>
> Try $p.text
>
>
> Jim 
Hi Jim,

I tried $p.text and on running the python script, this was the result:
AttributeError: 'list' object has no attribute 'text'.

I also tried modifying the grammar such that a csv string is passed to
the template instead of a list, but as expected, the separator treats
the text as one whole item and does not replace the commas with pipes.

Thanks for your time!

Sharath

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20081022/71993514/attachment.bin 


More information about the antlr-interest mailing list