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

Sharath MS sharathms at gmail.com
Wed Oct 22 08:09:42 PDT 2008


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]

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})
    ;

SYM
    : ('a'..'z')+ ('0'..'9')*
    ;

--------------------------------------------------

Foo.stg:

group Foo;
printfoo(symbols) ::= << <symbols; separator=" | "> >>



--------------------------------------------------

Foo.py:


import sys
import antlr3
import stringtemplate3
from FooParser import FooParser
from FooLexer import FooLexer

inputFileName = sys.argv[1]
templateFileName = sys.argv[2]

templates = stringtemplate3.StringTemplateGroup(
    file=open(templateFileName, 'r'), lexer='angle-bracket'
)

input = antlr3.ANTLRFileStream(inputFileName)
lexer = FooLexer(input)
tokens = antlr3.TokenRewriteStream(lexer)
parser = FooParser(tokens)
parser.templateLib = templates
parser.prog()
print tokens.toString()

----------------------------------------------------


Best Regards,
Sharath MS


More information about the antlr-interest mailing list