[antlr-interest] Python How To Convert from Java
Remy
bassglider at gmail.com
Fri Feb 26 03:09:53 PST 2010
I'm having trouble understanding how I would convert the following
java code to do the same thing in python so I can have somewhere to
start. As you can see the bottom statement of the java file would
print out the TABLE name. I am trying to do the same thing in python
with my AST. I think I'm a line or two away from getting this.
==============
>>>> JAVA <<<<<
==============
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
public class TestSQL {
public static void main(String[] args) throws Exception {
CharStream input = null;
if ( args.length>0 ) {
input = new ANTLRFileStream(args[0]);
}
else {
input = new ANTLRInputStream(System.in);
}
// BUILD AST
sqltestLexer lex = new sqltestLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lex);
sqltestParser parser = new sqltestParser(tokens);
sqltestParser.createtablestmt_return r = parser.createtablestmt();
System.out.println("tablename ="+((Tree)r.tree).getChild(2).toString());
//System.out.println("tree="+((Tree)r.tree).toStringTree());
}
}
===================
>>>>> PYTHON <<<<<<
===================
import antlr3
import antlr3.tree
from sqltestLexer import sqltestLexer
from sqltestParser import sqltestParser
#import sqltest ? won't import
import sys
#from sqltest import sqltest
#sys.argv[1]
char_stream = antlr3.ANTLRStringStream("SELECT * FROM BOOKS;")
lexer = sqltestLexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = sqltestParser(tokens)
r = parser.createtablestmt_return()
# this is the root of the AST
root = r.tree
nodes = antlr3.tree.CommonTreeNodeStream(root)
nodes.setTokenStream(tokens)
#walker = sqltest(nodes)
# STUCK!
=================
>>>>>> G <<<<<<<<<
=================
grammar sqltest;
options {output=AST;
language=Python;}
// Lexer Rules
tokens {
COMMA = ',';
LPAR = '(';
RPAR = ')';
TERMINATOR = ';';
}
@lexer::members {
def reportError(self, e):
raise e
}
@members {
def mismatch(self, input, ttype, follow):
raise MismatchedTokenException(ttype, input)
def recoverFromMismatchedSet(self, input, e, follow):
raise e
}
@rulecatch {
except RecognitionException, e:
raise
}
SQLCHAR : 'char' | 'CHAR';
SQLINT : 'int' | 'INT' |'Int' | 'integer' | 'INTEGER' | 'Integer';
CREATE : 'create' | 'CREATE';
TABLE : 'table' | 'TABLE';
ID : (('a'..'z'|'A'..'Z' | '_') ((DIGIT)*))+;
INT : '0'..'9'+
;
FLOAT
: ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
| '.' ('0'..'9')+ EXPONENT?
| ('0'..'9')+ EXPONENT
;
COMMENT
: '--' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
| '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
STRING
: '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
;
CHAR: '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
;
fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESC
| OCTAL_ESC
;
fragment
OCTAL_ESC
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
fragment DIGIT : '0'..'9' ;
// Parser rules
colconstraint
: 'not' 'null' | 'primary' 'key';
coltype : SQLINT | SQLCHAR LPAR INT RPAR;
colname : ID;
colspec : colname coltype (colconstraint)*;
colspeclist
: colspec (COMMA colspec)*;
createtablestmt
: CREATE TABLE ID LPAR colspeclist RPAR;
More information about the antlr-interest
mailing list