Reputation: 19
I have created a Datalog parser using Antlr. I am not sure how to proceed. I want to generate the AST
Antlr has generated the following files:
Lexer.py
Listener.py
Lexer.tokens
Parser.py
Program.tokens
I have gone through the tutorial. I couldn't understand it. How do I test my input in Python:
a(X): b(X)
I want to generate the AST so that I could use it my Query Processing engine.
So I have figured out, I am writing code to generate the AST.
import sys
import antlr4
from antlr4 import *
from NLexer import *
from NParser import *
from NListener import *
char_stream = InputStream('a(1)\n')
lexer = NLexer(char_stream )
stream = CommonTokenStream(lexer)
parser = NParser(stream)
tree = parser.XXX() . // This is where I am confused. How do I generate
tree. What function should be called here
instead of XXX?
Upvotes: 0
Views: 599
Reputation: 53552
The function you must call depends on what you wanna parse. You can use any of the generated functions in your parser to parse a subpart of your language, but for the entire language you usually select the top level parser rule (you should know which is the top level rule, if you wrote the grammar).
All parser functions return a parse tree you can then use to find e.g. symbols or the input structure etc.
Btw. you are using ANTLR4, which generates parse trees, not abstract syntax trees.
Upvotes: 1