[antlr-interest] Quick intro to Python backend
Ben
misc7 at emerose.org
Mon Sep 7 18:31:03 PDT 2009
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;}
;
More information about the antlr-interest
mailing list