0

Mycode(in python):

lexer = VisualBasic6Lexer(antlr4.InputStream(code))
stream = antlr4.CommonTokenStream(lexer)
parser = VisualBasic6Parser(stream)
tree = parser.visitModule()  //This visitModule() is not the coreect method.
    print(tree)

I followed this link: Parsing some Java code with Python using ANTLR

Here,for Java they have used

tree = parser.compilationUnit()

In VB what is the alternative of this compilationUnit()

Grammar I used- https://github.com/antlr/grammars-v4/blob/master/vb6/VisualBasic6Lexer.g4 https://github.com/antlr/grammars-v4/blob/master/vb6/VisualBasic6Parser.g4

My goal is to export the Abstract Syntax Tree of a VB code using python into a JSON File.

See the output in this link https://github.com/Sdccoding/proleap-vb6-parser

I want my output to be in this beautified fashion.

1
  • The default print routine in the Antlr Python runtime does not pretty print with indentation. You're going to have to write an output routine of the tree, stack based or a recursive approach. See github.com/Sdccoding/proleap-vb6-parser/blob/master/src/test/… for an example. If you write one, write a second method to just output json. Commented Jun 12, 2021 at 0:20

1 Answer 1

0

Usually, the root node you need to visit is at the start of the parser file. Also, a characteristic for the root node is it ends with EOF.

For your case,

tree = parser.startRule()
Sign up to request clarification or add additional context in comments.

5 Comments

How to beautify the output I am getting? I am geeting in this manner: (startRule (module (moduleBody (moduleBodyElement (subStmt (visibility Private) I want this: (startRule (module (moduleBody (moduleBodyElement (subStmt
Seems your wanted tree is incomplete, btw, what's your input code?
Because long comments are not allowed,so I gave the incomplete output.What I mean to say is,I want to see beautified json(like trees),but what I am getting is not the beautified version...Its written (startRule (module (moduleBody (moduleBodyElement (subStmt (visibility Private) like this.
import json, then print json.dumps(tree, sort_keys=True, indent=4)
No change..Its still the same only.I want my output to be as github .com/Sdccoding/proleap-vb6-parser , this link's output(in tree like structure). What I am getting is plain string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.