[antlr-interest] Anyone have some good Python examples?
Thomas Raef
TRaef at wewatchyourwebsite.com
Thu Feb 4 15:28:47 PST 2010
I'm trying to get my simple program working and I keep running into
errors.
In my JavaScript.g file I've set the language=Python;
I have a @members section like:
@members
{
insideFunction = False;
def prettyPrint(type, text):
text = text.replace("\r", '') //to remove carriage
returns in my javascript file (mt.js)
text = text.replace("\n", '') // to remove new lines
if len(text) > 55:
start = text[:40]
end = text[-10:]
text = start+' ... '+end
print type+' -> '+text
}
In a JavaScript.G file I have in the functionBody section:
functionBody
: '{' {insideFunction=True;) LT!* sourceElements LT!*
'}' {insideFunction=False;}
;
In sourceElement section I have:
sourceElement
: f=functionDeclaration { prettyPrint("FUNCTION ",
$f.text.toString()); }
| s=statement { prettyPrint("STATEMENT ",
$s.text.toString()); }
;
Then in python I'm doing:
import antlr3
from JavaScriptLexer import JavaScriptLexer
from JavaScriptParser import JavaScriptParser
char_stream = antlr3.ANTLRFileStream("mt.js")
lexer = JavaScriptLexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = JavaScriptParser(tokens)
parser.program()
it barfs:
prettyPrint("STATEMENT", ((s is not None) and
[self.input.toString(s.start, s.stop)] or [None])[0].toString());
NameError: global name 'prettyPrint' is not defined
I'm trying, but failing. Can anyone with python experience help me out?
Thank you in advance.
Thomas J. Raef
More information about the antlr-interest
mailing list