-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
39 lines (28 loc) · 1.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from antlr4 import FileStream, CommonTokenStream
from MySqlParser import MySqlParser
from MySqlLexer import MySqlLexer
from antlr4 import ParseTreeWalker
from MySqlParserListener import MySqlParserListener
class MyMySQLListener(MySqlParserListener):
def enterEveryRule(self, ctx):
print("Entered:", ctx.getText())
def exitEveryRule(self, ctx):
pass
# print("Exited:", ctx.getText())
def main():
# Replace 'example.sql' with the path to the SQL file you want to parse
input_stream = FileStream("example.sql")
lexer = MySqlLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = MySqlParser(token_stream)
# This is the entry point to your parser, replace root() with the actual root rule of your grammar.
# Check MySQLParser.py to find the name of the root rule (it's often the first rule defined in the .g4 file)
tree = parser.root()
walker = ParseTreeWalker()
my_listener = MyMySQLListener()
walker.walk(my_listener, tree)
# If you want to do something with the tree, you can do it here.
# For instance, you could implement a Listener or a Visitor to walk the tree and do something useful.
pass
if __name__ == '__main__':
main()