[antlr-interest] Anyone have some good Python examples?

Bart Kiers bkiers at gmail.com
Fri Feb 5 22:20:34 PST 2010


Thomas,

Especially read the paragraph 'Element Labels' on page 95 carefully: that's
what you need to print the part(s) you're interested in.

Regards,

Bart.


On Fri, Feb 5, 2010 at 10:59 PM, Bart Kiers <bkiers at gmail.com> wrote:

> Thomas,
>
> I highly recommend *carefully* reading the book.
> Forgive me for saying, but I doubt that you actually read it. And if so,
> you could only have skimmed through it (you have ordered it only
> yesterday!).
>
> Regards,
>
> Bart.
>
>
>
> On Fri, Feb 5, 2010 at 10:23 PM, Thomas Raef <TRaef at wewatchyourwebsite.com
> > wrote:
>
>> Okay. I would like to thank you all for allowing me to figure out the
>> errors of my efforts. No sarcasm.
>>
>> I found out that since Bart was nice enough to get me started, even
>> though it was a Java example, I didn't realize where my Python program
>> was starting and what was antlr code.
>>
>> I discovered that my line: insideFunction = False; was causing the java
>> compilation to complain so I moved that inside my def. I also realized
>> that the code: $f.text.toString()); is java and is not part of antlr.
>> Since I was using language=Python, it had no idea what I was trying to
>> do.
>>
>> Now when I run my program, all I get is: STATEMENT ->
>> (JavaScriptParser.statement_return object at 0x02168C90)
>>
>> What I want to see is the line of javascript code from the .js file I'm
>> reading from prefaced with either STATEMENT or FUNCTION.
>>
>> Can anyone help me with that?
>>
>> I've watched the tutorials and read through the book, but I cannot for
>> the life of me figure out how to get the line of javascript to print.
>>
>> Please help...
>>
>> Thomas J. Raef
>>
>>
>>
>> > -----Original Message-----
>> > From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> > bounces at antlr.org] On Behalf Of Thomas Raef
>> > Sent: Thursday, February 04, 2010 5:29 PM
>> > To: antlr-interest at antlr.org
>> > Subject: [antlr-interest] Anyone have some good Python examples?
>> >
>> > 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
>> >
>> >
>> >
>> >
>> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> > email-address
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>
>


More information about the antlr-interest mailing list