我在Python中使用antlr4来阅读以下语法:
https://github.com/antlr/grammars-v4/tree/master/plsql
文件授权.sql只有"开始从双选择'鲍勃';结束;">
像树一样打印Lisp的简单代码
from antlr4 import *
from PlSqlLexer import PlSqlLexer
from PlSqlParser import PlSqlParser
from PlSqlParserListener import PlSqlParserListener
input = FileStream('grants.sql')
lexer = PlSqlLexer(input)
stream = CommonTokenStream(lexer)
parser = PlSqlParser(stream)
tree = parser.sql_script()
print ("Tree " + tree.toStringTree(recog=parser));
因此,输出为:
树 (sql_script (unit_statement (anonymous_block 开始 (seq_of_statements (语句 (sql_statement (data_manipulation_language_statements (select_statement (子查询 (subquery_basic_elements (query_block 选择 (selected_element (select_list_elements (表达式 (表达式 (logical_expression (multiset_expression (relational_expression (compound_expression (连接 (model_expression (unary_expression (原子 (常量 (quoted_string 'bob'((from_((条款来自 (table_ref_list (table_ref (table_ref_aux (table_ref_aux_internal (dml_table_expression_clause (tableview_name (标识符 (id_expression (regular_id DUAL((;)(结束 ;)(
(我希望能够拥有python代码,而不是在类似lisp的语句中列出上述内容,而是列出所有规则和令牌。
- .sql_脚本
- ..unit_statement
- 。anonymous_block
- ....开始
等等
等等有人可以提供执行此操作的python代码或给我一些提示。感激不尽。
这是一个开始:
from antlr4 import *
from antlr4.tree.Tree import TerminalNodeImpl
from PlSqlLexer import PlSqlLexer
from PlSqlParser import PlSqlParser
# Generate the lexer nad parser like this:
#
# java -jar antlr-4.7.1-complete.jar -Dlanguage=Python3 *.g4
#
def main():
lexer = PlSqlLexer(InputStream("SELECT * FROM TABLE_NAME"))
parser = PlSqlParser(CommonTokenStream(lexer))
tree = parser.sql_script()
traverse(tree, parser.ruleNames)
def traverse(tree, rule_names, indent = 0):
if tree.getText() == "<EOF>":
return
elif isinstance(tree, TerminalNodeImpl):
print("{0}TOKEN='{1}'".format(" " * indent, tree.getText()))
else:
print("{0}{1}".format(" " * indent, rule_names[tree.getRuleIndex()]))
for child in tree.children:
traverse(child, rule_names, indent + 1)
if __name__ == '__main__':
main()
其中打印:
sql_script
unit_statement
data_manipulation_language_statements
select_statement
subquery
subquery_basic_elements
query_block
TOKEN='SELECT'
TOKEN='*'
from_clause
TOKEN='FROM'
table_ref_list
table_ref
table_ref_aux
table_ref_aux_internal
dml_table_expression_clause
tableview_name
identifier
id_expression
regular_id
TOKEN='TABLE_NAME'
请注意,为了使词法分析和解析器正常工作,我添加了以下 Python 类:
# PlSqlBaseLexer.py
from antlr4 import *
class PlSqlBaseLexer(Lexer):
def IsNewlineAtPos(self, pos):
la = self._input.LA(pos)
return la == -1 or la == 'n'
和:
# PlSqlBaseParser.py
from antlr4 import *
class PlSqlBaseParser(Parser):
_isVersion10 = False
_isVersion12 = True
def isVersion10(self):
return self._isVersion10
def isVersion12(self):
return self._isVersion12
def setVersion10(self, value):
self._isVersion10 = value
def setVersion12(self, value):
self._isVersion12 = value
我将其放置在与生成的 Python 类相同的文件夹中。我还需要将导入语句from PlSqlBaseLexer import PlSqlBaseLexer
在生成的PlSqlLexer.py
类中,并将导入语句从from ./PlSqlBaseParser import PlSqlBaseParser
PlSqlParser.py
修复为from PlSqlBaseParser import PlSqlBaseParser
。
请注意,运行演示相当慢。除非你在Python中很难做到这一点,否则我建议改用(更!(更快的Java或C#目标。