[antlr-interest] Quick intro to Python backend

Martijn Reuvers martijn.reuvers at gmail.com
Tue Sep 8 11:18:06 PDT 2009


Yeah you should try to use as less as target-language code as possible
in your grammars to keep them readable and maintainable I think. But
still actions { } are generally needed to drive the code but these
could call to support-type of constructs e.g.. This was only a quick
and dirty example, but it did match your described expressions. =)
getResult() could actually be called on the parser to return the
result instead of printing it.

On Tue, Sep 8, 2009 at 3:31 AM, Ben<misc7 at emerose.org> wrote:
> Thanks Martijn,
>
> I think I tried to do what you did by overriding lexer::members, and members (and rulecatch) but I think I may be doing it wrong.  Once difference though is that I'm trying to encapsulate the ANTLR code as much as possible---it's not very convenient to write Python inside AntlrWorks, and I've read that the Python runtime is subject to change so I want to use the recommended methods.  So I think this may be more of a Python-specific issue.
>
> --
> Ben Escoto
>
> ----------------- Original message -----------------
> From: Martijn Reuvers <martijn.reuvers at gmail.com>
> To: Ben <misc7 at emerose.org>
> Date: Mon, 7 Sep 2009 20:46:22 +0200
> Hi Ben,
>
> I can't help you with the Python part, but below is a quick & dirty
> grammar for Java which more or less does do what you want.
>
> Most likely your errors will come from the lexer, hence I throw the
> exception there to show you. So in your python program catching that
> should tell you it was wrong. The rest of the grammar fills a list
> with text or numbers that are parsed, and prints it in the @after of
> begin rule (which is only there so a quick printout was possible).
>
> Martijn
>
>
> grammar test001;
>
> @lexer::members {
>
>  @Override
>        public void reportError(RecognitionException e) {
>                throw new RuntimeException(e);
>        }
>
> }
>
> @members {
>
>        java.util.List result = new java.util.ArrayList();
>
>        public List getResult() {
>                return result;
>        }
> }
>
> begin
> @after {
>        System.out.println("RESULT=" + result);
> }
>        :       expr
>        ;
>
>
> expr
>        :       what (DOT expr)?
>        ;
>
> what
>        :       TEXT
>                {
>                        if($TEXT.text != null) {
>                                result.add($TEXT.text);
>                        }
>                }
>        | NUMBER
>                {
>                        if($NUMBER.text != null) {
>                                result.add($NUMBER.text);
>                        }
>                }
>        ;
>
> DOT
>        :       '.'
>        ;
>
> TEXT
>        : 'A'..'Z'+
>  ;
>
> NUMBER
>        :       '0'..'9'+
>        ;
>
> WS  :   ( ' '
>        | '\t'
>        | '\r'
>        | '\n'
>        ) {$channel=HIDDEN;}
>    ;
>
>
> 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