I am using Antlr4 Python for PLSQL.
The grammar is from here:
https://github.com/antlr/grammars-v4/tree/master/sql/plsql
Here is the input – a very simple SQL script:
SELECT col1, col2
FROM tab1
ORDER BY col1;
The `od -c -h output for this script is:
od -c -h
0000000 S E L E C T c o l 1 , c o l
4553 454c 5443 6320 6c6f 2c31 6320 6c6f
0000020 2 n F R O M t a b 1 n O R D E
0a32 5246 4d4f 7420 6261 0a31 524f 4544
0000040 R B Y c o l 1 ; n
2052 5942 6320 6c6f 3b31 000a
0000053
Here is my Python script to print out the tokens:
from antlr4 import *
from antlr4.tree.Tree import TerminalNodeImpl
#from antlr4.tree.Tree import ParseTree
from antlr4.tree.Trees import Trees
from PlSqlLexer import PlSqlLexer
from PlSqlParser import PlSqlParser
from PlSqlParserListener import PlSqlParserListener
def handleTree(tree, lvl=0):
for child in tree.getChildren():
if isinstance(child, TerminalNode):
print(lvl*'│ ' + '└─', child)
else:
handleTree(child, lvl+1)
class KeyPrinter(PlSqlParserListener):
def enterSelect_statement(self, ctx):
handleTree(ctx, 0)
def main():
with open( "myscript.sql" ) as file:
filesrc = file.read()
lexer = PlSqlLexer(InputStream(filesrc))
tokens = CommonTokenStream(lexer)
tokens.fill()
parser = PlSqlParser(CommonTokenStream(lexer))
tree = parser.sql_script()
printer = KeyPrinter()
walker = ParseTreeWalker()
walker.walk(printer, tree)
if __name__ == '__main__':
main()
The output I get is:
line 4:0 mismatched input '<EOF>' expecting {'ABORT', 'ABS', 'ABOUT' ...........
.
.
...... 'EXTEND', 'MAXLEN', 'PERSISTABLE', 'POLYMORPHIC', 'STRUCT', 'TDO', 'WM_CONCAT', '.', DELIMITED_ID, '(', '_', PROMPT_MESSAGE, START_CMD, REGULAR_ID}
I thought maybe the isue is the grammar is for PLSQL rather than simple SQL, so I tried with a simple PLSQL script as follows:
DECLARE
l_tmp NUMBER ;
BEGIN
SELECT COUNT(1)
INTO l_tmp
FROM mytab;
DBMS_OUTPUT.PUT_LINE(' Count: ' || l_tmp );
END;
/
But I still the same error:
line 13:0 mismatched input '<EOF>' expecting {'ABORT', 'ABS', ...............
How to fix ?